zoukankan      html  css  js  c++  java
  • ZXing二维码的生成和解析

    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;
        } 

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

  • 相关阅读:
    [导入]遍历表单的代码,包括遍历所有,以及遍历特定tag,如input,select lcs
    [导入]JS更改onclick事件 lcs
    [导入]SQLite 字符串长度 函数 lcs
    [导入]休眠 睡眠 区别 lcs
    【转】OpenGL鼠标点击事件
    【转】键盘按键和键盘对应代码表
    【转】vs2010快捷键;sql server 2008快捷;IE9快捷键
    【转】vc6.0移植到VS2010遇到的问题,散分给大家,虽然分不多,各位帮忙
    【转】单缓冲与双缓冲的区别
    【转】光照、材质、纹理的关系
  • 原文地址:https://www.cnblogs.com/wuxiang/p/3360920.html
Copyright © 2011-2022 走看看