package com.qianwen.smartman.common.utils; import cn.hutool.core.codec.Base64Encoder; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.math.BigDecimal; import javax.imageio.ImageIO; import net.coobird.thumbnailator.Thumbnails; import org.apache.commons.lang3.StringUtils; import com.qianwen.smartman.common.constant.DncConstant; import org.springframework.web.multipart.MultipartFile; public class PictureUtil { public static boolean isPicture(String imgName) { boolean flag = false; if (StringUtils.isBlank(imgName)) { return false; } String[] arr = {"bmp", "dib", "gif", "jfif", "jpe", "jpeg", "jpg", "png", "tif", "tiff", "ico"}; int length = arr.length; int i = 0; while (true) { if (i >= length) { break; } String item = arr[i]; if (!item.equals(imgName)) { i++; } else { flag = true; break; } } return flag; } public static String compressBase64(byte[] b, long desFileSize) throws IOException { double accuracy = 0.8d; if (104857.6d <= b.length && b.length <= 1048576) { accuracy = 0.3d; } else if (1048576 < b.length && b.length <= 2097152) { accuracy = 0.2d; } else if (2097152 < b.length) { accuracy = 0.1d; } byte[] bytes = commpressPicCycle(b, desFileSize, accuracy); if (bytes != null) { return Base64Encoder.encode(bytes); } return null; } public static String getSuffix(MultipartFile file) { String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(DncConstant.POINT) + 1); return suffix; } public static String commpressPicForScale(String srcPath, String desPath, long desFileSize, double accuracy, int desMaxWidth, int desMaxHeight) { if (StringUtils.isEmpty(srcPath) || StringUtils.isEmpty(srcPath) || !new File(srcPath).exists()) { return null; } try { File srcFile = new File(srcPath); long srcFileSize = srcFile.length(); System.out.println("源图片:" + srcPath + ",大小:" + (srcFileSize / 1024) + "kb"); BufferedImage bim = ImageIO.read(srcFile); int srcWidth = bim.getWidth(); int srcHeight = bim.getHeight(); Thumbnails.Builder builder = Thumbnails.of(new File[]{srcFile}).outputFormat("jpg"); if (srcWidth > desMaxWidth || srcHeight > desMaxHeight) { builder.size(desMaxWidth, desMaxHeight); } else { builder.size(srcWidth, srcHeight); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); builder.toOutputStream(baos); byte[] bytes = commpressPicCycle(baos.toByteArray(), desFileSize, accuracy); File desFile = new File(desPath); FileOutputStream fos = new FileOutputStream(desFile); fos.write(bytes); fos.close(); System.out.println("目标图片:" + desPath + ",大小" + (desFile.length() / 1024) + "kb"); System.out.println("图片压缩完成!"); return desPath; } catch (Exception e) { e.printStackTrace(); return null; } } private static byte[] commpressPicCycle(byte[] bytes, long desFileSize, double accuracy) throws IOException { long srcFileSizeJPG = bytes.length; if (srcFileSizeJPG <= desFileSize * 1024) { return bytes; } BufferedImage bim = ImageIO.read(new ByteArrayInputStream(bytes)); int srcWdith = bim.getWidth(); int srcHeigth = bim.getHeight(); int desWidth = new BigDecimal(srcWdith).multiply(new BigDecimal(accuracy)).intValue(); int desHeight = new BigDecimal(srcHeigth).multiply(new BigDecimal(accuracy)).intValue(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Thumbnails.of(new InputStream[]{new ByteArrayInputStream(bytes)}).size(desWidth, desHeight).outputQuality(accuracy).toOutputStream(baos); return commpressPicCycle(baos.toByteArray(), desFileSize, accuracy); } }