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

相关文章

使用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实现后台管理项目一定含有表格功能,通常离不开表单

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

C#使用Spire.XLS快速生成多表格Excel文件

《C#使用Spire.XLS快速生成多表格Excel文件》在日常开发中,我们经常需要将业务数据导出为结构清晰的Excel文件,本文将手把手教你使用Spire.XLS这个强大的.NET组件,只需几行C#... 目录一、Spire.XLS核心优势清单1.1 性能碾压:从3秒到0.5秒的质变1.2 批量操作的优雅

Python使用python-pptx自动化操作和生成PPT

《Python使用python-pptx自动化操作和生成PPT》这篇文章主要为大家详细介绍了如何使用python-pptx库实现PPT自动化,并提供实用的代码示例和应用场景,感兴趣的小伙伴可以跟随小编... 目录使用python-pptx操作PPT文档安装python-pptx基础概念创建新的PPT文档查看

在ASP.NET项目中如何使用C#生成二维码

《在ASP.NET项目中如何使用C#生成二维码》二维码(QRCode)已广泛应用于网址分享,支付链接等场景,本文将以ASP.NET为示例,演示如何实现输入文本/URL,生成二维码,在线显示与下载的完整... 目录创建前端页面(Index.cshtml)后端二维码生成逻辑(Index.cshtml.cs)总结

Python实现数据可视化图表生成(适合新手入门)

《Python实现数据可视化图表生成(适合新手入门)》在数据科学和数据分析的新时代,高效、直观的数据可视化工具显得尤为重要,下面:本文主要介绍Python实现数据可视化图表生成的相关资料,文中通过... 目录前言为什么需要数据可视化准备工作基本图表绘制折线图柱状图散点图使用Seaborn创建高级图表箱线图热