码迷,mamicode.com
首页 > 其他好文 > 详细

图片处理工具类

时间:2014-11-18 23:06:16      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   使用   sp   java   

  在实际项目中,我们经常会遇到处理各种各样的图片问题。 比如:图片的旋转、缩放、图片格式转换、获取图片类型、验证图片大小、写入图片 等。 这里我们使用java.awt.Graphics2D来实现常用图像处理的功能,形成我们的图像处理工具类。

  1 package com.zhangsx.util.image;
  2 import java.util.Iterator;
  3 import java.awt.Graphics2D;
  4 import java.awt.RenderingHints;
  5 import java.awt.image.BufferedImage;
  6 import java.io.IOException;
  7 import java.io.OutputStream;
  8 import java.io.ByteArrayInputStream;
  9 import java.io.ByteArrayOutputStream;
 10 import javax.imageio.ImageIO;
 11 import javax.imageio.ImageReader;
 12 import javax.imageio.stream.ImageInputStream;
 13 /**
 14  * 图像处理工具类。
 15  * 
 16  * @version 1.00 2010-1-15
 17  * @since 1.5
 18  * @author ZhangShixi
 19  */
 20 public class ImageUtil {
 21     /**
 22      * 旋转图像。
 23      * @param bufferedImage 图像。
 24      * @param degree 旋转角度。
 25      * @return 旋转后的图像。
 26      */
 27     public static BufferedImage rotateImage(
 28             final BufferedImage bufferedImage, final int degree) {
 29         int width = bufferedImage.getWidth();
 30         int height = bufferedImage.getHeight();
 31         int type = bufferedImage.getColorModel().getTransparency();
 32         BufferedImage image = new BufferedImage(width, height, type);
 33         Graphics2D graphics2D = image.createGraphics();
 34         graphics2D.setRenderingHint(
 35                 RenderingHints.KEY_INTERPOLATION,
 36                 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 37         graphics2D.rotate(Math.toRadians(degree), width / 2, height / 2);
 38         graphics2D.drawImage(bufferedImage, 0, 0, null);
 39         try {
 40             return image;
 41         } finally {
 42             if (graphics2D != null) {
 43                 graphics2D.dispose();
 44             }
 45         }
 46     }
 47     /**
 48      * 将图像按照指定的比例缩放。
 49      * 比如需要将图像放大20%,那么调用时scale参数的值就为20;如果是缩小,则scale值为-20。
 50      * @param bufferedImage 图像。
 51      * @param scale 缩放比例。
 52      * @return 缩放后的图像。
 53      */
 54     public static BufferedImage resizeImageScale(
 55             final BufferedImage bufferedImage, final int scale) {
 56         if (scale == 0) {
 57             return bufferedImage;
 58         }
 59         int width = bufferedImage.getWidth();
 60         int height = bufferedImage.getHeight();
 61         int newWidth = 0;
 62         int newHeight = 0;
 63         double nowScale = (double) Math.abs(scale) / 100;
 64         if (scale > 0) {
 65             newWidth = (int) (width * (1 + nowScale));
 66             newHeight = (int) (height * (1 + nowScale));
 67         } else if (scale < 0) {
 68             newWidth = (int) (width * (1 - nowScale));
 69             newHeight = (int) (height * (1 - nowScale));
 70         }
 71         return resizeImage(bufferedImage, newWidth, newHeight);
 72     }
 73     /**
 74      * 将图像缩放到指定的宽高大小。
 75      * @param bufferedImage 图像。
 76      * @param width 新的宽度。
 77      * @param height 新的高度。
 78      * @return 缩放后的图像。
 79      */
 80     public static BufferedImage resizeImage(
 81             final BufferedImage bufferedImage,
 82             final int width, final int height) {
 83         int type = bufferedImage.getColorModel().getTransparency();
 84         BufferedImage image = new BufferedImage(width, height, type);
 85         Graphics2D graphics2D = image.createGraphics();
 86         graphics2D.setRenderingHint(
 87                 RenderingHints.KEY_INTERPOLATION,
 88                 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 89         graphics2D.drawImage(bufferedImage, 0, 0, width, height, 0, 0,
 90                 bufferedImage.getWidth(), bufferedImage.getHeight(), null);
 91         try {
 92             return image;
 93         } finally {
 94             if (graphics2D != null) {
 95                 graphics2D.dispose();
 96             }
 97         }
 98     }
 99     /**
100      * 将图像水平翻转。
101      * @param bufferedImage 图像。
102      * @return 翻转后的图像。
103      */
104     public static BufferedImage flipImage(
105             final BufferedImage bufferedImage) {
106         int width = bufferedImage.getWidth();
107         int height = bufferedImage.getHeight();
108         int type = bufferedImage.getColorModel().getTransparency();
109         BufferedImage image = new BufferedImage(width, height, type);
110         Graphics2D graphics2D = image.createGraphics();
111         graphics2D.drawImage(bufferedImage, 0, 0, width, height,
112                 width, 0, 0, height, null);
113         try {
114             return image;
115         } finally {
116             if (graphics2D != null) {
117                 graphics2D.dispose();
118             }
119         }
120     }
121     /**
122      * 获取图片的类型。如果是 gif、jpg、png、bmp 以外的类型则返回null。
123      * @param imageBytes 图片字节数组。
124      * @return 图片类型。
125      * @throws java.io.IOException IO异常。
126      */
127     public static String getImageType(final byte[] imageBytes)
128             throws IOException {
129         ByteArrayInputStream input = new ByteArrayInputStream(imageBytes);
130         ImageInputStream imageInput = ImageIO.createImageInputStream(input);
131         Iterator<ImageReader> iterator = ImageIO.getImageReaders(imageInput);
132         String type = null;
133         if (iterator.hasNext()) {
134             ImageReader reader = iterator.next();
135             type = reader.getFormatName().toUpperCase();
136         }
137         try {
138             return type;
139         } finally {
140             if (imageInput != null) {
141                 imageInput.close();
142             }
143         }
144     }
145     /**
146      * 验证图片大小是否超出指定的尺寸。未超出指定大小返回true,超出指定大小则返回false。
147      * @param imageBytes 图片字节数组。
148      * @param width 图片宽度。
149      * @param height 图片高度。
150      * @return 验证结果。未超出指定大小返回true,超出指定大小则返回false。
151      * @throws java.io.IOException IO异常。
152      */
153     public static boolean checkImageSize(
154             final byte[] imageBytes, final int width, final int height)
155             throws IOException {
156         BufferedImage image = byteToImage(imageBytes);
157         int sourceWidth = image.getWidth();
158         int sourceHeight = image.getHeight();
159         if (sourceWidth > width || sourceHeight > height) {
160             return false;
161         } else {
162             return true;
163         }
164     }
165     /**
166      * 将图像字节数组转化为BufferedImage图像实例。
167      * @param imageBytes 图像字节数组。
168      * @return BufferedImage图像实例。
169      * @throws java.io.IOException IO异常。
170      */
171     public static BufferedImage byteToImage(
172             final byte[] imageBytes) throws IOException {
173         ByteArrayInputStream input = new ByteArrayInputStream(imageBytes);
174         BufferedImage image = ImageIO.read(input);
175         try {
176             return image;
177         } finally {
178             if (input != null) {
179                 input.close();
180             }
181         }
182     }
183     /**
184      * 将BufferedImage持有的图像转化为指定图像格式的字节数组。
185      * @param bufferedImage 图像。
186      * @param formatName 图像格式名称。
187      * @return 指定图像格式的字节数组。
188      * @throws java.io.IOException IO异常。
189      */
190     public static byte[] imageToByte(
191             final BufferedImage bufferedImage, final String formatName)
192             throws IOException {
193         ByteArrayOutputStream output = new ByteArrayOutputStream();
194         ImageIO.write(bufferedImage, formatName, output);
195         try {
196             return output.toByteArray();
197         } finally {
198             if (output != null) {
199                 output.close();
200             }
201         }
202     }
203     /**
204      * 将图像以指定的格式进行输出,调用者需自行关闭输出流。
205      * @param bufferedImage 图像。
206      * @param output 输出流。
207      * @param formatName 图片格式名称。
208      * @throws java.io.IOException IO异常。
209      */
210     public static void writeImage(final BufferedImage bufferedImage,
211             final OutputStream output, final String formatName)
212             throws IOException {
213         ImageIO.write(bufferedImage, formatName, output);
214     }
215     /**
216      * 将图像以指定的格式进行输出,调用者需自行关闭输出流。
217      * @param imageBytes 图像的byte数组。
218      * @param output 输出流。
219      * @param formatName 图片格式名称。
220      * @throws java.io.IOException IO异常。
221      */
222     public static void writeImage(final byte[] imageBytes,
223             final OutputStream output, final String formatName)
224             throws IOException {
225         BufferedImage image = byteToImage(imageBytes);
226         ImageIO.write(image, formatName, output);
227     }
228 }

 

图片处理工具类

标签:style   blog   io   color   ar   os   使用   sp   java   

原文地址:http://www.cnblogs.com/libaoting/p/4106398.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!