对图片进行压缩,水印,伸缩变换,透明处理,格式转换操作

对图片进行压缩,水印,伸缩变换,透明处理,格式转换操作

 

  1 /**
  2  * <html>
  3  * <body>
  4  *  <P> Copyright 1994 JsonInternational</p>
  5  *  <p> All rights reserved.</p>
  6  *  <p> Created on 19941115</p>
  7  *  <p> Created by Jason</p>
  8  *  </body>
  9  * </html>
 10  */
 11 package cn.ucaner.alpaca.framework.utils.image;
 12 
 13 import java.awt.AlphaComposite;
 14 import java.awt.Color;
 15 import java.awt.Font;
 16 import java.awt.Graphics2D;
 17 import java.awt.Image;
 18 import java.awt.color.ColorSpace;
 19 import java.awt.image.BufferedImage;
 20 import java.awt.image.ColorConvertOp;
 21 import java.io.File;
 22 import java.io.FileInputStream;
 23 import java.io.IOException;
 24 
 25 import javax.imageio.ImageIO;
 26 
 27 /**
 28 * @Package:cn.ucaner.common.utils.image   
 29 * @ClassName:ImageUtil   
 30 * @Description:   <p> 对图片进行压缩,水印,伸缩变换,透明处理,格式转换操作 </p>
 31 * @Author: - Jason 
 32 * @CreatTime:2017年10月26日 上午10:52:22   
 33 * @Modify By:   
 34 * @ModifyTime:  
 35 * @Modify marker:   
 36 * @version    V1.0
 37  */
 38 public class ImageUtil {
 39 
 40     public static final float DEFAULT_QUALITY = 0.2125f ;
 41     
 42     /**
 43      * 添加图片水印操作(物理存盘,使用默认格式)
 44      * @param imgPath  待处理图片
 45      * @param markPath 水印图片
 46      * @param x 水印位于图片左上角的 x 坐标值
 47      * @param y 水印位于图片左上角的 y 坐标值
 48      * @param alpha  水印透明度 0.1f ~ 1.0f
 49      * @param destPath  文件存放路径  
 50      * @throws Exception          
 51      * 
 52      */
 53      public static void addWaterMark(String imgPath, String markPath, int x, int y, float alpha,String destPath) throws Exception{
 54          try {
 55                 BufferedImage bufferedImage = addWaterMark(imgPath, markPath, x, y, alpha);
 56                 ImageIO.write(bufferedImage, imageFormat(imgPath), new File(destPath));
 57             } catch (Exception e) {
 58                 throw new RuntimeException("添加图片水印异常");
 59             }
 60      }
 61     
 62         
 63     /**
 64      * 添加图片水印操作(物理存盘,自定义格式)
 65      * @param imgPath 待处理图片
 66      * @param markPath 水印图片
 67      * @param x 水印位于图片左上角的 x 坐标值
 68      * @param y 水印位于图片左上角的 y 坐标值
 69      * @param alpha 水印透明度 0.1f ~ 1.0f
 70      * @param format  添加水印后存储的格式
 71      * @param destPath  文件存放路径  
 72      * @throws Exception          
 73      * 
 74      */
 75      public static void addWaterMark(String imgPath, String markPath, int x, int y, float alpha,String format,String destPath) throws Exception{
 76          try {
 77                 BufferedImage bufferedImage = addWaterMark(imgPath, markPath, x, y, alpha);
 78                 ImageIO.write(bufferedImage,format , new File(destPath));
 79             } catch (Exception e) {
 80                 throw new RuntimeException("添加图片水印异常");
 81             }
 82      }
 83     
 84      
 85     /**
 86      * 添加图片水印操作,返回BufferedImage对象
 87      * @param imgPath 待处理图片
 88      * @param markPath 水印图片
 89      * @param x 水印位于图片左上角的 x 坐标值
 90      * @param y 水印位于图片左上角的 y 坐标值
 91      * @param alpha 水印透明度 0.1f ~ 1.0f
 92      * @return 处理后的图片对象
 93      * @throws Exception          
 94      * 
 95      */
 96     public static BufferedImage addWaterMark(String imgPath, String markPath, int x, int y, float alpha) throws Exception{
 97         BufferedImage targetImage = null;
 98         try {
 99                         // 加载待处理图片文件
100             Image img = ImageIO.read(new File(imgPath));
101 
102                         //创建目标图象文件
103             targetImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
104             Graphics2D g = targetImage.createGraphics();
105             g.drawImage(img, 0, 0, null);
106             
107                         // 加载水印图片文件
108             Image markImg = ImageIO.read(new File(markPath));
109             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
110             g.drawImage(markImg, x, y, null);
111             g.dispose();
112         } catch (Exception e) {
113             throw new RuntimeException("添加图片水印操作异常");
114         }
115         return targetImage;
116        
117     }
118 
119     /**
120      * 添加文字水印操作(物理存盘,使用默认格式)
121      * @param imgPath 待处理图片
122      * @param text 水印文字    
123      * @param font 水印字体信息    不写默认值为宋体
124      * @param color 水印字体颜色
125      * @param x 水印位于图片左上角的 x 坐标值
126      * @param y 水印位于图片左上角的 y 坐标值
127      * @param alpha 水印透明度 0.1f ~ 1.0f
128      * @param format 添加水印后存储的格式
129      * @param destPath  文件存放路径     
130      * @throws Exception          
131      */
132     public static void addTextMark(String imgPath, String text, Font font, Color color, float x, float y, float alpha,String destPath) throws Exception{
133         try {
134             BufferedImage bufferedImage = addTextMark(imgPath, text, font, color, x, y, alpha);
135             ImageIO.write(bufferedImage, imageFormat(imgPath), new File(destPath));
136         } catch (Exception e) {
137             throw new RuntimeException("图片添加文字水印异常");
138         }
139     }
140     
141     /**
142      * 添加文字水印操作(物理存盘,自定义格式)
143      * @param imgPath  待处理图片
144      * @param text 水印文字    
145      * @param font 水印字体信息    不写默认值为宋体
146      * @param color 水印字体颜色
147      * @param x 水印位于图片左上角的 x 坐标值
148      * @param y  水印位于图片左上角的 y 坐标值
149      * @param alpha 水印透明度 0.1f ~ 1.0f
150      * @param format 添加水印后存储的格式
151      * @param destPath 文件存放路径     
152      * @throws Exception          
153      */
154     public static void addTextMark(String imgPath, String text, Font font, Color color, float x, float y, float alpha,String format,String destPath) throws Exception{
155         try {
156             BufferedImage bufferedImage = addTextMark(imgPath, text, font, color, x, y, alpha);
157             ImageIO.write(bufferedImage, format, new File(destPath));
158         } catch (Exception e) {
159             throw new RuntimeException("图片添加文字水印异常");
160         }
161     }
162     
163     /**
164      * 添加文字水印操作,返回BufferedImage对象
165      * @param imgPath  待处理图片
166      * @param text 水印文字    
167      * @param font 水印字体信息    不写默认值为宋体
168      * @param color 水印字体颜色
169      * @param x  水印位于图片左上角的 x 坐标值
170      * @param y 水印位于图片左上角的 y 坐标值
171      * @param alpha 水印透明度 0.1f ~ 1.0f
172      * @return  处理后的图片对象
173      * @throws Exception          
174      */
175 
176     public static BufferedImage addTextMark(String imgPath, String text, Font font, Color color, float x, float y, float alpha) throws Exception{
177         BufferedImage targetImage = null;
178         try {
179             Font Dfont = (font == null) ? new Font("宋体", 20, 13) : font;    
180             Image img = ImageIO.read(new File(imgPath));
181                         //创建目标图像文件
182             targetImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
183             Graphics2D g = targetImage.createGraphics();
184             g.drawImage(img, 0, 0, null);
185             g.setColor(color);
186             g.setFont(Dfont);
187             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
188             g.drawString(text, x, y);
189             g.dispose();
190         } catch (Exception e) {
191             throw new RuntimeException("添加文字水印操作异常");
192         }
193         return targetImage;
194     }
195     
196     /**
197      * 压缩图片操作(文件物理存盘,使用默认格式)
198      * @param imgPath  待处理图片
199      * @param quality  图片质量(0-1之間的float值)
200      * @param width  输出图片的宽度    输入负数参数表示用原来图片宽
201      * @param height  输出图片的高度    输入负数参数表示用原来图片高
202      * @param autoSize  是否等比缩放 true表示进行等比缩放 false表示不进行等比缩放
203      * @param format 压缩后存储的格式
204      * @param destPath 文件存放路径
205      * 
206      * @throws Exception
207      */
208     public static void compressImage(String imgPath,float quality,int width, int height, boolean autoSize,String destPath)throws Exception{
209         try {
210             BufferedImage bufferedImage = compressImage(imgPath, quality, width, height, autoSize);
211             ImageIO.write(bufferedImage, imageFormat(imgPath), new File(destPath));
212         } catch (Exception e) {
213             throw new RuntimeException("图片压缩异常");
214         }
215         
216     }
217     
218     /**
219      * 压缩图片操作(文件物理存盘,可自定义格式)
220      * @param imgPath 待处理图片
221      * @param quality  图片质量(0-1之間的float值)
222      * @param width 输出图片的宽度    输入负数参数表示用原来图片宽
223      * @param height   输出图片的高度    输入负数参数表示用原来图片高
224      * @param autoSize  是否等比缩放 true表示进行等比缩放 false表示不进行等比缩放
225      * @param format   压缩后存储的格式
226      * @param destPath  文件存放路径
227      * 
228      * @throws Exception
229      */
230     public static void compressImage(String imgPath,float quality,int width, int height, boolean autoSize,String format,String destPath)throws Exception{
231         try {
232             BufferedImage bufferedImage = compressImage(imgPath, quality, width, height, autoSize);
233             ImageIO.write(bufferedImage, format, new File(destPath));
234         } catch (Exception e) {
235             throw new RuntimeException("图片压缩异常");
236         }
237     }
238     
239     
240     /**
241      * 压缩图片操作,返回BufferedImage对象
242      * @param imgPath 待处理图片
243      * @param quality 图片质量(0-1之間的float值)
244      * @param width  输出图片的宽度    输入负数参数表示用原来图片宽
245      * @param height 输出图片的高度    输入负数参数表示用原来图片高
246      * @param autoSize  是否等比缩放 true表示进行等比缩放 false表示不进行等比缩放
247      * @return 处理后的图片对象
248      * @throws Exception
249      */
250     public static BufferedImage compressImage(String imgPath,float quality,int width, int height, boolean autoSize)throws Exception{
251         BufferedImage targetImage = null;
252         if(quality<0F||quality>1F){
253             quality = DEFAULT_QUALITY;
254         }
255         try {
256             Image img = ImageIO.read(new File(imgPath));
257                         //如果用户输入的图片参数合法则按用户定义的复制,负值参数表示执行默认值
258             int newwidth =( width > 0 ) ? width : img.getWidth(null);
259                         //如果用户输入的图片参数合法则按用户定义的复制,负值参数表示执行默认值
260             int newheight = ( height > 0 )? height: img.getHeight(null);    
261                         //如果是自适应大小则进行比例缩放
262             if(autoSize){                                                    
263                         // 为等比缩放计算输出的图片宽度及高度
264                 double Widthrate = ((double) img.getWidth(null)) / (double) width + 0.1;
265                 double heightrate = ((double) img.getHeight(null))/ (double) height + 0.1;
266                 double rate = Widthrate > heightrate ? Widthrate : heightrate;
267                 newwidth = (int) (((double) img.getWidth(null)) / rate);
268                 newheight = (int) (((double) img.getHeight(null)) / rate);
269             }
270                         //创建目标图像文件
271             targetImage = new BufferedImage(newwidth,newheight,BufferedImage.TYPE_INT_RGB);
272             Graphics2D g = targetImage.createGraphics();
273             g.drawImage(img, 0, 0, newwidth, newheight, null);
274                         //如果添加水印或者文字则继续下面操作,不添加的话直接返回目标文件----------------------
275             g.dispose();
276             
277         } catch (Exception e) {
278             throw new RuntimeException("图片压缩操作异常");
279         }
280         return targetImage;
281     }
282     
283     /**
284      * 图片黑白化操作(文件物理存盘,使用默认格式)
285      * @param bufferedImage 处理的图片对象
286      * @param destPath 目标文件地址
287      * @throws Exception  
288      *
289      */
290     public static void imageGray(String imgPath, String destPath)throws Exception{
291         imageGray(imgPath, imageFormat(imgPath), destPath);
292     }
293     
294     
295     /**
296      * 图片黑白化操作(文件物理存盘,可自定义格式)
297      * @param bufferedImage 处理的图片对象
298      * @param format  图片格式
299      * @param destPath  目标文件地址
300      * @throws Exception 
301      * 
302      */
303     public static void imageGray(String imgPath,String format, String destPath)throws Exception{
304         try {
305              BufferedImage bufferedImage = ImageIO.read(new File(imgPath));
306              ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);            
307              ColorConvertOp op = new ColorConvertOp(cs, null);  
308              bufferedImage = op.filter(bufferedImage, null);
309              ImageIO.write(bufferedImage, format , new File(destPath));
310         } catch (Exception e) {
311             throw new RuntimeException("图片灰白化异常");
312         }
313     }
314     
315     
316     
317     /**
318      * 图片透明化操作(文件物理存盘,使用默认格式)
319      * @param imgPath  图片路径
320      * @param destPath  图片存放路径
321      * @throws Exception
322      */
323     public static void imageLucency(String imgPath,String destPath)throws Exception{
324         try {
325             BufferedImage bufferedImage = imageLucency(imgPath);
326             ImageIO.write(bufferedImage, imageFormat(imgPath), new File(destPath));
327         } catch (Exception e) {
328             throw new RuntimeException("图片透明化异常");
329         }
330     }
331     
332     
333     /**
334      * 图片透明化操作(文件物理存盘,可自定义格式)
335      * @param imgPath  图片路径
336      * @param format 图片格式
337      * @param destPath  图片存放路径
338      * @throws Exception
339      */
340     public static void imageLucency(String imgPath,String format,String destPath)throws Exception{
341         try {
342             BufferedImage bufferedImage = imageLucency(imgPath);
343             ImageIO.write(bufferedImage, format, new File(destPath));
344         } catch (Exception e) {
345             throw new RuntimeException("图片透明化异常");
346         }
347     }
348     
349     /**
350      * 图片透明化操作返回BufferedImage对象
351      * @param imgPath  图片路径
352      * @return 透明化后的图片对象
353      * @throws Exception 
354      */
355     public static BufferedImage imageLucency(String imgPath)throws Exception{
356         BufferedImage targetImage = null;
357         try {
358                         //读取图片   
359             BufferedImage img = ImageIO.read(new FileInputStream(imgPath));
360                         //透明度
361             int alpha = 0;    
362                         //执行透明化
363             executeRGB(img, alpha);
364             targetImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
365             Graphics2D g = targetImage.createGraphics();
366             g.drawImage(img, 0, 0, null);
367             g.dispose();
368         } catch (Exception e) {
369             throw new RuntimeException("图片透明化执行异常");
370         }
371         return targetImage;
372     }
373     
374     /**
375      * 执行透明化的核心算法
376      * @param img 图片对象
377      * @param alpha   透明度
378      * @throws Exception 
379      */
380     public static  void executeRGB(BufferedImage img, int alpha) throws Exception{
381         int rgb = 0;//RGB值
382                     //x表示BufferedImage的x坐标,y表示BufferedImage的y坐标
383         for(int x=img.getMinX();x<img.getWidth();x++){
384             for(int y=img.getMinY();y<img.getHeight();y++){
385                      //获取点位的RGB值进行比较重新设定
386                 rgb = img.getRGB(x, y); 
387                 int R =(rgb & 0xff0000 ) >> 16 ; 
388                 int G= (rgb & 0xff00 ) >> 8 ; 
389                 int B= (rgb & 0xff ); 
390                 if(((255-R)<30) && ((255-G)<30) && ((255-B)<30)){ 
391                     rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff); 
392                     img.setRGB(x, y, rgb);
393                 }
394             }
395         }
396     }
397     
398     
399     /**
400      * 图片格式转化操作(文件物理存盘)
401      * @param imgPath    原始图片存放地址
402      * @param format 待转换的格式 jpeg,gif,png,bmp等
403      * @param destPath  目标文件地址
404      * @throws Exception
405      */
406     public static void formatConvert(String imgPath, String format, String destPath)throws Exception{
407         try {
408             BufferedImage bufferedImage = ImageIO.read(new File(imgPath));
409             ImageIO.write(bufferedImage, format, new File(destPath));
410         } catch (IOException e) {
411             throw new RuntimeException("文件格式转换出错");
412         }
413     }
414     
415     /**
416      * 图片格式转化操作返回BufferedImage对象
417      * @param bufferedImage  BufferedImage图片转换对象
418      * @param format  待转换的格式 jpeg,gif,png,bmp等
419      * @param destPath   目标文件地址
420      * @throws Exception
421      */
422     public static void formatConvert(BufferedImage bufferedImag, String format, String destPath)throws Exception{
423         try {
424             ImageIO.write(bufferedImag, format, new File(destPath));
425         } catch (IOException e) {
426             throw new RuntimeException("文件格式转换出错");
427         }
428     }
429     
430     
431     /**
432      * 获取图片文件的真实格式信息
433      * @param imgPath   图片原文件存放地址
434      * @return 图片格式
435      * @throws Exception
436      */
437     public static String imageFormat(String imgPath)throws Exception{
438         String[] filess = imgPath.split("\\\\");
439         String[] formats = filess[filess.length - 1].split("\\.");
440         return formats[formats.length - 1];
441      }
442     
443     /**
444      * For Test By Jason
445      */
446     public static void main(String[] args) {
447         
448     }
449 
450 }

 

posted @ 2018-06-14 20:20  安以北往南  阅读(383)  评论(0编辑  收藏  举报