汤姆熊猫

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

以下位置可以下载zxing(Code License:Apache License 2.0):

http://code.google.com/p/zxing/downloads/list

我下载的是Zxing-1.7.zip

1、安装JDK

2、编译ZXing

    可以参照Google的手册进行编译

  http://code.google.com/p/zxing/wiki/GettingStarted

3、找到编译好的core.jar和javase.jar备用

    (这两个文件主要用于PC开发,如果需要在Android上开发的话,需要编译相应的Source)

4、测试QRcode的生成和解码

 

package com.dalian.qrcode.test;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;

import javax.imageio.ImageIO; 

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import com.google.zxing.DecodeHintType; 
import com.google.zxing.LuminanceSource; 
import com.google.zxing.MultiFormatReader; 
import com.google.zxing.Result; 
import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 
import com.google.zxing.common.HybridBinarizer;

public class QRcoder {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        encode();
        decode();
    }
    
    static void encode(){
        try{
            //String str = "http://www.cnblogs.com/tompandas";
            
            StringBuilder sb = new StringBuilder(1024);
            sb.append("Name:TomPandas");
            sb.append("\r\n");
            sb.append("WebSite:http://www.cnblogs.com/tompandas");
            sb.append("\r\n");            
            String str = sb.toString();
            
            String picFormat = "png";
            String path = "d:/test_qrcode";
            File file = new File(path + "." + picFormat);
            str = new String(str.getBytes("GBK"), "ISO-8859-1");
            Hashtable hints = new Hashtable();
            BitMatrix bitMatrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 200, 200, hints);
            MatrixToImageWriter.writeToFile(bitMatrix, picFormat, file);
            
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
    }
    
    static void decode(){
        try{
            String file = "d:/test_qrcode.png";
            Result result = null; 
            BufferedImage image = null;
            
            image = ImageIO.read(new File(file));
            LuminanceSource source = new BufferedImageLuminanceSource(image); 
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); 
            Hashtable<Object, Object> hints = new Hashtable<Object, Object>(); 
            hints.put(DecodeHintType.CHARACTER_SET, "GBK"); 
 
            result = new MultiFormatReader().decode(bitmap, hints); 
            String rtn = result.getText();
            System.out.println(rtn);
            
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
    }

}

 

以下是生成的文件

 

 

如果中文有乱码的话,请参照以下试试,反正我没试过。

源代码中有两处UTF-8的问题,会导致乱码,

其一:com.google.zxing.qrcode.encoder.encoder类中的

internal const System.String DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1";

此处,将ISO-8859-1改为UTF-8

其二:com.google.zxing.qrcode.decoder.DecodedBitStreamParser类的成员

private const System.String UTF8 = "UTF8";

应将UTF8改为UTF-8

posted on 2012-04-10 14:19  汤姆熊猫  阅读(7354)  评论(0编辑  收藏  举报