动态生成带logo的二维码

3 篇文章 0 订阅

二维码的生成,需要通过GOOGLE提供的ZXING来完成,而水印图片的合成则通过JDK自带的com.sum.image.code.*包来完成

首选,通过http://www.baidu.com搜索zxing包下载,压缩包很大里面包含了j2se、Android用到的jar及示例。

1. 定义一个图片生成器,实现了二维码的生成及logo的添加

[java]  view plain  copy
  1. import java.awt.Graphics;  
  2. import java.awt.image.BufferedImage;  
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.InputStream;  
  9. import java.io.OutputStream;  
  10. import java.util.Hashtable;  
  11.   
  12. import javax.imageio.ImageIO;  
  13.   
  14. import org.apache.log4j.Logger;  
  15.   
  16. import com.google.zxing.BarcodeFormat;  
  17. import com.google.zxing.EncodeHintType;  
  18. import com.google.zxing.MultiFormatWriter;  
  19. import com.google.zxing.common.BitMatrix;  
  20. import com.sun.image.codec.jpeg.JPEGCodec;  
  21. import com.sun.image.codec.jpeg.JPEGImageEncoder;  
  22.   
  23. /**    
  24.  * 二维码生成器 
  25.  * @Description:  
  26.  * @author wuyan    
  27.  * @date 2015年12月18日 下午6:18:37  
  28.  */  
  29. public class Code2dBuilder {  
  30.   
  31.     /** 
  32.      * logo与二维码图片的宽、高比例 
  33.      */  
  34.     private double icon_w_proportion = 0.2 ;  
  35.     private double icon_h_proportion = 0.2 ;  
  36.       
  37.     /** 
  38.      * 生在指定大小的二维码图片 
  39.      * 
  40.      * @author wuyan    
  41.      * @date 2015年12月21日 下午6:10:58  
  42.      * @param text 用于生成二维的信息 
  43.      * @param output 二维码输出流 
  44.      * @param width 宽度 
  45.      * @param height 高度 
  46.      * @param format 图片格式 
  47.      * @throws Exception 
  48.      */  
  49.     public static void build2Code(String text,OutputStream output,int width,int height,String format) throws Exception {  
  50.         Hashtable hints = new Hashtable();  
  51.         hints.put(EncodeHintType.CHARACTER_SET, "utf-8");  
  52.         BitMatrix bitMatrix = new MultiFormatWriter().encode(text,  
  53.         BarcodeFormat.QR_CODE, width, height, hints);  
  54.           
  55. //      MatrixToImageConfig config = new MatrixToImageConfig(MatrixToImageConfig.WHITE,MatrixToImageConfig.BLACK);  
  56.           
  57.         MatrixToImageConfig config = new MatrixToImageConfig();  
  58.         MatrixToImageWriter.writeToStream(bitMatrix, format, output,config);  
  59.           
  60.     }  
  61.       
  62.     /** 
  63.      * 添加水印 
  64.      * 通过输入两张图片的流信息来制作合成图片 
  65.      * @author wuyan    
  66.      * @date 2015年12月21日 下午6:12:11  
  67.      * @param background  底层图片流 
  68.      * @param logo logo图片流 
  69.      * @param dest  合成结果流 
  70.      * @throws Exception 
  71.      */  
  72.     public void addicon(InputStream background,InputStream logo,OutputStream dest) throws Exception{  
  73.         BufferedImage canvas =ImageIO.read(background);  
  74.         BufferedImage icon =ImageIO.read(logo);  
  75.   
  76.         Graphics g = canvas.getGraphics();  
  77. //      int x = (int) canvas.getWidth() /2 - icon.getWidth() / 2 -icon_x_offset;  
  78. //      int y = (int) canvas.getHeight() /2 - icon.getHeight() /2 - icon_y_offset;  
  79. //      int w = (int) (canvas.getWidth() * icon_w_proportion) ;  
  80. //      int h = (int) (canvas.getHeight() * icon_h_proportion) ;  
  81.   
  82.           
  83.         int w = (int) (canvas.getWidth() * icon_w_proportion) ;  
  84.         int h = (int) (canvas.getHeight() * icon_h_proportion) ;  
  85.         int x = (int) canvas.getWidth() /2 - w / 2 ;  
  86.         int y = (int) canvas.getHeight() /2 - h /2 ;  
  87.           
  88.           
  89.         g.drawImage(icon,x,y,w,h,null);  
  90.         JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(dest);  
  91.         enc.encode(canvas);  
  92.         dest.flush();  
  93.         dest.close();  
  94.           
  95.     }  
  96.       
  97.     /** 
  98.      * 添加水印 
  99.      * 通过输入两张图片的流信息来制作合成图片 
  100.      * @author wuyan    
  101.      * @date 2015年12月21日 下午6:12:11  
  102.      * @param background  底层图片流 
  103.      * @param logo logo图片流 
  104.      * @param x 指定横坐标 
  105.      * @param y 指定纵坐标 
  106.      * @param dest  合成结果流 
  107.      * @throws Exception 
  108.      */  
  109.     public void addicon(InputStream background,InputStream logo,int x,int y,int w,int h,OutputStream dest) throws Exception{  
  110.         BufferedImage canvas =ImageIO.read(background);  
  111.         BufferedImage icon =ImageIO.read(logo);  
  112.   
  113.         Graphics g = canvas.getGraphics();  
  114.         g.drawImage(icon,x,y,w,h,null);  
  115.         JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(dest);  
  116.         enc.encode(canvas);  
  117.         dest.flush();  
  118.         dest.close();  
  119.   
  120.     }  
  121.       
  122.   
  123.   
  124.     /** 
  125.      * @param icon_w_proportion the icon_w_proportion to set 
  126.      */  
  127.     public void setIcon_w_proportion(double icon_w_proportion) {  
  128.         this.icon_w_proportion = icon_w_proportion;  
  129.     }  
  130.   
  131.     /** 
  132.      * @param icon_h_proportion the icon_h_proportion to set 
  133.      */  
  134.     public void setIcon_h_proportion(double icon_h_proportion) {  
  135.         this.icon_h_proportion = icon_h_proportion;  
  136.     }  
  137. }  


