Zxing2.2 生成QR二维码和一维码(条码)

12 篇文章 1 订阅

http://code.google.com/p/zxing/downloads/list下载zxing压缩包Zxing-2.2,

使用core包

代码如下:

 

Java代码   收藏代码
  1. package cn.wuhongbox.common.javaQR;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4. import java.io.File;  
  5. import java.io.IOException;  
  6. import java.util.Hashtable;  
  7.   
  8. import javax.imageio.ImageIO;  
  9.   
  10. import com.google.zxing.BarcodeFormat;  
  11. import com.google.zxing.BinaryBitmap;  
  12. import com.google.zxing.DecodeHintType;  
  13. import com.google.zxing.EncodeHintType;  
  14. import com.google.zxing.LuminanceSource;  
  15. import com.google.zxing.MultiFormatReader;  
  16. import com.google.zxing.MultiFormatWriter;  
  17. import com.google.zxing.Result;  
  18. import com.google.zxing.client.j2se.BufferedImageLuminanceSource;  
  19. import com.google.zxing.common.BitMatrix;  
  20. import com.google.zxing.common.HybridBinarizer;  
  21.   
  22. public class QRUtil  
  23. {  
  24.     private static final String CODE = "utf-8";  
  25.     private static final int BLACK = 0xff000000;  
  26.     private static final int WHITE = 0xFFFFFFFF;  
  27.   
  28.     /** 
  29.      * 生成RQ二维码 
  30.      *  
  31.      * @author wuhongbo 
  32.      * @param str 
  33.      *            内容 
  34.      * @param height 
  35.      *            高度(px) 
  36.      *  
  37.      */  
  38.     public static BufferedImage getRQ(String str, Integer height)  
  39.     {  
  40.         if (height == null || height < 100)  
  41.         {  
  42.             height = 200;  
  43.         }  
  44.   
  45.         try  
  46.         {  
  47.             // 文字编码  
  48.             Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
  49.             hints.put(EncodeHintType.CHARACTER_SET, CODE);  
  50.   
  51.             BitMatrix bitMatrix = new MultiFormatWriter().encode(str,  
  52.                     BarcodeFormat.QR_CODE, height, height, hints);  
  53.   
  54.             return toBufferedImage(bitMatrix);  
  55.   
  56.             // 输出方式  
  57.             // 网页  
  58.             // ImageIO.write(image, "png", response.getOutputStream());  
  59.   
  60.             // 文件  
  61.             // ImageIO.write(image, "png", file);  
  62.         }  
  63.         catch (Exception e)  
  64.         {  
  65.             e.printStackTrace();  
  66.         }  
  67.         return null;  
  68.     }  
  69.   
  70.     /** 
  71.      * 生成一维码(128) 
  72.      *  
  73.      * @author wuhongbo 
  74.      * @param str 
  75.      * @param width 
  76.      * @param height 
  77.      * @return 
  78.      */  
  79.     public static BufferedImage getBarcode(String str, Integer width,  
  80.             Integer height)  
  81.     {  
  82.   
  83.         if (width == null || width < 200)  
  84.         {  
  85.             width = 200;  
  86.         }  
  87.   
  88.         if (height == null || height < 50)  
  89.         {  
  90.             height = 50;  
  91.         }  
  92.   
  93.         try  
  94.         {  
  95.             // 文字编码  
  96.             Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
  97.             hints.put(EncodeHintType.CHARACTER_SET, CODE);  
  98.   
  99.             BitMatrix bitMatrix = new MultiFormatWriter().encode(str,  
  100.                     BarcodeFormat.CODE_128, width, height, hints);  
  101.   
  102.             return toBufferedImage(bitMatrix);  
  103.         }  
  104.         catch (Exception e)  
  105.         {  
  106.             e.printStackTrace();  
  107.         }  
  108.         return null;  
  109.     }  
  110.   
  111.     /** 
  112.      * 生成二维码,写到文件中 
  113.      *  
  114.      * @author wuhongbo 
  115.      * @param str 
  116.      * @param height 
  117.      * @param file 
  118.      * @throws IOException 
  119.      */  
  120.     public static void getRQWriteFile(String str, Integer height, File file)  
  121.             throws IOException  
  122.     {  
  123.         BufferedImage image = getRQ(str, height);  
  124.         ImageIO.write(image, "png", file);  
  125.     }  
  126.   
  127.     /** 
  128.      * 生成一维码,写到文件中 
  129.      *  
  130.      * @author wuhongbo 
  131.      * @param str 
  132.      * @param height 
  133.      * @param file 
  134.      * @throws IOException 
  135.      */  
  136.     public static void getBarcodeWriteFile(String str, Integer width,  
  137.             Integer height, File file) throws IOException  
  138.     {  
  139.         BufferedImage image = getBarcode(str, width, height);  
  140.         ImageIO.write(image, "png", file);  
  141.     }  
  142.   
  143.     /** 
  144.      * 转换成图片 
  145.      *  
  146.      * @author wuhongbo 
  147.      * @param matrix 
  148.      * @return 
  149.      */  
  150.     private static BufferedImage toBufferedImage(BitMatrix matrix)  
  151.     {  
  152.         int width = matrix.getWidth();  
  153.         int height = matrix.getHeight();  
  154.         BufferedImage image = new BufferedImage(width, height,  
  155.                 BufferedImage.TYPE_INT_ARGB);  
  156.         for (int x = 0; x < width; x++)  
  157.         {  
  158.             for (int y = 0; y < height; y++)  
  159.             {  
  160.                 image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);  
  161.             }  
  162.         }  
  163.         return image;  
  164.     }  
  165.   
  166.     /** 
  167.      * 解码(二维、一维均可) 
  168.      */  
  169.     public static String read(File file)  
  170.     {  
  171.   
  172.         BufferedImage image;  
  173.         try  
  174.         {  
  175.             if (file == null || file.exists() == false)  
  176.             {  
  177.                 throw new Exception(" File not found:" + file.getPath());  
  178.             }  
  179.   
  180.             image = ImageIO.read(file);  
  181.   
  182.             LuminanceSource source = new BufferedImageLuminanceSource(image);  
  183.             BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
  184.   
  185.             Result result;  
  186.   
  187.             // 解码设置编码方式为:utf-8,  
  188.             Hashtable hints = new Hashtable();  
  189.             hints.put(DecodeHintType.CHARACTER_SET, "utf-8");  
  190.   
  191.             result = new MultiFormatReader().decode(bitmap, hints);  
  192.   
  193.             return result.getText();  
  194.   
  195.         }  
  196.         catch (Exception e)  
  197.         {  
  198.             e.printStackTrace();  
  199.         }  
  200.   
  201.         return null;  
  202.     }  
  203.   
  204.     public static void main(String[] args) throws Exception  
  205.     {  
  206.         File file = new File("c://1.png");  
  207.         // RQUtil.getRQwriteFile("吴宏波中华人民共和国", 200, file);  
  208.   
  209.         // code39 大写字母、数字、-、  
  210.         // code128   
  211.         QRUtil.getBarcodeWriteFile("12345678900-J_j"null,null, file);  
  212.   
  213.         System.out.println("-----成生成功----");  
  214.         System.out.println();  
  215.   
  216.         String s = QRUtil.read(file);  
  217.   
  218.         System.out.println("-----解析成功----");  
  219.         System.out.println(s);  
  220.     }  
  221.   
  222. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值