Administrator
2023-04-14 cc0cbfc79a34e1b106fdb998450cd2ad03446126
提交 | 用户 | 时间
58d006 1 package com.mandi.common;
A 2
3 import java.awt.AlphaComposite;
4 import java.awt.Color;
5 import java.awt.Font;
6 import java.awt.Graphics;
7 import java.awt.Graphics2D;
8 import java.awt.Image;
9 import java.awt.RenderingHints;
10 import java.awt.Toolkit;
11 import java.awt.color.ColorSpace;
12 import java.awt.image.BufferedImage;
13 import java.awt.image.ColorConvertOp;
14 import java.awt.image.CropImageFilter;
15 import java.awt.image.FilteredImageSource;
16 import java.awt.image.ImageFilter;
17 import java.awt.image.ImagingOpException;
18 import java.io.File;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21
22 import javax.imageio.ImageIO;
23
24 /** 
25  * @author mengly 
26  * @version 创建时间:2016年4月21日 下午6:01:01 
27  * 类说明 
28  */
29
30 public class ImageUtils {
31       /**
32      * 相对于图片的位置
33      */
34     private static final int POSITION_UPPERLEFT=0;
35     private static final int POSITION_UPPERRIGHT=10;
36     private static final int POSITION_LOWERLEFT=1;
37     private static final int POSITION_LOWERRIGHT=11;
38     /**
39      * 几种常见的图片格式
40      */
41     public static String IMAGE_TYPE_GIF = "gif";// 图形交换格式
42     public static String IMAGE_TYPE_JPG = "jpg";// 联合照片专家组
43     public static String IMAGE_TYPE_JPEG = "jpeg";// 联合照片专家组
44     public static String IMAGE_TYPE_BMP = "bmp";// 英文Bitmap(位图)的简写,它是Windows操作系统中的标准图像文件格式
45     public static String IMAGE_TYPE_PNG = "png";// 可移植网络图形
46     private static ImageUtils instance;
47     private ImageUtils() {
48         instance = this;
49     }
50     /**
51      * 获取实例
52      * @return
53      */
54     public static ImageUtils getInstance() {
55         if (instance == null) {
56             instance = new ImageUtils();
57         }
58         return instance;
59     }
60     public  BufferedImage image2BufferedImage(Image image){
61         System.out.println(image.getWidth(null));
62         System.out.println(image.getHeight(null));
63         BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
64         Graphics2D g = bufferedImage.createGraphics();
65         g.drawImage(image, null, null);
66         g.dispose();
67         return bufferedImage;
68     }
69     /**
70      * 缩放并转换格式后保存
71      * @param srcPath源路径
72      * @param destPath目标路径
73      * @param width:目标宽
74      * @param height:目标高
75      * @param format:文件格式
76      * @return
77      */
78     public static boolean scaleToFile(String srcPath, String destPath, int width,  int height,String format) {
79         boolean flag = false;
80         try {
81             File file = new File(srcPath);
82             File destFile = new File(destPath);
83             if (!destFile.getParentFile().exists()) {
84                 destFile.getParentFile().mkdir();
85             }
86             BufferedImage src = ImageIO.read(file); // 读入文件
87             Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
88             BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
89             Graphics g = tag.getGraphics();
90             g.drawImage(image, 0, 0, null); // 绘制缩小后的图
91             g.dispose();
92             flag = ImageIO.write(tag, format, new FileOutputStream(destFile));// 输出到文件流
93         } catch (IOException e) {
94             e.printStackTrace();
95         }
96         return flag;
97     }
98     /**
99      * 缩放Image,此方法返回源图像按百分比缩放后的图像
100      * @param inputImage
101      * @param percentage 百分比 允许的输入0<percentage<10000
102      * @return
103      */
104     public static BufferedImage scaleByPercentage(BufferedImage inputImage,int percentage){
105         //允许百分比
106         if(0>percentage||percentage>10000){
107             throw new ImagingOpException("Error::不合法的参数:percentage->"+percentage+",percentage应该大于0~小于10000");
108         }       
109         //获取原始图像透明度类型
110         int type = inputImage.getColorModel().getTransparency();
111         //获取目标图像大小
112         int w=inputImage.getWidth()*percentage;
113         int h=inputImage.getHeight()*percentage; 
114         //开启抗锯齿
115         RenderingHints renderingHints=new RenderingHints(RenderingHints.KEY_INTERPOLATION,
116                 RenderingHints.VALUE_ANTIALIAS_ON);
117         //使用高质量压缩
118         renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_RENDER_QUALITY);
119         BufferedImage img = new BufferedImage(w, h, type);
120         Graphics2D graphics2d =img.createGraphics();
121         graphics2d.setRenderingHints(renderingHints);        
122         graphics2d.drawImage(inputImage, 0, 0, w, h, 0, 0, inputImage
123                 .getWidth(), inputImage.getHeight(), null);
124         graphics2d.dispose();
125         return img;
126         /*此代码将返回Image类型
127         return inputImage.getScaledInstance(inputImage.getWidth()*percentage,
128                 inputImage.getHeight()*percentage, Image.SCALE_SMOOTH);
129         */
130     }
131     /**
132      * 缩放Image,此方法返回源图像按给定最大宽度限制下按比例缩放后的图像
133      * @param inputImage
134      * @param maxWidth:压缩后允许的最大宽度
135      * @param maxHeight:压缩后允许的最大高度
136      * @throws java.io.IOException
137      * return 
138      */
139     public static BufferedImage scaleByPixelRate(BufferedImage inputImage, int maxWidth, int maxHeight) throws Exception {
140         //获取原始图像透明度类型
141         int type = inputImage.getColorModel().getTransparency();
142         int width = inputImage.getWidth();
143         int height = inputImage.getHeight();
144         int newWidth = maxWidth;
145         int newHeight =maxHeight;
146         //如果指定最大宽度超过比例
147         if(width*maxHeight<height*maxWidth){
148             newWidth=(int)(newHeight*width/height) ;
149         }
150         //如果指定最大高度超过比例
151         if(width*maxHeight>height*maxWidth){
152             newHeight=(int)(newWidth*height/width);
153         }
154         //开启抗锯齿
155         RenderingHints renderingHints=new RenderingHints(RenderingHints.KEY_INTERPOLATION,
156                 RenderingHints.VALUE_ANTIALIAS_ON);
157         //使用高质量压缩
158         renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_RENDER_QUALITY);
159         BufferedImage img = new BufferedImage(newWidth, newHeight, type);
160         Graphics2D graphics2d =img.createGraphics();
161         graphics2d.setRenderingHints(renderingHints);        
162         graphics2d.drawImage(inputImage, 0, 0, newWidth, newHeight, 0, 0, width, height, null);
163         graphics2d.dispose();
164         return img;
165     }
166     /**
167      * 缩放Image,此方法返回源图像按给定宽度、高度限制下缩放后的图像
168      * @param inputImage
169      * @param maxWidth:压缩后宽度
170      * @param maxHeight:压缩后高度
171      * @throws java.io.IOException
172      * return 
173      */
174     public static BufferedImage scaleByPixel(BufferedImage inputImage, int newWidth, int newHeight) throws Exception {
175         //获取原始图像透明度类型
176         int type = inputImage.getColorModel().getTransparency();
177         int width = inputImage.getWidth();
178         int height = inputImage.getHeight();
179         //开启抗锯齿
180         RenderingHints renderingHints=new RenderingHints(RenderingHints.KEY_ANTIALIASING,
181                 RenderingHints.VALUE_ANTIALIAS_ON);
182         //使用高质量压缩
183         renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
184         BufferedImage img = new BufferedImage(newWidth, newHeight, type);
185         Graphics2D graphics2d =img.createGraphics();
186         graphics2d.setRenderingHints(renderingHints);        
187         graphics2d.drawImage(inputImage, 0, 0, newWidth, newHeight, 0, 0, width, height, null);
188         graphics2d.dispose();
189         return img;
190     }
191     /**
192      * 切割图像,返回指定范围的图像
193      * @param inputImage
194      * @param x 起点横坐标
195      * @param y 起点纵坐标
196      * @param width 切割图片宽度:如果宽度超出图片,将改为图片自x剩余宽度
197      * @param height 切割图片高度:如果高度超出图片,将改为图片自y剩余高度
198      * @param fill 指定目标图像大小超出时是否补白,如果true,则表示补白;false表示不补白,此时将重置目标图像大小
199      * @return
200      */
201     public static BufferedImage cut(BufferedImage inputImage,int x,int y,int width,int height,boolean fill){
202         //获取原始图像透明度类型
203         int type = inputImage.getColorModel().getTransparency();
204         int w = inputImage.getWidth();
205         int h = inputImage.getHeight();
206         int endx=x+width;
207         int endy=y+height;
208         if(x>w)
209             throw new ImagingOpException("起点横坐标超出源图像范围");
210         if(y>h)
211             throw new ImagingOpException("起点纵坐标超出源图像范围");
212         BufferedImage img;
213         //补白
214         if(fill){
215             img = new BufferedImage(width, height, type);
216             //宽度超出限制
217             if((w-x)<width){
218                 width=w-x;
219                 endx=w;
220             }
221             //高度超出限制
222             if((h-y)<height){
223                 height=h-y;
224                 endy=h;
225             }
226         //不补
227         }else{
228             //宽度超出限制
229             if((w-x)<width){
230                 width=w-x;
231                 endx=w;
232             }
233             //高度超出限制
234             if((h-y)<height){
235                 height=h-y;
236                 endy=h;
237             }
238             img = new BufferedImage(width, height, type);  
239         }
240         //开启抗锯齿
241         RenderingHints renderingHints=new RenderingHints(RenderingHints.KEY_INTERPOLATION,
242                 RenderingHints.VALUE_ANTIALIAS_ON);
243         //使用高质量压缩
244         renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_RENDER_QUALITY);
245         Graphics2D graphics2d =img.createGraphics();
246         graphics2d.setRenderingHints(renderingHints);        
247         graphics2d.drawImage(inputImage, 0, 0, width, height, x, y, endx, endy, null);
248         graphics2d.dispose();
249         return img;
250     }
251     /**
252      * 切割图像,返回指定起点位置指定大小图像
253      * @param inputImage
254      * @param startPoint 起点位置:左上:0、右上:10、左下:1、右下:11
255      * @param width 切割图片宽度
256      * @param height 切割图片高度
257      * @param fill 指定目标图像大小超出时是否补白,如果true,则表示补白;false表示不补白,此时将重置目标图像大小
258      * @return
259      */
260     public static BufferedImage cut(BufferedImage inputImage,int startPoint,int width,int height,boolean fill){
261         //获取原始图像透明度类型
262         int type = inputImage.getColorModel().getTransparency();
263         int w = inputImage.getWidth();
264         int h = inputImage.getHeight();
265         BufferedImage img;
266         //补白
267         if(fill){
268             img = new BufferedImage(width, height, type);
269             if(width>w)
270                 width=w;
271             if(height>h)
272                 height=h;
273         //不补
274         }else{
275             if(width>w)
276                 width=w;
277             if(height>h)
278                 height=h;
279             img = new BufferedImage(width, height, type);
280         }
281         //开启抗锯齿
282         RenderingHints renderingHints=new RenderingHints(RenderingHints.KEY_INTERPOLATION,
283                 RenderingHints.VALUE_ANTIALIAS_ON);
284         //使用高质量压缩
285         renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_RENDER_QUALITY);       
286         Graphics2D graphics2d =img.createGraphics();
287         graphics2d.setRenderingHints(renderingHints); 
288         switch(startPoint){
289                 //右上
290             case POSITION_UPPERRIGHT:
291                 graphics2d.drawImage(inputImage, w-width, 0, w, height, 0, 0, width, height, null);
292                 break;
293                 //左下
294             case POSITION_LOWERLEFT:
295                 graphics2d.drawImage(inputImage, 0, h-height, width, h, 0, 0, width, height, null);   
296                 break;
297                 //右下
298             case POSITION_LOWERRIGHT:
299                 graphics2d.drawImage(inputImage, w-width, h-height, w, h, 0, 0, width, height, null); 
300                 break;
301                 //默认左上
302             case POSITION_UPPERLEFT:
303             default: 
304                 graphics2d.drawImage(inputImage, 0, 0, width, height, 0, 0, width, height, null);
305         }
306         graphics2d.dispose();
307         return img;
308     }
309     /**
310      * 以指定角度旋转图片:使用正角度 theta 进行旋转,可将正 x 轴上的点转向正 y 轴。 
311      * @param inputImage
312      * @param degree 角度:以度数为单位
313      * @return
314      */
315     public static BufferedImage rotateImage(final BufferedImage inputImage,
316             final int degree) {
317         int w = inputImage.getWidth();
318         int h = inputImage.getHeight();
319         int type = inputImage.getColorModel().getTransparency();
320         BufferedImage img=new BufferedImage(w, h, type);
321         Graphics2D graphics2d =img.createGraphics();
322         //开启抗锯齿
323         RenderingHints renderingHints=new RenderingHints(RenderingHints.KEY_INTERPOLATION,
324                 RenderingHints.VALUE_ANTIALIAS_ON);
325         //使用高质量压缩
326         renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_RENDER_QUALITY);
327         graphics2d.setRenderingHints(renderingHints);
328         graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
329         graphics2d.drawImage(inputImage, 0, 0, null);
330         graphics2d.dispose();
331         return img;
332     }
333     /**
334      * 水平翻转图像
335      * 
336      * @param bufferedimage 目标图像
337      * @return
338      */
339     public static BufferedImage flipHorizontalImage(final BufferedImage inputImage) {
340         int w = inputImage.getWidth();
341         int h = inputImage.getHeight();
342         BufferedImage img;
343         Graphics2D graphics2d;
344         (graphics2d = (img = new BufferedImage(w, h, inputImage
345                 .getColorModel().getTransparency())).createGraphics())
346                 .drawImage(inputImage, 0, 0, w, h, w, 0, 0, h, null);
347         graphics2d.dispose();
348         return img;
349     }
350     /**
351      * 竖直翻转图像
352      * 
353      * @param bufferedimage 目标图像
354      * @return
355      */
356     public static BufferedImage flipVerticalImage(final BufferedImage inputImage) {
357         int w = inputImage.getWidth();
358         int h = inputImage.getHeight();
359         BufferedImage img;
360         Graphics2D graphics2d;
361         (graphics2d = (img = new BufferedImage(w, h, inputImage
362                 .getColorModel().getTransparency())).createGraphics())
363                 .drawImage(inputImage, 0, 0, w, h, 0, h, w, 0, null);
364         graphics2d.dispose();
365         return img;
366     }   
367     /**
368      * 图片水印
369      * 
370      * @param inputImage
371      *            待处理图像
372      * @param markImage
373      *            水印图像
374      * @param x
375      *            水印位于图片左上角的 x 坐标值
376      * @param y
377      *            水印位于图片左上角的 y 坐标值
378      * @param alpha
379      *            水印透明度 0.1f ~ 1.0f
380      * */
381     public static BufferedImage waterMark(BufferedImage inputImage,BufferedImage markImage, int x, int y,
382                     float alpha) {
383             BufferedImage image = new BufferedImage(inputImage.getWidth(), inputImage
384                             .getHeight(), BufferedImage.TYPE_INT_ARGB);
385             Graphics2D g = image.createGraphics();
386             g.drawImage(inputImage, 0, 0, null);
387             // 加载水印图像
388             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
389                             alpha));
390             g.drawImage(markImage, x, y, null);
391             g.dispose();
392             return image;
393     }
394     /**
395      * 文字水印
396      * 
397      * @param inputImage
398      *            待处理图像
399      * @param text
400      *            水印文字
401      * @param font
402      *            水印字体信息
403      * @param color
404      *            水印字体颜色
405      * @param x
406      *            水印位于图片左上角的 x 坐标值
407      * @param y
408      *            水印位于图片左上角的 y 坐标值
409      * @param alpha
410      *            水印透明度 0.1f ~ 1.0f
411      */
412     public static BufferedImage textMark(BufferedImage inputImage, String text, Font font,
413                     Color color, int x, int y, float alpha) {
414             Font dfont = (font == null) ? new Font("宋体", 20, 13) : font;
415             BufferedImage image = new BufferedImage(inputImage.getWidth(), inputImage
416                             .getHeight(), BufferedImage.TYPE_INT_ARGB);
417             Graphics2D g = image.createGraphics();
418             g.drawImage(inputImage, 0, 0, null);
419             g.setColor(color);
420             g.setFont(dfont);
421             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
422                             alpha));
423             g.drawString(text, x, y);
424             g.dispose();
425             return image;
426     }  
427     /**
428      * 图像彩色转黑白色
429      * @param inputImage
430      * @return 转换后的BufferedImage
431      */
432     public final static BufferedImage toGray(BufferedImage inputImage)
433     {
434             ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
435             //对源 BufferedImage 进行颜色转换。如果目标图像为 null,
436             //则根据适当的 ColorModel 创建 BufferedImage。 
437             ColorConvertOp op = new ColorConvertOp(cs, null);
438             return op.filter(inputImage, null);
439     }
440     /**
441      * 图像彩色转为黑白
442      * @param srcImageFile
443      *            源图像地址
444      * @param destImageFile
445      *            目标图像地址
446      * @param formatType
447      *              目标图像格式:如果formatType为null;将默认转换为PNG
448      */
449     public final static void toGray(String srcImageFile, String destImageFile,String formatType)
450     {
451         try
452         {
453             BufferedImage src = ImageIO.read(new File(srcImageFile));
454             ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
455             ColorConvertOp op = new ColorConvertOp(cs, null);
456             src = op.filter(src, null);
457             //如果formatType为null;将默认转换为PNG
458             if(formatType==null){
459                 formatType="PNG";
460             }
461             ImageIO.write(src,formatType,new File(destImageFile));
462         } catch (IOException e)
463         {
464             e.printStackTrace();
465         }
466     }
467     /**
468      * 图像类型转换:GIF->JPG、GIF->PNG、PNG->JPG、PNG->GIF(X)、BMP->PNG
469      * 
470      * @param inputImage
471      *            源图像地址
472      * @param formatType
473      *            包含格式非正式名称的 String:如JPG、JPEG、GIF等
474      * @param destImageFile
475      *            目标图像地址
476      */
477     public final static void convert(BufferedImage inputImage, String formatType,String destImageFile)
478     {
479         try
480         {
481             ImageIO.write(inputImage, formatType, new File(destImageFile));
482         } catch (Exception e)
483         {
484             e.printStackTrace();
485         }
486     }
487     /**
488      * 图像切割(指定切片的行数和列数)
489      * 
490      * @param srcImageFile
491      *            源图像地址
492      * @param destDir
493      *            切片目标文件夹
494      * @param formatType
495      *              目标格式
496      * @param rows
497      *            目标切片行数。默认2,必须是范围 [1, 20] 之内
498      * @param cols
499      *            目标切片列数。默认2,必须是范围 [1, 20] 之内
500      */
501     public final static void cut(BufferedImage inputImage, String destDir,
502             String formatType,int rows, int cols)
503     {
504         try
505         {
506             if (rows <= 0 || rows > 20)
507                 rows = 2; // 切片行数
508             if (cols <= 0 || cols > 20)
509                 cols = 2; // 切片列数
510             // 读取源图像
511             //BufferedImage bi = ImageIO.read(new File(srcImageFile));
512             int w = inputImage.getHeight(); // 源图宽度
513             int h = inputImage.getWidth(); // 源图高度
514             if (w > 0 && h > 0)
515             {
516                 Image img;
517                 ImageFilter cropFilter;
518                 Image image = inputImage.getScaledInstance(w, h,
519                         Image.SCALE_DEFAULT);
520                 int destWidth = w; // 每张切片的宽度
521                 int destHeight = h; // 每张切片的高度
522                 // 计算切片的宽度和高度
523                 if (w % cols == 0)
524                 {
525                     destWidth = w / cols;
526                 } else
527                 {
528                     destWidth = (int) Math.floor(w / cols) + 1;
529                 }
530                 if (h % rows == 0)
531                 {
532                     destHeight = h / rows;
533                 } else
534                 {
535                     destHeight = (int) Math.floor(h / rows) + 1;
536                 }
537                 // 循环建立切片
538                 // 改进的想法:是否可用多线程加快切割速度
539                 for (int i = 0; i < rows; i++)
540                 {
541                     for (int j = 0; j < cols; j++)
542                     {
543                         // 四个参数分别为图像起点坐标和宽高
544                         // 即: CropImageFilter(int x,int y,int width,int height)
545                         cropFilter = new CropImageFilter(j * destWidth, i
546                                 * destHeight, destWidth, destHeight);
547                         img = Toolkit.getDefaultToolkit().createImage(
548                                 new FilteredImageSource(image.getSource(),
549                                         cropFilter));
550                         BufferedImage tag = new BufferedImage(destWidth,
551                                 destHeight, BufferedImage.TYPE_INT_ARGB);
552                         Graphics g = tag.getGraphics();
553                         g.drawImage(img, 0, 0, null); // 绘制缩小后的图
554                         g.dispose();
555                         // 输出为文件
556                         ImageIO.write(tag, formatType, new File(destDir + "_r" + i
557                                 + "_c" + j + "."+formatType.toLowerCase()));
558                     }
559                 }
560             }
561         } catch (Exception e)
562         {
563             e.printStackTrace();
564         }
565     }
566     /**
567      * 给图片添加文字水印
568      * 
569      * @param pressText
570      *            水印文字
571      * @param srcImageFile
572      *            源图像地址
573      * @param destImageFile
574      *            目标图像地址
575      * @param fontName
576      *            水印的字体名称
577      * @param fontStyle
578      *            水印的字体样式
579      * @param color
580      *            水印的字体颜色
581      * @param fontSize
582      *            水印的字体大小
583      * @param x
584      *            修正值
585      * @param y
586      *            修正值
587      * @param alpha
588      *            透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
589      * @param formatType
590      *              目标格式
591      */
592     public final static void pressText(String pressText, String srcImageFile,
593             String destImageFile, String fontName, int fontStyle, Color color,
594             int fontSize, int x, int y, float alpha,String formatType)
595     {
596         try
597         {
598             File img = new File(srcImageFile);
599             Image src = ImageIO.read(img);
600             int width = src.getWidth(null);
601             int height = src.getHeight(null);
602             BufferedImage image = new BufferedImage(width, height,
603                     BufferedImage.TYPE_INT_ARGB);
604             Graphics2D g = image.createGraphics();
605             g.drawImage(src, 0, 0, width, height, null);
606             g.setColor(color);
607             g.setFont(new Font(fontName, fontStyle, fontSize));
608             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
609                     alpha));
610             // 在指定坐标绘制水印文字
611             g.drawString(pressText, (width - (getLength(pressText) * fontSize))
612                     / 2 + x, (height - fontSize) / 2 + y);
613             g.dispose();
614             ImageIO.write((BufferedImage) image, formatType,
615                     new File(destImageFile));// 输出到文件流
616         } catch (Exception e)
617         {
618             e.printStackTrace();
619         }
620     }
621     /**
622      * 给图片添加图片水印
623      * 
624      * @param pressImg
625      *            水印图片
626      * @param srcImageFile
627      *            源图像地址
628      * @param destImageFile
629      *            目标图像地址
630      * @param x
631      *            修正值。 默认在中间
632      * @param y
633      *            修正值。 默认在中间
634      * @param alpha
635      *            透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
636      * @param formatType
637      *              目标格式
638      */ 
639     public final static void pressImage(String pressImg, String srcImageFile,
640             String destImageFile, int x, int y, float alpha,String formatType)
641     {
642         try
643         {
644             File img = new File(srcImageFile);
645             Image src = ImageIO.read(img);
646             int wideth = src.getWidth(null);
647             int height = src.getHeight(null);
648             BufferedImage image = new BufferedImage(wideth, height,
649                     BufferedImage.TYPE_INT_ARGB);
650             Graphics2D g = image.createGraphics();
651             g.drawImage(src, 0, 0, wideth, height, null);
652             // 水印文件
653             Image src_biao = ImageIO.read(new File(pressImg));
654             int wideth_biao = src_biao.getWidth(null);
655             int height_biao = src_biao.getHeight(null);
656             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
657                     alpha));
658             g.drawImage(src_biao, (wideth - wideth_biao) / 2,
659                     (height - height_biao) / 2, wideth_biao, height_biao, null);
660             // 水印文件结束
661             g.dispose();
662             ImageIO.write((BufferedImage) image, formatType,
663                     new File(destImageFile));
664         } catch (Exception e)
665         {
666             e.printStackTrace();
667         }
668     }
669     /**
670      * 计算text的长度(一个中文算两个字符)
671      * 
672      * @param text
673      * @return
674      */
675     public final static int getLength(String text)
676     {
677         int length = 0;
678         for (int i = 0; i < text.length(); i++)
679         {
680             if (new String(text.charAt(i) + "").getBytes().length > 1)
681             {
682                 length += 2;
683             } else
684             {
685                 length += 1;
686             }
687         }
688         return length / 2;
689     }
690 }