Commit 2fba11ca authored by 张永's avatar 张永

上传图片根据压缩

parent 028a8da9
...@@ -151,6 +151,14 @@ ...@@ -151,6 +151,14 @@
</dependency> </dependency>
<!-- 简体转繁体end --> <!-- 简体转繁体end -->
<!-- 图片压缩 https://www.cnblogs.com/yinjing/p/12157562.html-->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.20</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -528,6 +528,12 @@ public class FileUtil { ...@@ -528,6 +528,12 @@ public class FileUtil {
return FileUtil.createFile(new File(name)); return FileUtil.createFile(new File(name));
} }
public static void main(String[] args) {
String name = "d:/data/erp/excel/QY68011/";
createFolder(name);
}
public static File createFile(File file) { public static File createFile(File file) {
File folder = file.getParentFile(); File folder = file.getParentFile();
if (!(folder.exists() && folder.isDirectory())) { if (!(folder.exists() && folder.isDirectory())) {
......
package com.egolm.common;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class ImageUtil {
//压缩图片
public static boolean compressImage(String sourceImagePath, String outputImagePath,int desFileSize) {
Set<String> extSet = new HashSet<String>();
extSet.add("jpg");
extSet.add("png");
System.out.println("desFileSize----"+desFileSize);
String extName = FileUtil.getExtName(new File(sourceImagePath)).toLowerCase();
if(!extSet.contains(extName)) {
return false;
}
byte[] images = ThumbnailsUtil.compressPicForScale(sourceImagePath, outputImagePath, desFileSize);
if(images != null) {
File dFile = new File(outputImagePath);
if(dFile.exists()) {
return true;
}
}
return false;
}
public static void main(String[] args) throws IOException {
float imageSizeKB = 100;
String sourceImagePath = "C:\\Users\\zhangyong\\Desktop\\jpg\\9.png";
String outputImagePath = "C:\\Users\\zhangyong\\Desktop\\jpg\\9_"+imageSizeKB+".png";
boolean res = compressImage(sourceImagePath, outputImagePath,50);
System.out.println(res);
}
}
...@@ -1232,7 +1232,7 @@ public class StringUtil { ...@@ -1232,7 +1232,7 @@ public class StringUtil {
public static void main(String[] args) { public static void main(String[] args) {
String original = " \"VendorName\": \"萬里行有限公司\","; String original = "采购订单收货,支持无订单快捷收货,收货后增加商铺库存。应用业务参数::ShowCheckMsg(制单时提醒是否审核)";
String result = ZhConverterUtil.toTraditional(original); String result = ZhConverterUtil.toTraditional(original);
System.out.println("opecc : "+result); System.out.println("opecc : "+result);
String result1 = ICU4JConverter(original); String result1 = ICU4JConverter(original);
......
package com.egolm.common;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import javax.swing.ImageIcon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.coobird.thumbnailator.Thumbnails;
//https://blog.csdn.net/hechurui/article/details/129042805
//
public class ThumbnailsUtil {
private static Logger logger = LoggerFactory.getLogger(ThumbnailsUtil.class);
//desFileSize 指定多少KB
public static byte[] compressPicForScale(String filePath, String compressPath, long desFileSize) {
System.out.println("【图片压缩开始】");
byte[] imageBytes = new byte[0];
try {
imageBytes = FileUtil.fileToBytes(new File(filePath));
//判断大小,如果小于desFileSize kb,不用压缩,如果大于等于 desFileSize kb,需要压缩
if (imageBytes == null || imageBytes.length <= 0 || imageBytes.length < desFileSize * 1024) {
return imageBytes;
}
long srcSize = imageBytes.length;
double accuracy = getAccuracy(srcSize / 1024);
System.out.println("accuracy : "+accuracy);
// 防止压缩后图片变红
Image src = Toolkit.getDefaultToolkit().getImage(filePath);
BufferedImage image = toBufferedImage(src);
// 第一次压缩
Thumbnails.of(image).scale(accuracy).toFile(compressPath);
imageBytes = FileUtil.fileToBytes(new File(compressPath));
System.out.println("imageBytes length : "+imageBytes.length +"------desFileSize * 1024: "+desFileSize * 1024);
while (imageBytes.length > desFileSize * 1024) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);
Thumbnails.of(inputStream).scale(accuracy).outputQuality(accuracy).toOutputStream(outputStream);
imageBytes = outputStream.toByteArray();
System.out.println("imageBytes length : "+imageBytes.length );
}
System.out.println("【图片压缩结束】 图片原大小= "+srcSize / 1024+" kb | 压缩后大小= "+imageBytes.length / 1024+" kb" );
} catch (Exception e) {
System.out.println("【图片压缩】msg=图片压缩失败!"+ e);
}
return imageBytes;
}
/**
* 自动调节精度(经验数值)
*
* @param size 源图片大小
* @return 图片压缩质量比
*/
private static double getAccuracy(long size) {
double accuracy;
if (size < 900) {
accuracy = 0.85;
} else if (size < 2047) {
accuracy = 0.6;
} else if (size < 3275) {
accuracy = 0.44;
} else {
accuracy = 0.4;
}
return accuracy;
}
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
int transparency = Transparency.OPAQUE;
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
} catch (HeadlessException e) {
// The system does not have a screen
}
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
public static void main(String[] args) {
String s = "C:\\Users\\zhangyong\\Desktop\\jpg\\2023_12_08_09_21_32_21723102_11.jpg";
String d = "C:\\Users\\zhangyong\\Desktop\\jpg\\2023_12_08_09_21_32_21723102_12.jpg";
compressPicForScale(s,d,1);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment