/**
* 处理图片
*
* @param fileBufferd 图片数据流
* @param tempFilePath 临时文件夹
* @return
*/
public void writeImage(BufferedImage fileBufferd, String tempFilePath) {
File file = new File(tempFilePath + (new Date().getTime()) + ".jpg");
//这么写是为了防止使用ImageIO.write后失真
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpg");
if (iter.hasNext()) {
ImageWriter writer = iter.next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.92f);
try {
FileImageOutputStream out = new FileImageOutputStream(file);
writer.setOutput(out);
// writer.write(bi);
writer.write(null, new IIOImage(fileBufferd, null, null), param);
out.close();
writer.dispose();
} catch (Exception ex) {
System.out.println("===异常了==");
}
}
}
大小可设置。 经测试 0.92 与原图差不多。