ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果

首先说下,QRCode是日本人开发的,ZXing是google开发,barcode4j也是老美开发的,barcode4j对一维条形码处理的很好,而且支持的格式很多,当然也可以对二维码进行处理,效果个人感觉没有前两种好;ZXing对j2me,j2se,还有Android等支持也比较好,如果你是搞Android的或以后准备走Android,建议还是用zxing的比较好,毕竟都一个母亲(goole)生的,QRCode就不用说了吧,虽说技术无国界,但是国人还是有点...。

好,言归正传,java用ZXing开发二维码,首先需要添加code.jar包,这个包需要自己生成下,官网下载ZXing-2.3.0(目前最新的)后,解压 把code中的代码拷贝到一个工程中生成jar包。ZXing-code-2.3.0.jar包和源码包本人已编译后,不想自己重新编译下的可以直接下载使用的,下面的代码就是使用这个jar包的,ZXing-code-2.3.0.jar下载地址:http://download.csdn.net/detail/sanfye/8704583  

Code源码包:http://download.csdn.net/detail/sanfye/8704593

ZXing 生成二维码

二维码的生成可以参考Google代码测试包中的MatrixToImageWriter类来辅助开发,可以将该类直接拷贝到源码中使用 (code/src/test/java ),当然你也可以自己写个,个人感觉没必要所以就直接考过来用了。另外,在生成带图片的二维码时要注意下,参数中设置的容错级别尽量设置到最高,以免出现无法解析的情况:hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

[java]  view plain  copy
  1. import java.awt.image.BufferedImage;  
  2. import java.io.File;  
  3. import java.io.IOException;  
  4. import java.io.OutputStream;  
  5.   
  6. import javax.imageio.ImageIO;  
  7.   
  8. import com.google.zxing.common.BitMatrix;  
  9.   
  10. /** 
  11.  * 二维码的生成需要借助MatrixToImageWriter类,该类是由Google提供的,可以将该类直接拷贝到源码中使用,当然你也可以自己写个 
  12.  * 生产条形码的基类 
  13.  */  
  14. public class MatrixToImageWriter {  
  15.     private static final int BLACK = 0xFF000000;//用于设置图案的颜色  
  16.     private static final int WHITE = 0xFFFFFFFF//用于背景色  
  17.   
  18.     private MatrixToImageWriter() {  
  19.     }  
  20.   
  21.     public static BufferedImage toBufferedImage(BitMatrix matrix) {  
  22.         int width = matrix.getWidth();  
  23.         int height = matrix.getHeight();  
  24.         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
  25.         for (int x = 0; x < width; x++) {  
  26.             for (int y = 0; y < height; y++) {  
  27.                 image.setRGB(x, y,  (matrix.get(x, y) ? BLACK : WHITE));  
  28. //              image.setRGB(x, y,  (matrix.get(x, y) ? Color.YELLOW.getRGB() : Color.CYAN.getRGB()));  
  29.             }  
  30.         }  
  31.         return image;  
  32.     }  
  33.   
  34.     public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {  
  35.         BufferedImage image = toBufferedImage(matrix);  
  36.         //设置logo图标  
  37.         LogoConfig logoConfig = new LogoConfig();  
  38.         image = logoConfig.LogoMatrix(image);  
  39.           
  40.         if (!ImageIO.write(image, format, file)) {  
  41.             throw new IOException("Could not write an image of format " + format + " to " + file);  
  42.         }else{  
  43.             System.out.println("图片生成成功!");  
  44.         }  
  45.     }  
  46.   
  47.     public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {  
  48.         BufferedImage image = toBufferedImage(matrix);  
  49.         //设置logo图标  
  50.         LogoConfig logoConfig = new LogoConfig();  
  51.         image = logoConfig.LogoMatrix(image);  
  52.           
  53.         if (!ImageIO.write(image, format, stream)) {  
  54.             throw new IOException("Could not write an image of format " + format);  
  55.         }  
  56.     }  
  57. }  


操作类:

