使用Java的ImageIO的图片处理实现

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");

猜你喜欢

转载自peak.iteye.com/blog/2318803