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

处理图片的工具类

时间:2017-09-08 18:39:33      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:file   cal   idt   ext   fine   eth   utf-8   png   pos   

  1 package com.ideal.common.util; 
  2 import java.awt.AlphaComposite;
  3 import java.awt.Color;
  4 import java.awt.Font;
  5 import java.awt.Graphics2D;
  6 import java.awt.Image;
  7 import java.awt.geom.AffineTransform;
  8 import java.awt.image.AffineTransformOp;
  9 import java.awt.image.BufferedImage;
 10 import java.io.ByteArrayInputStream;
 11 import java.io.ByteArrayOutputStream;
 12 import java.io.File;
 13 import java.io.FileOutputStream;
 14 import java.io.IOException;
 15 import java.io.InputStream;
 16 
 17 import javax.imageio.ImageIO;
 18 
 19 import net.glxn.qrgen.core.AbstractQRCode;
 20 import net.glxn.qrgen.core.image.ImageType;
 21 import net.glxn.qrgen.javase.QRCode;
 22 
 23 /**
 24  *
 25  */
 26 public final class ImageUtils {
 27     /**
 28      * 图片上加图片
 29      * @param pressImg 生成二维码的url
 30      * @param targetImg 
 31      * @param x 
 32      * @param y 
 33      * @param alpha
 34      */
 35     public final static void pressImage(String pressImg, String srcImg, String targetImg, int x, int y, float alpha) {
 36         try {
 37             File target = new File(targetImg);
 38             File img = new File(srcImg);
 39             Image src = ImageIO.read(img);
 40             int wideth = src.getWidth(null);
 41             int height = src.getHeight(null);
 42             BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
 43             Graphics2D g = image.createGraphics();
 44             g.drawImage(src, 0, 0, wideth, height, null);
 45             //
 46             File qrCode = QRCode.from(pressImg).withCharset("UTF-8").to(ImageType.PNG).withSize(182, 182).file();
 47             Image src_biao = ImageIO.read(qrCode);
 48             //Image src_biao = ImageIO.read(new File(pressImg));
 49             int wideth_biao = src_biao.getWidth(null);
 50             int height_biao = src_biao.getHeight(null);
 51             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
 52             //g.drawImage(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wideth_biao, height_biao, null);
 53             g.drawImage(src_biao, 126, 823, wideth_biao, height_biao, null);
 54             //
 55             g.dispose();
 56             ImageIO.write((BufferedImage) image, "png", target);
 57         } catch (Exception e) {
 58             e.printStackTrace();
 59         }
 60     }
 61 
 62     /**
 63      * 添加文字
 64      * @param pressText 
 65      * @param targetImg 目标图片
 66      * @param fontName
 67      * @param fontStyle 
 68      * @param color 
 69      * @param fontSize 
 70      * @param x
 71      * @param y 
 72      * @param alpha
 73      */
 74     public static File pressText(String data, String srcImg, String fontName, int fontStyle, Color color, int fontSize, int x, int y, float alpha) {
 75         try {
 76              
 77             
 78             //Cpp cp = new Cpp(srcImg,targetImg);
 79             File img = new File(srcImg);
 80             Image src = ImageIO.read(img);
 81             int width = src.getWidth(null);
 82             int height = src.getHeight(null);
 83             BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
 84             Graphics2D g = image.createGraphics();
 85             g.drawImage(src, 0, 0, width, height, null);
 86             g.setColor(color);
 87             Font font = new Font(fontName, fontStyle, fontSize);
 88             g.setFont(font);
 89             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
 90             // 计算字符并且换行处理
 91             // 
 92             /*if (data.length()< 32) {
 93                 g.drawString(data, (width - (getLength(data) * fontSize)) / 2+ x, (height - fontSize)  + y);
 94                 
 95             }else{
 96                 for (int i = 0; i < data.length(); i++) {
 97                     
 98                 }
 99                 
100             }*/
101             //g.drawString(data, -410, 25);
102             g.drawString(data, (width - (getLength(data) * fontSize)) / 2+ x, 100);
103             g.dispose();
104             
105             //FileOutputStream outImgStream = new FileOutputStream(targetImg); 
106             // 输出图片
107              ImageIO.write((BufferedImage) image, "png", img);
108             // 转成输出流
109 //            ByteArrayOutputStream os = new ByteArrayOutputStream();  
110 //            ImageIO.write(image, "png", os);  
111 //            InputStream is = new ByteArrayInputStream(os.toByteArray());
112             
113             
114             //ImageIO.read(img); // 写入文件
115             return img;
116         } catch (Exception e) {
117             e.printStackTrace();
118             return null;
119         }
120     }
121 
122     /**
123      * 重置图片的大小
124      * @param filePath 
125      * @param height 
126      * @param width 
127      * @param bb
128      */
129     public static void resize(String filePath, int height, int width, boolean bb) {
130         try {
131             double ratio = 0.0; //
132             File f = new File(filePath);
133             BufferedImage bi = ImageIO.read(f);
134             Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
135             //
136             if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
137                 if (bi.getHeight() > bi.getWidth()) {
138                     ratio = (new Integer(height)).doubleValue() / bi.getHeight();
139                 } else {
140                     ratio = (new Integer(width)).doubleValue() / bi.getWidth();
141                 }
142                 AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
143                 itemp = op.filter(bi, null);
144             }
145             if (bb) {
146                 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
147                 Graphics2D g = image.createGraphics();
148                 g.setColor(Color.white);
149                 g.fillRect(0, 0, width, height);
150                 if (width == itemp.getWidth(null))
151                     g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
152                 else
153                     g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
154                 g.dispose();
155                 itemp = image;
156             }
157             ImageIO.write((BufferedImage) itemp, "png", f);
158         } catch (IOException e) {
159             e.printStackTrace();
160         }
161     }
162 
163     
164 
165     public static int getLength(String text) {
166         int length = 0;
167         for (int i = 0; i < text.length(); i++) {
168             if (new String(text.charAt(i) + "").getBytes().length > 1) {
169                 length += 2;
170             } else {
171                 length += 1;
172             }
173         }
174         return length / 2;
175     }
176     
177     /**
178      *  画空白图片
179      * @throws IOException 
180      */
181     public static void drawVerifyCode() throws IOException{
182         File file = new File("E:\\test\\1.png");
183         int width = 400;
184         int height = 400;
185         // 创建一个画布
186         BufferedImage bi = new BufferedImage(width, height,
187                 BufferedImage.TYPE_INT_RGB);
188         // 获取一个画笔
189         Graphics2D g = bi.createGraphics();
190         // 画图过程
191         // 设置画笔的颜色为白色
192         g.setColor(Color.WHITE);
193         g.fillRect(0, 0, width, height);
194         // 绘制灰色边框
195         g.setColor(Color.GRAY);
196         g.drawRect(0, 0, width - 1, height - 1);
197         // 将画布输出到文件中
198         ImageIO.write(bi, "PNG", file);
199     }
200     
201     public static File getInputStream(String pressImg,String title,String content, String path) {
202         //String pressImg = "E:/test/c.png"; // 生成二维码的url
203         String modelImg = path+"mb.png";
204         String targetImg = path+"send.png";
205         // 添加小图片
206         pressImage(pressImg, modelImg,targetImg, 0, 0, 1.0f);
207         //title = "  您所在的社区新增了一条通知,请扫码前去查看";
208         // 添加文字
209         return pressText(title, targetImg,"微软雅黑", Font.BOLD, Color.RED, 25, 0,0, 1.0f);
210     }
211     
212     public static void main(String[] args) throws IOException {
213 //        drawVerifyCode();
214 //        String path = "E:/test/";
215 //     
216         String pressImg = "E:/test/c.png"; // 生成二维码的url
217         String modelImg = "E:/test/mb.png";
218         String targetImg = "E:/test/ts.png";
219         // 添加小图片
220         pressImage(pressImg, modelImg,targetImg, 0, 0, 1.0f);
221         String data = "  随便说点啥";
222 //
223         // 添加文字
224     pressText(data, targetImg,"微软雅黑", Font.BOLD, Color.RED, 25, 0,0, 1.0f);
225 //    resize("E:/test/c.png", 182, 182, true);
226 //        String data = "开始加文字,这是加文字吗多加文字,这是加文字吗多加文字,这是加文字吗多加文字,这是加文字吗多加文字,这是加文字吗多加文字,这是加文字吗多加文字,这是加文字吗多加文字,这是加文字吗多加文字,这是加文字吗多加文字,这是加文字吗多加文字";
227 //        int length = getLength(data);
228 //        System.out.println(length);
229     }
230 }
231  
232  

 

处理图片的工具类

标签:file   cal   idt   ext   fine   eth   utf-8   png   pos   

原文地址:http://www.cnblogs.com/dp-blog/p/7463950.html

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