2. 定义一个jsp页面用于展示二维码信息code2d.jsp

[html]  view plain  copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3.   
  4. <%@ page import="java.io.*" %>  
  5. <%@ page import="org.apache.log4j.Logger" %>  
  6. <%@ page import="code2d.*" %>  
  7.   
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  9.   
  10. <%  
  11. String icon = "./logo.png" ;  
  12.   
  13. String path=request.getSession().getServletContext().getRealPath("/");  
  14. String text = request.getParameter("text");  
  15. Logger logger = Logger.getRootLogger();  
  16.   
  17. if (text == null)  
  18.     response.sendError(HttpServletResponse.SC_BAD_REQUEST);  
  19.           
  20. logger.info("text:" + text);  
  21.           
  22. try {  
  23.               
  24.     ByteArrayOutputStream result = new ByteArrayOutputStream();  
  25.       
  26. //创建二维码   
  27.     Code2dBuilder code2dBuilder = new Code2dBuilder();  
  28.     code2dBuilder.build2Code(text, result,370,370,"jpg");  
  29. //判断图标            
  30.       
  31.     File iconfile = new File(path + icon) ;  
  32.     if (iconfile.exists()){  
  33.         InputStream icon_in = new FileInputStream(iconfile);  
  34.           
  35.         ByteArrayInputStream code2d_in = new ByteArrayInputStream(result.toByteArray());  
  36.         result = new ByteArrayOutputStream();  
  37. //添加图标        
  38.         code2dBuilder.addicon(code2d_in,icon_in,result);  
  39.         code2d_in.close();  
  40.         icon_in.close();  
  41.     }  
  42.       
  43.     File outfile = new File(path + "/out.png");             
  44.     outfile.createNewFile();  
  45.       
  46.     FileOutputStream fos = new FileOutputStream(outfile);  
  47.     fos.write(result.toByteArray());  
  48.     fos.close();  
  49.     result.flush();  
  50.     result.close();  
  51.     out.print("<img src='./out.png'/>");  
  52.   
  53.   
  54. } catch (Exception e) {  
  55.     logger.error(e);  
  56.     e.printStackTrace();  
  57.     out.print(e.getClass().getName() + " " + e.getMessage());  
  58. }  
  59.   
  60. %>  

3. 访问方式

http://localhost:8080/code2d.jsp?text=http://www.baidu.com


text:即想要生成二维码的内容

logo:已经在jsp页面中指定了固定的路径地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值