运用Java的ImageIO的图片处理完成ITeye - 威尼斯人

运用Java的ImageIO的图片处理完成ITeye

2019-01-11 17:37:02 | 作者: 青旋 | 标签: 清楚,写法,感觉 | 浏览: 2972

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");
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表威尼斯人立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章