154 lines
5.9 KiB
Java
Raw Normal View History

2023-02-16 15:28:15 +08:00
package com.zhgd.xmgl.util;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import com.zhgd.xmgl.base.ImgTextBean;
import net.coobird.thumbnailator.Thumbnails;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;
/**
* @program: wisdomSite
* @description: 图片处理
* @author: Mr.Peng
* @create: 2021-07-14 16:03
**/
public class ImageUtils {
/**
*
* @param bytes 原图片字节数组
* @param desFileSize 指定图片大小,单位 kb
* @param accuracy 精度,递归压缩的比率,建议小于0.9
* @return
*/
/* public static byte[] commpressPicCycle(byte[] bytes, long desFileSize, double accuracy) throws IOException {
// 获取目标图片
// File imgFile = new File(desPath);
// long fileSize = imgFile.length();
long fileSize = bytes.length;
log.info("=====fileSize======== "+fileSize);
// 判断图片大小是否小于指定图片大小
if(fileSize <= desFileSize * 1024){
return bytes;
}
//计算宽高
BufferedImage bim = ImageIO.read(new ByteArrayInputStream(bytes));
int imgWidth = bim.getWidth();
log.info(imgWidth+"====imgWidth=====");
int imgHeight = bim.getHeight();
int desWidth = new BigDecimal(imgWidth).multiply( new BigDecimal(accuracy)).intValue();
log.info(desWidth+"====desWidth=====");
int desHeight = new BigDecimal(imgHeight).multiply( new BigDecimal(accuracy)).intValue();
//字节输出流(写入到内存)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Thumbnails.of(new ByteArrayInputStream(bytes)).size(desWidth, desHeight).outputQuality(accuracy).toOutputStream(baos);
//如果不满足要求,递归直至满足要求
return commpressPicCycle(baos.toByteArray(), desFileSize, accuracy);
}*/
public static byte[] commpressPicCycle(byte[] bytes, long desFileSize, double accuracy) throws IOException {
// 获取目标图片
// File imgFile = new File(desPath);
// long fileSize = imgFile.length();
long fileSize = bytes.length;
//log.info("=====fileSize======== "+fileSize);
// 判断图片大小是否小于指定图片大小
if(fileSize <= desFileSize * 1024){
return bytes;
}
//计算宽高
BufferedImage bim = ImageIO.read(new ByteArrayInputStream(bytes));
int imgWidth = bim.getWidth();
//log.info(imgWidth+"====imgWidth=====");
int imgHeight = bim.getHeight();
int desWidth = new BigDecimal(imgWidth).multiply( new BigDecimal(accuracy)).intValue();
//log.info(desWidth+"====desWidth=====");
int desHeight = new BigDecimal(imgHeight).multiply( new BigDecimal(accuracy)).intValue();
//字节输出流(写入到内存)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Thumbnails.of(new ByteArrayInputStream(bytes)).size(desWidth, desHeight).outputQuality(accuracy).toOutputStream(baos);
//如果不满足要求,递归直至满足要求
return commpressPicCycle(baos.toByteArray(), desFileSize, accuracy);
}
public static byte[] File2byte(File tradeFile){
byte[] buffer = null;
try
{
FileInputStream fis = new FileInputStream(tradeFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1)
{
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return buffer;
}
/**
* 添加水印
* @param inputStream
* @param list
* @return
*/
public static BufferedImage pressText(InputStream inputStream,java.util.List<ImgTextBean> list) {
BufferedImage targetImage=null;
try {
targetImage= ImgUtil.read(inputStream);
Graphics2D g = targetImage.createGraphics();
if(list!=null&&list.size()>0){
for(ImgTextBean imgTextBean:list){
if(null == imgTextBean.getFont()) {
// 默认字体
imgTextBean.setFont(new Font("Courier", Font.PLAIN, (int)(targetImage.getHeight() * 0.75)));
}
// 抗锯齿
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(imgTextBean.getColor());
g.setFont(imgTextBean.getFont());
// 透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, imgTextBean.getAlpha()));
// 在指定坐标绘制水印文字
FontMetrics metrics = g.getFontMetrics(imgTextBean.getFont());
int textLength = metrics.stringWidth(imgTextBean.getPressText());
int textHeight = metrics.getAscent() - metrics.getLeading() - metrics.getDescent();
g.drawString(imgTextBean.getPressText(), Math.abs(targetImage.getWidth() - textLength) / 2 + imgTextBean.getX(), Math.abs(targetImage.getHeight() + textHeight) / 2 + imgTextBean.getY());
}
}
g.dispose();
}catch (Exception e){
e.printStackTrace();
}
return targetImage;
}
public static void main(String[] args) {
try {
File file=new File("E:/11.jpg");
byte[] by=commpressPicCycle(File2byte(file),45,0.5f);
FileUtil.writeBytes(by,"E:/22.jpg");
}catch (Exception e){
e.printStackTrace();
}
}
}