- package com.cy.thumb;
- import java.awt.Rectangle;
- import java.awt.image.BufferedImage;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Iterator;
- import javax.imageio.ImageIO;
- import javax.imageio.ImageReadParam;
- import javax.imageio.ImageReader;
- import javax.imageio.stream.ImageInputStream;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
- public class Thumb {
- public static void main(String[] args) throws IOException {
- //把图片image.png 长宽等比缩小5倍。
- reduceImage("E:/image.png", "E:/image1.png", 5);
- //把图片image.png 长宽各设置为100
- reduceImage("E:/image.png", "E:/image2.png", 100, 100);
- }
- /**
- * 对图片进行剪裁 返回字节数组
- * @param is 图片输入流
- * @param width 裁剪图片的宽
- * @param height 裁剪图片的高
- * @param imageFormat 输出图片的格式 "jpeg jpg等"
- * @return
- */
- public static byte[] clipImage(InputStream is, int width, int height, String imageFormat) {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- try {
- // 构造Image对象
- BufferedImage src = javax.imageio.ImageIO.read(is);
- // 缩小边长
- BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- // 绘制 缩小 后的图片
- tag.getGraphics().drawImage(src, 0, 0, width, height, null);
- ImageIO.write(tag, imageFormat, bos);
- } catch(IOException e) {
- e.printStackTrace();
- }
- return bos.toByteArray();
- }
- /**
- * 重置图片大小
- * @param srcImagePath 读取图片路径
- * @param toImagePath 写入图片路径
- * @param width 重新设置图片的宽
- * @param height 重新设置图片的高
- * @throws IOException
- */
- public static void reduceImage(String srcImagePath, String toImagePath, int width, int height) throws IOException {
- FileOutputStream out = null;
- try {
- //读入文件
- File file = new File(srcImagePath);
- // 构造Image对象
- BufferedImage src = javax.imageio.ImageIO.read(file);
- // 缩小边长
- BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- // 绘制 缩小 后的图片
- tag.getGraphics().drawImage(src, 0, 0, width, height, null);
- out = new FileOutputStream(toImagePath);
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(tag);
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- if (out != null) {
- out.close();
- }
- }
- }
- /**
- * 按倍率缩小图片
- * @param srcImagePath 读取图片路径
- * @param toImagePath 写入图片路径
- * @param ratio 缩小比率 宽、高一起等比率缩小
- * @throws IOException
- */
- public static void reduceImage(String srcImagePath, String toImagePath, int ratio) throws IOException {
- FileOutputStream out = null;
- try {
- //读入文件
- File file = new File(srcImagePath);
- // 构造Image对象
- BufferedImage src = javax.imageio.ImageIO.read(file);
- int width = src.getWidth();
- int height = src.getHeight();
- // 缩小边长
- BufferedImage tag = new BufferedImage(width / ratio, height / ratio, BufferedImage.TYPE_INT_RGB);
- // 绘制 缩小 后的图片
- tag.getGraphics().drawImage(src, 0, 0, width / ratio, height / ratio, null);
- out = new FileOutputStream(toImagePath);
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(tag);
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- if (out != null) {
- out.close();
- }
- }
- }
- /**
- * 对图片裁剪,并把裁剪新图片保存
- * @param srcPath 读取源图片路径
- * @param toPath 写入图片路径
- * @param x 剪切起始点x坐标
- * @param y 剪切起始点y坐标
- * @param width 剪切宽度
- * @param height 剪切高度
- * @param readImageFormat 读取图片格式
- * @param writeImageFormat 写入图片格式
- */
- public static void cropImage(String srcPath, String toPath, int x, int y, int width, int height, String readImageFormat, String writeImageFormat) {
- FileInputStream fis = null;
- ImageInputStream iis = null;
- try {
- //读取图片文件
- fis = new FileInputStream(srcPath);
- Iterator < ImageReader > readers = ImageIO.getImageReadersByFormatName(readImageFormat);
- ImageReader reader = readers.next();
- //获取图片流
- iis = ImageIO.createImageInputStream(fis);
- reader.setInput(iis, true);
- ImageReadParam param = reader.getDefaultReadParam();
- //定义一个矩形
- Rectangle rect = new Rectangle(x, y, width, height);
- //提供一个 BufferedImage,将其用作解码像素数据的目标。
- param.setSourceRegion(rect);
- BufferedImage bi = reader.read(0, param);
- //保存新图片
- ImageIO.write(bi, writeImageFormat, new File(toPath));
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (fis != null) {
- fis.close();
- }
- if (iis != null) {
- iis.close();
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
来源: http://www.bubuko.com/infodetail-2303738.html