parent
39b76479fd
commit
072d61a722
@ -0,0 +1,307 @@ |
||||
package com.hai.common.utils; |
||||
|
||||
|
||||
import com.hai.common.QRCodeGenerator; |
||||
import com.hai.config.CommonSysConst; |
||||
import sun.misc.BASE64Decoder; |
||||
import sun.misc.BASE64Encoder; |
||||
|
||||
import javax.imageio.ImageIO; |
||||
import javax.swing.*; |
||||
import java.awt.*; |
||||
import java.awt.image.BufferedImage; |
||||
import java.io.*; |
||||
import java.net.HttpURLConnection; |
||||
import java.net.URL; |
||||
import java.util.Objects; |
||||
|
||||
/** |
||||
* 图片处理 |
||||
*/ |
||||
public class ImageUtils { |
||||
|
||||
public static void main(String[] args) throws Exception { |
||||
String a = ImageToBase64ByOnline("https://oos.jyfwy.cn/common/test/test20210511.jpg"); |
||||
System.out.println(a); |
||||
} |
||||
|
||||
public static String transferAlphaForHttp(InputStream inputStream){ |
||||
try { |
||||
byte[] data = transferAlpha(null,inputStream,130); |
||||
return byteArrayToBase64(data); |
||||
}catch (Exception e){ |
||||
e.printStackTrace(); |
||||
} |
||||
return ""; |
||||
} |
||||
|
||||
public static String byteArrayToBase64(byte[] bytes) { |
||||
BASE64Encoder encoder = new BASE64Encoder(); |
||||
String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串
|
||||
png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
|
||||
System.out.println("值为:" + "data:image/jpg;base64," + png_base64); |
||||
return "data:image/jpg;base64," + png_base64; |
||||
} |
||||
|
||||
/** |
||||
* 图片去白色的背景,并裁切 |
||||
* |
||||
* @param image 图片 |
||||
* @param range 范围 1-255 越大 容错越高 去掉的背景越多 |
||||
* @return 图片 |
||||
* @throws Exception 异常 |
||||
*/ |
||||
public static byte[] transferAlpha(Image image, InputStream in, int range) throws Exception { |
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
||||
try { |
||||
if (image == null && in != null) { |
||||
image = ImageIO.read(in); |
||||
} |
||||
ImageIcon imageIcon = new ImageIcon(image); |
||||
BufferedImage bufferedImage = new BufferedImage(imageIcon |
||||
.getIconWidth(), imageIcon.getIconHeight(), |
||||
BufferedImage.TYPE_4BYTE_ABGR); |
||||
Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics(); |
||||
g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon |
||||
.getImageObserver()); |
||||
int alpha = 0; |
||||
int minX = bufferedImage.getWidth(); |
||||
int minY = bufferedImage.getHeight(); |
||||
int maxX = 0; |
||||
int maxY = 0; |
||||
|
||||
for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage |
||||
.getHeight(); j1++) { |
||||
for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage |
||||
.getWidth(); j2++) { |
||||
int rgb = bufferedImage.getRGB(j2, j1); |
||||
|
||||
int R = (rgb & 0xff0000) >> 16; |
||||
int G = (rgb & 0xff00) >> 8; |
||||
int B = (rgb & 0xff); |
||||
if (((255 - R) < range) && ((255 - G) < range) && ((255 - B) < range)) { //去除白色背景;
|
||||
rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff); |
||||
} else { |
||||
minX = minX <= j2 ? minX : j2; |
||||
minY = minY <= j1 ? minY : j1; |
||||
maxX = maxX >= j2 ? maxX : j2; |
||||
maxY = maxY >= j1 ? maxY : j1; |
||||
} |
||||
bufferedImage.setRGB(j2, j1, rgb); |
||||
} |
||||
} |
||||
int width = maxX - minX; |
||||
int height = maxY - minY; |
||||
BufferedImage sub = bufferedImage.getSubimage(minX, minY, width, height); |
||||
ImageIO.write(sub, "png", byteArrayOutputStream); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
throw e; |
||||
} |
||||
|
||||
return byteArrayOutputStream.toByteArray(); |
||||
} |
||||
|
||||
public static byte[] transferAlphaAndRotate(Image image, InputStream in, int range, double angel) throws Exception { |
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
||||
try { |
||||
if (image == null && in != null) { |
||||
image = ImageIO.read(in); |
||||
} |
||||
image = imageRotate(image,angel); |
||||
ImageIcon imageIcon = new ImageIcon(image); |
||||
BufferedImage bufferedImage = new BufferedImage(imageIcon |
||||
.getIconWidth(), imageIcon.getIconHeight(), |
||||
BufferedImage.TYPE_4BYTE_ABGR); |
||||
Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics(); |
||||
g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon |
||||
.getImageObserver()); |
||||
int alpha = 0; |
||||
int minX = bufferedImage.getWidth(); |
||||
int minY = bufferedImage.getHeight(); |
||||
int maxX = 0; |
||||
int maxY = 0; |
||||
|
||||
for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage |
||||
.getHeight(); j1++) { |
||||
for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage |
||||
.getWidth(); j2++) { |
||||
int rgb = bufferedImage.getRGB(j2, j1); |
||||
|
||||
int R = (rgb & 0xff0000) >> 16; |
||||
int G = (rgb & 0xff00) >> 8; |
||||
int B = (rgb & 0xff); |
||||
if (((255 - R) < range) && ((255 - G) < range) && ((255 - B) < range)) { //去除白色背景;
|
||||
rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff); |
||||
} else { |
||||
minX = minX <= j2 ? minX : j2; |
||||
minY = minY <= j1 ? minY : j1; |
||||
maxX = maxX >= j2 ? maxX : j2; |
||||
maxY = maxY >= j1 ? maxY : j1; |
||||
} |
||||
bufferedImage.setRGB(j2, j1, rgb); |
||||
} |
||||
} |
||||
int width = maxX - minX; |
||||
int height = maxY - minY; |
||||
BufferedImage sub = bufferedImage.getSubimage(minX, minY, width, height); |
||||
ImageIO.write(sub, "png", byteArrayOutputStream); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
throw e; |
||||
} |
||||
|
||||
return byteArrayOutputStream.toByteArray(); |
||||
} |
||||
|
||||
/** |
||||
* 图像旋转 |
||||
* @param src |
||||
* @param angel |
||||
* @return |
||||
*/ |
||||
public static BufferedImage imageRotate(Image src, double angel) { |
||||
int src_width = src.getWidth(null); |
||||
int src_height = src.getHeight(null); |
||||
// calculate the new image size
|
||||
Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension( |
||||
src_width, src_height)), angel); |
||||
|
||||
BufferedImage res = null; |
||||
res = new BufferedImage(rect_des.width, rect_des.height, |
||||
BufferedImage.TYPE_3BYTE_BGR); |
||||
Graphics2D g2 = res.createGraphics(); |
||||
// transform
|
||||
g2.translate((rect_des.width - src_width) / 2, |
||||
(rect_des.height - src_height) / 2); |
||||
g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2); |
||||
|
||||
g2.drawImage(src, null, null); |
||||
return res; |
||||
} |
||||
|
||||
public static Rectangle CalcRotatedSize(Rectangle src, double angel) { |
||||
// if angel is greater than 90 degree, we need to do some conversion
|
||||
if (angel >= 90) { |
||||
if(angel / 90 % 2 == 1){ |
||||
int temp = src.height; |
||||
src.height = src.width; |
||||
src.width = temp; |
||||
} |
||||
angel = angel % 90; |
||||
} |
||||
|
||||
double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2; |
||||
double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r; |
||||
double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2; |
||||
double angel_dalta_width = Math.atan((double) src.height / src.width); |
||||
double angel_dalta_height = Math.atan((double) src.width / src.height); |
||||
|
||||
int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha |
||||
- angel_dalta_width)); |
||||
len_dalta_width=len_dalta_width>0?len_dalta_width:-len_dalta_width; |
||||
|
||||
int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha |
||||
- angel_dalta_height)); |
||||
len_dalta_height=len_dalta_height>0?len_dalta_height:-len_dalta_height; |
||||
|
||||
int des_width = src.width + len_dalta_width * 2; |
||||
int des_height = src.height + len_dalta_height * 2; |
||||
des_width=des_width>0?des_width:-des_width; |
||||
des_height=des_height>0?des_height:-des_height; |
||||
return new Rectangle(new Dimension(des_width, des_height)); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* base64转inputStream |
||||
* @param base64string |
||||
* @return |
||||
*/ |
||||
public static InputStream BaseToInputStream(String base64string){ |
||||
ByteArrayInputStream stream = null; |
||||
try { |
||||
BASE64Decoder decoder = new BASE64Decoder(); |
||||
byte[] bytes1 = decoder.decodeBuffer(base64string); |
||||
stream = new ByteArrayInputStream(bytes1); |
||||
} catch (Exception e) { |
||||
// TODO: handle exception
|
||||
} |
||||
return stream; |
||||
} |
||||
|
||||
/** |
||||
* 获取网络图片流 |
||||
* |
||||
* @param url |
||||
* @return |
||||
*/ |
||||
public static InputStream getImageStream(String url) { |
||||
try { |
||||
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); |
||||
connection.setReadTimeout(5000); |
||||
connection.setConnectTimeout(5000); |
||||
connection.setRequestMethod("GET"); |
||||
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { |
||||
InputStream inputStream = connection.getInputStream(); |
||||
return inputStream; |
||||
} |
||||
} catch (IOException e) { |
||||
System.out.println("获取网络图片出现异常,图片路径为:" + url); |
||||
e.printStackTrace(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
|
||||
public static String ImageToBase64ByOnline(String imgURL) { |
||||
ByteArrayOutputStream data = new ByteArrayOutputStream(); |
||||
try { |
||||
// 创建URL
|
||||
URL url = new URL(imgURL); |
||||
byte[] by = new byte[1024]; |
||||
// 创建链接
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
||||
conn.setRequestMethod("GET"); |
||||
conn.setConnectTimeout(5000); |
||||
InputStream is = conn.getInputStream(); |
||||
// 将内容读取内存中
|
||||
int len = -1; |
||||
while ((len = is.read(by)) != -1) { |
||||
data.write(by, 0, len); |
||||
} |
||||
// 关闭流
|
||||
is.close(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
// 对字节数组Base64编码
|
||||
BASE64Encoder encoder = new BASE64Encoder(); |
||||
return encoder.encode(data.toByteArray()); |
||||
} |
||||
|
||||
public static String ImageToBase64(String imgPath) { |
||||
byte[] data = null; |
||||
// 读取图片字节数组
|
||||
try { |
||||
InputStream in = new FileInputStream(imgPath); |
||||
data = new byte[in.available()]; |
||||
in.read(data); |
||||
in.close(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
// 对字节数组Base64编码
|
||||
BASE64Encoder encoder = new BASE64Encoder(); |
||||
// 返回Base64编码过的字节数组字符串
|
||||
return encoder.encode(Objects.requireNonNull(data)); |
||||
} |
||||
|
||||
public static String QrCodeImg(String imgPath , Integer id) throws Exception { |
||||
// 生成二维码
|
||||
String qrCodeImg = "qrCode/" + id + ".png"; |
||||
String qrCodeUrl = CommonSysConst.getSysConfig().getFileUrl() + qrCodeImg; |
||||
QRCodeGenerator.generateQRCodeImage(imgPath + id, 134, 134, qrCodeUrl); |
||||
return qrCodeImg; |
||||
} |
||||
} |
@ -0,0 +1,169 @@ |
||||
package com.hai.config; |
||||
|
||||
import org.springframework.context.annotation.Configuration; |
||||
import sun.font.FontDesignMetrics; |
||||
|
||||
import javax.imageio.ImageIO; |
||||
import java.awt.*; |
||||
import java.awt.image.BufferedImage; |
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.IOException; |
||||
import java.io.OutputStream; |
||||
import java.util.Map; |
||||
import java.util.zip.ZipEntry; |
||||
import java.util.zip.ZipOutputStream; |
||||
|
||||
/** |
||||
* @serviceName QrCodeUtilsConfig.java |
||||
* @author Sum1Dream |
||||
* @version 1.0.0 |
||||
* @Description // 工具
|
||||
* @createTime 09:54 2022/4/13 |
||||
**/ |
||||
@Configuration |
||||
public class CommonConfig { |
||||
|
||||
//得到该字体字符串的长度
|
||||
public static int getWordWidth(Font font, String content) { |
||||
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font); |
||||
int width = 0; |
||||
for (int i = 0; i < content.length(); i++) { |
||||
width += metrics.charWidth(content.charAt(i)); |
||||
} |
||||
return width; |
||||
} |
||||
|
||||
// 二维码生产自定义图片
|
||||
public static void overlapImage(String path , String content) throws Exception { |
||||
BufferedImage read = ImageIO.read(new File(CommonSysConst.getSysConfig().getFileUrl() + "img/original_image.png")); |
||||
Image image = ImageIO.read(new File(CommonSysConst.getSysConfig().getFileUrl() + "/" + path)); |
||||
|
||||
Graphics graphics = read.getGraphics(); |
||||
|
||||
graphics.drawImage(image, (read.getWidth() - image.getWidth(null))/2 ,306, null); |
||||
graphics.setColor(Color.BLACK); |
||||
//定义字体
|
||||
Font font = new Font("微软雅黑", Font.PLAIN, 14); |
||||
//计算该字体文本的长度
|
||||
int wordWidth = CommonConfig.getWordWidth(font, content); |
||||
|
||||
graphics.setFont(font); |
||||
graphics.drawString( content, (read.getWidth() - wordWidth)/2 ,304 + image.getHeight(null)); |
||||
|
||||
ImageIO.write(read , "png" , new File(CommonSysConst.getSysConfig().getFileUrl() + "/" + path)); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 多文件压缩成ZIP |
||||
* |
||||
* @param imageMap 需要压缩的文件列表,键值对为 <文件名,文件的字节数组>,文件名必须包含后缀 |
||||
* @param out 压缩文件输出流 |
||||
* @throws RuntimeException 压缩失败会抛出运行时异常 |
||||
*/ |
||||
public static void toZip(Map<String, byte[]> imageMap, OutputStream out) throws RuntimeException { |
||||
long start = System.currentTimeMillis(); |
||||
try (ZipOutputStream zos = new ZipOutputStream(out)) { |
||||
for (Map.Entry<String, byte[]> map : imageMap.entrySet()) { |
||||
zos.putNextEntry(new ZipEntry(map.getKey())); |
||||
zos.write(map.getValue()); |
||||
zos.closeEntry(); |
||||
} |
||||
long end = System.currentTimeMillis(); |
||||
System.out.println("压缩完成,耗时:" + (end - start) + " ms"); |
||||
} catch (Exception e) { |
||||
throw new RuntimeException("zip error from ZipUtils", e); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* |
||||
* 文件夹压缩成ZIP |
||||
* |
||||
* @param srcDir 压缩文件夹路径 |
||||
* @param out 压缩文件输出流 |
||||
* @param keepDirStructure 是否保留原来的目录结构,true:保留目录结构; |
||||
* |
||||
* false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) |
||||
* |
||||
* @throws RuntimeException 压缩失败会抛出运行时异常 |
||||
* |
||||
*/ |
||||
public static void toZip(String srcDir, OutputStream out, boolean keepDirStructure) |
||||
throws RuntimeException { |
||||
long start = System.currentTimeMillis(); |
||||
ZipOutputStream zos = null; |
||||
try { |
||||
zos = new ZipOutputStream(out); |
||||
File sourceFile = new File(srcDir); |
||||
compress(sourceFile, zos, sourceFile.getName(), keepDirStructure); |
||||
long end = System.currentTimeMillis(); |
||||
System.out.println("压缩完成,耗时:" + (end - start) + " ms"); |
||||
} catch (Exception e) { |
||||
throw new RuntimeException("zip error from ZipUtils", e); |
||||
} finally { |
||||
if (zos != null) { |
||||
try { |
||||
zos.close(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* 递归压缩方法 |
||||
* @param sourceFile 源文件 |
||||
* @param zos zip输出流 |
||||
* @param name 压缩后的名称 |
||||
* @param keepDirStructure 是否保留原来的目录结构,true:保留目录结构; |
||||
* |
||||
* false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) |
||||
* @throws Exception |
||||
*/ |
||||
private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean keepDirStructure) throws Exception { |
||||
byte[] buf = new byte[2048]; |
||||
if (sourceFile.isFile()) { |
||||
// 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
|
||||
zos.putNextEntry(new ZipEntry(name)); |
||||
// copy文件到zip输出流中
|
||||
int len; |
||||
FileInputStream in = new FileInputStream(sourceFile); |
||||
while ((len = in.read(buf)) != -1) { |
||||
zos.write(buf, 0, len); |
||||
} |
||||
// Complete the entry
|
||||
zos.closeEntry(); |
||||
in.close(); |
||||
} else { |
||||
File[] listFiles = sourceFile.listFiles(); |
||||
if (listFiles == null || listFiles.length == 0) { |
||||
// 需要保留原来的文件结构时,需要对空文件夹进行处理
|
||||
if (keepDirStructure) { |
||||
// 空文件夹的处理
|
||||
zos.putNextEntry(new ZipEntry(name + "/")); |
||||
// 没有文件,不需要文件的copy
|
||||
zos.closeEntry(); |
||||
} |
||||
} else { |
||||
for (File file : listFiles) { |
||||
// 判断是否需要保留原来的文件结构
|
||||
if (keepDirStructure) { |
||||
// 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
|
||||
// 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
|
||||
compress(file, zos, name + "/" + file.getName(), keepDirStructure); |
||||
} else { |
||||
compress(file, zos, file.getName(), keepDirStructure); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
|
||||
} |
@ -1,7 +1,30 @@ |
||||
package com.hai.dao; |
||||
|
||||
import org.apache.ibatis.annotations.Param; |
||||
import org.apache.ibatis.annotations.Select; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* mapper扩展类 |
||||
*/ |
||||
public interface BsDistributionRebateMapperExt { |
||||
|
||||
|
||||
@Select({ |
||||
"<script> " + |
||||
" select count(*) as count , sum(integral_num) as sum from bs_distribution_rebate where popularize_user_id = #{userId} " + |
||||
"</script>" |
||||
}) |
||||
Map<String, Object> countDistribution(@Param("userId") Long userId); |
||||
|
||||
@Select({ |
||||
"<script> " + |
||||
" SELECT sum(integral_num) FROM bs_distribution_rebate WHERE DATEDIFF(create_time,NOW())=0 and popularize_user_id = #{userId} " + |
||||
"</script>" |
||||
}) |
||||
BigDecimal toDayIntegralNum(@Param("userId") Long userId); |
||||
|
||||
|
||||
} |
@ -1,7 +1,14 @@ |
||||
package com.hai.dao; |
||||
|
||||
import org.apache.ibatis.annotations.Param; |
||||
import org.apache.ibatis.annotations.Select; |
||||
|
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* mapper扩展类 |
||||
*/ |
||||
public interface BsRequestRecordMapperExt { |
||||
|
||||
|
||||
} |
@ -0,0 +1,49 @@ |
||||
package com.hai.enum_type; |
||||
|
||||
import java.util.Objects; |
||||
|
||||
/** |
||||
* 产品图片 |
||||
* @author hurui |
||||
*/ |
||||
public enum OrderLogoEnum { |
||||
name1("orderLogo/coupon.png", 1), |
||||
name3("orderLogo/refuel.png", 3), |
||||
name4("orderLogo/kfc.png", 4), |
||||
name9("orderLogo/starbucks.png", 9), |
||||
name10("orderLogo/member-recharge.png", 10), |
||||
name20("orderLogo/call-charges.png", 20), |
||||
; |
||||
|
||||
|
||||
private String imgUrl; |
||||
|
||||
private Integer type; |
||||
public static String getNameByImgUrl(Integer type) { |
||||
for (OrderLogoEnum ele : values()) { |
||||
if(Objects.equals(type,ele.getType())) return ele.getImgUrl(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
OrderLogoEnum(String imgUrl, Integer type) { |
||||
this.imgUrl = imgUrl; |
||||
this.type = type; |
||||
} |
||||
|
||||
public Integer getType() { |
||||
return type; |
||||
} |
||||
|
||||
public void setType(Integer type) { |
||||
this.type = type; |
||||
} |
||||
|
||||
public String getImgUrl() { |
||||
return imgUrl; |
||||
} |
||||
|
||||
public void setImgUrl(String imgUrl) { |
||||
this.imgUrl = imgUrl; |
||||
} |
||||
} |
Loading…
Reference in new issue