Zxing是Google提供的关于条码(一维码、二维码)的解析工具,提供了二维码的生成与解析的方法,

现在我简单介绍一下使用Java利用Zxing生成与解析二维码

注意:

二维码的生成需要借助辅助类(MatrixToImageWriter),解析需要一个辅助类(BufferedImageLuminanceSource),两个辅助类Google都提供了,下载附件com.google.zxing.client.rar两个辅助类源码,解压放入项目中引用就可以了。

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import javax.imageio.ImageIO;

//引用辅助类

import com.test.util.BufferedImageLuminanceSource;
import com.test.util.MatrixToImageWriter;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Reader;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;

/**
     * <p>Title: encode</p>
     * <p>Description: 生成二维码的实现代码</p>
     * @param content  要生成二维码的内容
     * @param imgpath  生成二维码图片的路径
     * @date
     */
    public void encode(String content,String imgpath) {  
        try {  
            BitMatrix byteMatrix;  
            
//            Hashtable hints= new Hashtable();  
//            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");  
//            BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height,hints);  
             
            byteMatrix = new MultiFormatWriter().encode(new String(content.getBytes("GBK"),"iso-8859-1"),  
                    BarcodeFormat.QR_CODE, 200, 200);  
            File file = new File(imgpath);  
              
            MatrixToImageWriter.writeToFile(byteMatrix, "png", file);  
            System.out.println("成功生成二维码");
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }

/**
   * <p>Title: decode</p>
   * <p>Description:解析二维码的实现代码 </p>
   * @param imgPath 二维码的路径
   * @return  返回解析的二维码内容
   * @date
   */
    public String decode(String imgPath) {  
        String response = "";
        try {  
            Reader reader = new MultiFormatReader();  
          
            File file = new File(imgPath);  
            BufferedImage image;  
            try {  
                image = ImageIO.read(file);  
                if (image == null) {  
                    System.out.println("Could not find image");  
                }  
                LuminanceSource source = new BufferedImageLuminanceSource(image);  
                BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
                Result result;  
                Hashtable hints = new Hashtable();  
                hints.put(DecodeHintType.CHARACTER_SET, "GBK");  
                result = new MultiFormatReader().decode(bitmap, hints);  
                
                System.out.println("解析出的结果如下:");
                System.out.println("result = "+ result.toString());
                   System.out.println("resultFormat = "+ result.getBarcodeFormat());
                   System.out.println("resultText = "+ result.getText());
                   
                   response = result.toString();
 
            } catch (IOException ioe) {  
                System.out.println(ioe.toString());  
            } catch (ReaderException re) {  
                System.out.println(re.toString());  
            }  
 
        } catch (Exception ex) {  
             ex.printStackTrace();
        }
        
        return response;
    }

现在你就可以生成你想要的二维码并解析你看到的二维码看看它是什么内容。

ZXing二维码的生成和解析的更多相关文章

  1. .net core 的图片处理及二维码的生成及解析

    写代码这事,掐指算来已经十有余年. 从html到css到javascript到vbscript到c#,从兴趣到职业,生活总是失落与惊喜并存. 绝大部分时候,出发并不是因为知道该到哪里去,只是知道不能再 ...

  2. 关于java的二维码的生成与解析

    本文说的是通过zxing实现二维码的生成与解析,看着很简单,直接上代码 import java.io.File; import java.io.IOException; import java.nio ...

  3. Java 条形码 二维码 的生成与解析

    Barcode简介 Barcode是由一组按一定编码规则排列的条,空符号,用以表示一定的字符,数字及符号组成的,一种机器可读的数据表示方式. Barcode的形式多种多样,按照它们的外观分类: Lin ...

  4. zxing二维码的生成与解码(C#)

    ZXing是一个开源Java类库用于解析多种格式的1D/2D条形码.目标是能够对QR编码.Data Matrix.UPC的1D条形码进行解码. 其提供了多种平台下的客户端包括:J2ME.J2SE和An ...

  5. java实现二维码的生成和解析:QRCode、zxing 两种方式

    第一种:QRCode.jar,使用QRCode生成和解析二维码 1.导入jar包  2.代码 (1)QRCodeUtil .java import com.swetake.util.Qrcode; i ...

  6. Android二维码的生成,解析以及扫描功能

    <1> 布局只有2个按钮,实现生成二维码和解析二维码 <Button android:layout_width="wrap_content" android:la ...

  7. JavaUtil_02_二维码的生成与解析

    1.引入jar包 zxing-core-1.7.jar  :   http://viralpatel.net/blogs/download/jar/zxing-core-1.7.jar zxing-j ...

  8. java实现二维码的生成与解析

    简单介绍下二维码:二维码其实就是一种编码技术,只是这种编码技术是用在图片上了,将给定的一些文字,数字转换为一张经过特定编码的图片,而解析二维码则相反,就是将一张经过编码的图片解析为数字或者文字. 当然 ...

  9. 【Java】Java实现二维码的生成与解析

    pom依赖 <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</ ...

随机推荐

  1. Android Studio中的Module,Facet

    详细内容请参看 http://www.jetbrains.com/idea/webhelp/facet.html 以及 http://www.jetbrains.com/idea/webhelp/an ...

  2. sqlserver的执行计划

    一:执行计划生成过程 说到执行计划,首先要知道的是执行计划大概生成的过程,这样就可以做到就心中有数了,下面我画下简图: 1. 分析过程 这三个比较容易理解,首先我们要保证sql的语法不能错误,sele ...

  3. Fastreport使用经验(转)在Delphi程序中访问报表对象

    Fastreport使用经验(转) 在Delphi程序中访问报表对象 最基本的方法就是frxReport1.FindObject. 然后把返回的对象强制转换成它的类型,当然,在报表中必须真的有这么个东 ...

  4. Python多线程(2)——线程同步机制

    本文介绍Python中的线程同步对象,主要涉及 thread 和 threading 模块. threading 模块提供的线程同步原语包括:Lock.RLock.Condition.Event.Se ...

  5. codeforces B. The Fibonacci Segment 解题报告

    题目链接:http://codeforces.com/problemset/problem/365/B 题目意思:简单来说,就是要找出最长的斐波纳契长度. 解决的方法不难,但是要注意更新左区间和右区间 ...

  6. springJDBC实现查询方法二

    无废话,看代码: @Override public List<Sites> queryAllSites(Pager pager) { String sql = "select * ...

  7. mybatis There is no getter for property named 'xx' in 'class java.lang.String

    转载自://http://www.cnblogs.com/anee/p/3324140.html 用mybatis查询时,传入一个字符串传参数,且进行判断时,会报 There is no getter ...

  8. 【wireshark】打开后显示There are no interfaces on which a capture can be done

    解决方式:用管理员方式打开wireshark即可

  9. javascript中json解密

    一直以前都会断断续续会碰到js中的json数据的解析,下面凭着自己的经验,简单的讲解一下在js中的json的几种解析方法.  一.jquery的方式 首先你得先得到数据,一般都是jquery的ajax ...

  10. Struts2中过滤器和拦截器的区别

    拦截器和过滤器的区别: 1.拦截器是基于java的反射机制的,而过滤器是基于函数回调 2.过滤器依赖与servlet容器,而拦截器不依赖与servlet容器 3.拦截器只能对action请求起作用,而 ...