用com.google.zxing生成、解析二维码

http://code.google.com/p/zxing/downloads/list下载zxing压缩包(我用的Zxing-1.5),解压后将core/src和javase/src中的com文件夹整体复制到你的java工程中,这两个包里面包含java所用的java源码,

代码如下:

package com.easyoa.test;

import java.awt.image.BufferedImage;

importjava.io.File;

importjava.io.IOException;

import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;

importcom.google.zxing.BinaryBitmap;

importcom.google.zxing.DecodeHintType;

importcom.google.zxing.LuminanceSource;

importcom.google.zxing.MultiFormatReader;

importcom.google.zxing.MultiFormatWriter;

importcom.google.zxing.Reader;

importcom.google.zxing.ReaderException;

importcom.google.zxing.Result;

importcom.google.zxing.client.j2se.BufferedImageLuminanceSource;

importcom.google.zxing.common.ByteMatrix;

import com.google.zxing.common.HybridBinarizer;

public class Test {

privatestaticfinalintBLACK=0xff000000;

privatestaticfinalintWHITE=0xFFFFFFFF;

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

Testtest=newTest();

test.encode();

test.decode();

}

//编码

/**

*在编码时需要将com.google.zxing.qrcode.encoder.Encoder.java中的

*staticfinalStringDEFAULT_BYTE_MODE_ENCODING="ISO8859-1";修改为UTF-8,否则中文编译后解析不了

*/

publicvoidencode(){

try{

Stringstr="姓名:张三,性别:男,年龄:25,籍贯:中国北京,";//二维码内容

Stringpath="D://test.png";

ByteMatrixbyteMatrix;

byteMatrix=newMultiFormatWriter().encode(str,BarcodeFormat.QR_CODE,200,200);

Filefile=newFile(path);

writeToFile(byteMatrix,"png",file);

}catch(Exceptione){

e.printStackTrace();

}

}

publicstaticvoidwriteToFile(ByteMatrixmatrix,Stringformat,Filefile)

throwsIOException{

BufferedImageimage=toBufferedImage(matrix);

ImageIO.write(image,format,file);

}

publicstaticBufferedImagetoBufferedImage(ByteMatrixmatrix){

intwidth=matrix.getWidth();

intheight=matrix.getHeight();

BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);

for(intx=0;x<width;x++){

for(inty=0;y<height;y++){

image.setRGB(x,y,matrix.get(x,y)==0?BLACK:WHITE);

}

}

returnimage;

}

//解码

publicvoiddecode(){

try{

Readerreader=newMultiFormatReader();

StringimgPath="D://test.png";

Filefile=newFile(imgPath);

BufferedImageimage;

try{

image=ImageIO.read(file);

if(image==null){

System.out.println("Couldnotdecodeimage");

}

LuminanceSourcesource=newBufferedImageLuminanceSource(image);

BinaryBitmapbitmap=newBinaryBitmap(newHybridBinarizer(source));

Resultresult;

Hashtablehints=newHashtable();

hints.put(DecodeHintType.CHARACTER_SET,"utf-8");

//解码设置编码方式为:utf-8,

result=newMultiFormatReader().decode(bitmap,hints);

StringresultStr=result.getText();

    System.out.println("解析后内容:"+resultStr);

   } catch (IOException ioe) { 

System.out.println(ioe.toString());

}catch(ReaderExceptionre){

System.out.println(re.toString());

   }

  }catch(Exception ex){

System.out.println(ex.toString());

}

 }

}

好了,运行一下是不是很简单?

生成后的二维码:

用com.google.zxing生成、解析二维码

解码后:

解析后内容:姓名:张三,性别:男,年龄:25,籍贯:中国北京,

欢迎朋友留言交流;

<转载自:http://blog.csdn.net/a_b_a_b_a_b_a_b/article/details/6197636>

相关推荐