使用Java的ImageIO的图片处理实现
程序员文章站
2022-06-04 15:22:43
...
import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.imageio.IIOImage; import javax.imageio.ImageIO; import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; import javax.imageio.stream.ImageOutputStream; import com.ysccc.api.parent.constant.ProjectException; import com.ysccc.api.parent.vo.Messages; public class ImageCompress { private static BufferedImage loadImage(InputStream in) throws IOException { return ImageIO.read(in); } private static BufferedImage loadImage(String filePath) throws IOException { FileInputStream is = null; BufferedImage image = null; try { File file = new File(filePath); if (file.exists() && file.isFile() && file.canRead()) { is = new FileInputStream(file); image = loadImage(is); } else { throw new ProjectException(Messages.IMAGE_FILE_READ_FAIL); } } catch (Exception e) { throw e; } finally { if (is != null) { is.close(); } } return image; } private static BufferedImage resizeImage(BufferedImage oldImage, int width, int height) throws IOException { int oldWidth = oldImage.getWidth(); int oldHeight = oldImage.getHeight(); ColorModel colorModel = oldImage.getColorModel(); BufferedImage newImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(width, height), colorModel.isAlphaPremultiplied(), null); // 说是能扫描的更清楚的写法,咱没感觉出来 Image img = oldImage.getScaledInstance(oldWidth, oldHeight, Image.SCALE_SMOOTH); Graphics2D graphics = newImage.createGraphics(); // 说是能抗锯齿,能平滑,能……,实际完全没感觉,是不是写的有问题呢 graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); graphics.drawImage(img, 0, 0, width, height, null); return newImage; } private static ImageWriteParam configCompressParam(ImageWriter writer, float quality) { ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(quality); return param; } private static void writeImage(ImageWriter writer, String filePath, BufferedImage image, float quality) throws IOException { OutputStream os = null; ImageOutputStream ios = null; try { os = new FileOutputStream(new File(filePath)); ios = ImageIO.createImageOutputStream(os); writer.setOutput(ios); writer.write(null, new IIOImage(image, null, null), configCompressParam(writer, quality)); } catch (Exception e) { throw e; } finally { if (os != null) { os.close(); } if (ios != null) { ios.close(); } if (writer != null) { writer.dispose(); } } } private static ImageWriter getWriter(String imageSuffix) { return ImageIO.getImageWritersByFormatName(imageSuffix).next(); } public static void main(String[] args) throws IOException { BufferedImage oImage = loadImage("C:\\Users\\Administrator\\Desktop\\bigImage\\_G7A2008.JPG"); BufferedImage nImage = resizeImage(oImage, (int) (oImage.getWidth() / 2), (int) (oImage.getHeight() / 2)); ImageWriter writer = getWriter("jpg"); writeImage(writer, "C:\\Users\\Administrator\\Desktop\\bigImage\\compress1.jpg", nImage, 0.5f); } }
写了这么多,如果按比例缩小图片的话,会发现一点都不清楚,按质量倒是没什么问题。
于是就放弃这种写法了,改用了Google的Thumbnails,发现简直差距太大了,唉……原生的代码果然不行啊
Thumbnails.of(new File("C:\\Users\\Administrator\\Desktop\\bigImage\\7.jpg")).scale(1f).outputFormat("jpg") .outputQuality(0.5) .toFile("C:\\Users\\Administrator\\Desktop\\bigImage\\compress2.jpg");
上一篇: Jsdoc注释标签
下一篇: 2018版中国行政区代码和名称JSON
推荐阅读
-
Java web实现动态图片验证码的示例代码
-
iOS开发中使用UIScrollView实现无限循环的图片浏览器
-
详解java中的深拷贝和浅拷贝(clone()方法的重写、使用序列化实现真正的深拷贝)
-
java实现图片文件与Base64的互转
-
如何使用jQuery定时器的原理实现banner图片切换
-
Python 使用PIL numpy 实现拼接图片的示例
-
java使用this调用构造函数的实现方法示例
-
Java实现拖拽文件上传dropzone.js的简单使用示例代码
-
微信开发中使用微信JSSDK和使用URL.createObjectURL上传预览图片的不同处理对比
-
微信开发中使用通用函数处理,以重定向的方式实现用户身份信息的获取并转回原来页面