/**
* 对图片进行原比例无损压缩,压缩后覆盖原图片
*
*/
private static void doWithPhoto(String base64) {
File file = new File(path);
if (!file.exists()) {
return;
}
BufferedImage image = null;
FileOutputStream os = null;
try {
image = ImageIO.read(file);
int width = image.getWidth();
int height = image.getHeight();
BufferedImage bfImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
bfImage.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
os = new FileOutputStream(base64);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
encoder.encode(bfImage);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
对图片进行原比例无损压缩,压缩后覆盖原图片
于 2022-07-11 17:42:37 首次发布