[java]  view plain  copy
  1. import java.io.File;  
  2. import java.io.IOException;  
  3. import java.util.Hashtable;  
  4. import com.google.zxing.BarcodeFormat;  
  5. import com.google.zxing.EncodeHintType;  
  6. import com.google.zxing.MultiFormatWriter;  
  7. import com.google.zxing.WriterException;  
  8. import com.google.zxing.common.BitMatrix;  
  9. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;  
  10. import com.utils.code.MatrixToImageWriter;  
  11.   
  12. public class EncodeTest {  
  13.       
  14.     public static void Encode_QR_CODE() throws IOException, WriterException{  
  15.         String contents = "ZXing 二维码内容1234!"// 二维码内容  
  16.         int width = 430// 二维码图片宽度 300   
  17.         int height = 430// 二维码图片高度300  
  18.           
  19.         String format = "gif";// 二维码的图片格式 gif  
  20.           
  21.         Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();  
  22.          // 指定纠错等级,纠错级别(L 7%、M 15%、Q 25%、H 30%)  
  23.         hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);  
  24.         // 内容所使用字符集编码  
  25.         hints.put(EncodeHintType.CHARACTER_SET, "utf-8");     
  26. //      hints.put(EncodeHintType.MAX_SIZE, 350);//设置图片的最大值  
  27. //      hints.put(EncodeHintType.MIN_SIZE, 100);//设置图片的最小值  
  28.         hints.put(EncodeHintType.MARGIN, 1);//设置二维码边的空度,非负数  
  29.           
  30.         BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,//要编码的内容  
  31.                 //编码类型,目前zxing支持:Aztec 2D,CODABAR 1D format,Code 39 1D,Code 93 1D ,Code 128 1D,  
  32.                 //Data Matrix 2D , EAN-8 1D,EAN-13 1D,ITF (Interleaved Two of Five) 1D,  
  33.                 //MaxiCode 2D barcode,PDF417,QR Code 2D,RSS 14,RSS EXPANDED,UPC-A 1D,UPC-E 1D,UPC/EAN extension,UPC_EAN_EXTENSION  
  34.                 BarcodeFormat.QR_CODE,  
  35.                 width, //条形码的宽度  
  36.                 height, //条形码的高度  
  37.                 hints);//生成条形码时的一些配置,此项可选  
  38.           
  39.         // 生成二维码  
  40.         File outputFile = new File("d:" + File.separator + "new-1.gif");//指定输出路径  
  41.           
  42.         MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);  
  43.     }  
  44.       
  45.     public static void main(String[] args) throws Exception {  
  46.         try {  
  47.             Encode_QR_CODE();  
  48.         } catch (Exception e) {  
  49.             // TODO: handle exception  
  50.             e.printStackTrace();  
  51.         }  
  52.     }  
  53.       
  54. }  

LogoConfig类,主要处理logo图标已达到和微信自动的二维码相近的效果

[java]  view plain  copy
  1. import java.awt.BasicStroke;  
  2. import java.awt.Color;  
  3. import java.awt.Graphics2D;  
  4. import java.awt.geom.RoundRectangle2D;  
  5. import java.awt.image.BufferedImage;  
  6. import java.io.File;  
  7. import java.io.IOException;  
  8. import javax.imageio.ImageIO;  
  9. /** 
  10.  * 二维码 添加 logo图标 处理的方法, 
  11.  * 模仿微信自动生成二维码效果,有圆角边框,logo和二维码间有空白区,logo带有灰色边框 
  12.  * @author Administrator sangwenhao 
  13.  * 
  14.  */  
  15. public class LogoConfig {  
  16.       
  17.     /** 
  18.      * 设置 logo  
  19.      * @param matrixImage 源二维码图片 
  20.      * @return 返回带有logo的二维码图片 
  21.      * @throws IOException 
  22.      * @author Administrator sangwenhao 
  23.      */  
  24.      public BufferedImage LogoMatrix(BufferedImage matrixImage) throws IOException{  
  25.          /** 
  26.           * 读取二维码图片,并构建绘图对象 
  27.           */  
  28.          Graphics2D g2 = matrixImage.createGraphics();  
  29.            
  30.          int matrixWidth = matrixImage.getWidth();  
  31.          int matrixHeigh = matrixImage.getHeight();  
  32.            
  33.          /** 
  34.           * 读取Logo图片 
  35.           */  
  36.          BufferedImage logo = ImageIO.read(new File("D:\\logo.jpg"));  
  37.   
  38.          //开始绘制图片  
  39.          g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5null);//绘制       
  40.          BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);   
  41.          g2.setStroke(stroke);// 设置笔画对象  
  42.          //指定弧度的圆角矩形  
  43.          RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20);  
  44.          g2.setColor(Color.white);  
  45.          g2.draw(round);// 绘制圆弧矩形  
  46.            
  47.          //设置logo 有一道灰色边框  
  48.          BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);   
  49.          g2.setStroke(stroke2);// 设置笔画对象  
  50.          RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20);  
  51.          g2.setColor(new Color(128,128,128));  
  52.          g2.draw(round2);// 绘制圆弧矩形  
  53.            
  54.          g2.dispose();  
  55.          matrixImage.flush() ;  
  56.          return matrixImage ;  
  57.      }  
  58.       
  59. }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值