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利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

java中使用POI生成Excel并导出过程

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求说明及实现方式需求完成通用代码版本1版本2结果展示type参数为atype参数为b总结注:本文章中代码均为

在java中如何将inputStream对象转换为File对象(不生成本地文件)

《在java中如何将inputStream对象转换为File对象(不生成本地文件)》:本文主要介绍在java中如何将inputStream对象转换为File对象(不生成本地文件),具有很好的参考价... 目录需求说明问题解决总结需求说明在后端中通过POI生成Excel文件流,将输出流(outputStre

Vue 调用摄像头扫描条码功能实现代码

《Vue调用摄像头扫描条码功能实现代码》本文介绍了如何使用Vue.js和jsQR库来实现调用摄像头并扫描条码的功能,通过安装依赖、获取摄像头视频流、解析条码等步骤,实现了从开始扫描到停止扫描的完整流... 目录实现步骤:代码实现1. 安装依赖2. vue 页面代码功能说明注意事项以下是一个基于 Vue.js

C/C++随机数生成的五种方法

《C/C++随机数生成的五种方法》C++作为一种古老的编程语言,其随机数生成的方法已经经历了多次的变革,早期的C++版本使用的是rand()函数和RAND_MAX常量,这种方法虽然简单,但并不总是提供... 目录C/C++ 随机数生成方法1. 使用 rand() 和 srand()2. 使用 <random

Flask 验证码自动生成的实现示例

《Flask验证码自动生成的实现示例》本文主要介绍了Flask验证码自动生成的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习... 目录生成图片以及结果处理验证码蓝图html页面展示想必验证码大家都有所了解,但是可以自己定义图片验证码

Python如何在Word中生成多种不同类型的图表

《Python如何在Word中生成多种不同类型的图表》Word文档中插入图表不仅能直观呈现数据,还能提升文档的可读性和专业性,本文将介绍如何使用Python在Word文档中创建和自定义各种图表,需要的... 目录在Word中创建柱形图在Word中创建条形图在Word中创建折线图在Word中创建饼图在Word

nginx生成自签名SSL证书配置HTTPS的实现

《nginx生成自签名SSL证书配置HTTPS的实现》本文主要介绍在Nginx中生成自签名SSL证书并配置HTTPS,包括安装Nginx、创建证书、配置证书以及测试访问,具有一定的参考价值,感兴趣的可... 目录一、安装nginx二、创建证书三、配置证书并验证四、测试一、安装nginxnginx必须有"-