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

2024-01-28 11:38

本文主要是介绍Zxing2.2 生成QR二维码和一维码(条码),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在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. }  

这篇关于Zxing2.2 生成QR二维码和一维码(条码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/653534

相关文章

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco

SpringBoot集成iText快速生成PDF教程

《SpringBoot集成iText快速生成PDF教程》本文介绍了如何在SpringBoot项目中集成iText9.4.0生成PDF文档,包括新特性的介绍、环境准备、Service层实现、Contro... 目录SpringBoot集成iText 9.4.0生成PDF一、iText 9新特性与架构变革二、环

idea-java序列化serialversionUID自动生成方式

《idea-java序列化serialversionUID自动生成方式》Java的Serializable接口用于实现对象的序列化和反序列化,通过将对象转换为字节流来存储或传输,实现Serializa... 目录简介实现序列化serialVersionUID配置使用总结简介Java.io.Seripyth

Java中的随机数生成案例从范围字符串到动态区间应用

《Java中的随机数生成案例从范围字符串到动态区间应用》本文介绍了在Java中生成随机数的多种方法,并通过两个案例解析如何根据业务需求生成特定范围的随机数,本文通过两个实际案例详细介绍如何在java中... 目录Java中的随机数生成:从范围字符串到动态区间应用引言目录1. Java中的随机数生成基础基本随

C#自动化生成PowerPoint(PPT)演示文稿

《C#自动化生成PowerPoint(PPT)演示文稿》在当今快节奏的商业环境中,演示文稿是信息传递和沟通的关键工具,下面我们就深入探讨如何利用C#和Spire.Presentationfor.NET... 目录环境准备与Spire.Presentation安装核心操作:添加与编辑幻灯片元素添加幻灯片文本操

Python实现Word文档自动化的操作大全(批量生成、模板填充与内容修改)

《Python实现Word文档自动化的操作大全(批量生成、模板填充与内容修改)》在职场中,Word文档是公认的好伙伴,但你有没有被它折磨过?批量生成合同、制作报告以及发放证书/通知等等,这些重复、低效... 目录重复性文档制作,手动填充模板,效率低下还易错1.python-docx入门:Word文档的“瑞士

使用python生成固定格式序号的方法详解

《使用python生成固定格式序号的方法详解》这篇文章主要为大家详细介绍了如何使用python生成固定格式序号,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下... 目录生成结果验证完整生成代码扩展说明1. 保存到文本文件2. 转换为jsON格式3. 处理特殊序号格式(如带圈数字)4

Java使用Swing生成一个最大公约数计算器

《Java使用Swing生成一个最大公约数计算器》这篇文章主要为大家详细介绍了Java使用Swing生成一个最大公约数计算器的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下... 目录第一步:利用欧几里得算法计算最大公约数欧几里得算法的证明情形 1:b=0情形 2:b>0完成相关代码第二步:加

k8s admin用户生成token方式

《k8sadmin用户生成token方式》用户使用Kubernetes1.28创建admin命名空间并部署,通过ClusterRoleBinding为jenkins用户授权集群级权限,生成并获取其t... 目录k8s admin用户生成token创建一个admin的命名空间查看k8s namespace 的

Vue3 如何通过json配置生成查询表单

《Vue3如何通过json配置生成查询表单》本文给大家介绍Vue3如何通过json配置生成查询表单,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录功能实现背景项目代码案例功能实现背景通过vue3实现后台管理项目一定含有表格功能,通常离不开表单