2024-04-14 21:05:01 +08:00

156 lines
6.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.zhgd.xmgl.util;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import com.zhgd.xmgl.base.ImgTextBean;
import lombok.extern.slf4j.Slf4j;
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
**/
@Slf4j
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){
log.error("error", e);
}catch (IOException e){
log.error("error", e);
}
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){
log.error("error", e);
}
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){
log.error("error", e);
}
}
}