image.setRGB(x,y,matrix.get(x,y)?
BLACK:
WHITE);
}
}
returnimage;
}
publicstaticvoidwriteToFile(BitMatrixmatrix,Stringformat,Filefile)
throwsIOException{
BufferedImageimage=toBufferedImage(matrix);
if(!
ImageIO.write(image,format,file)){
thrownewIOException("Couldnotwriteanimageofformat"+format+"to"+file);
}
}
publicstaticvoidwriteToStream(BitMatrixmatrix,Stringformat,OutputStreamstream)
throwsIOException{
BufferedImageimage=toBufferedImage(matrix);
if(!
ImageIO.write(image,format,stream)){
thrownewIOException("Couldnotwriteanimageofformat"+format);
}
}
}1.3编写生成二维码的实现代码//www.hake.cc
[java]viewplaincopyprint?
importjava.io.File;
importjava.util.Hashtable;
importcom.google.zxing.BarcodeFormat;
importcom.google.zxing.EncodeHintType;
importcom.google.zxing.MultiFormatWriter;
importmon.BitMatrix;
publicclassTestEncode{
publicstaticvoidmain(String[]args)throwsException{
Stringtext="你好";
intwidth=100;
intheight=100;
Stringformat="png";
Hashtablehints=newHashtable();
hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
BitMatrixbitMatrix=newMultiFormatWriter().encode(text,BarcodeFormat.QR_CODE,width,height,hints);
FileoutputFile=newFile("d:
//new.png");
MatrixToImageWriter.writeToFile(bitMatrix,format,outputFile);
}
}
importjava.io.File;
importjava.util.Hashtable;
importcom.google.zxing.BarcodeFormat;
importcom.google.zxing.EncodeHintType;
importcom.google.zxing.MultiFormatWriter;
importmon.BitMatrix;
publicclassTestEncode{
publicstaticvoidmain(String[]args)throwsException{
Stringtext="你好";
intwidth=100;
intheight=100;
Stringformat="png";
Hashtablehints=newHashtable();
hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
BitMatrixbitMatrix=newMultiFormatWriter().encode(text,BarcodeFormat.QR_CODE,width,height,hints);
FileoutputFile=newFile("d:
//new.png");
MatrixToImageWriter.writeToFile(bitMatrix,format,outputFile);
}
}现在运行后即可生成一张二维码图片,是不是很简单啊?
接下来我们看看如何解析二维码
2、二维码的解析
2.1将core.jar包加入到classpath下。
2.2和生成一样,我们需要一个辅助类(BufferedImageLuminanceSource),同样该类Google也提供了,这里我同样将该类的源码贴出来,可以直接拷贝使用个,省去查找的麻烦
[java]viewplaincopyprint?
importcom.google.zxing.LuminanceSource;
importjava.awt.Graphics2D;
importjava.awt.geom.AffineTransform;
importjava.awt.image.BufferedImage;
publicfinalclassBufferedImageLuminanceSourceextendsLuminanceSource{
privatefinalBufferedImageimage;
privatefinalintleft;
privatefinalinttop;
publicBufferedImageLuminanceSource(BufferedImageimage){
this(image,0,0,image.getWidth(),image.getHeight());
}
publicBufferedImageLuminanceSource(BufferedImageimage,intleft,inttop,intwidth,intheight){
super(width,height);
intsourceWidth=image.getWidth();
intsourceHeight=image.getHeight();
if(left+width>sourceWidth||top+height>sourceHeight){
thrownewIllegalArgumentException("Croprectangledoesnotfitwithinimagedata.");
}
for(inty=top;yfor(intx=left;xif((image.getRGB(x,y)&0xFF000000)==0){
image.setRGB(x,y,0xFFFFFFFF);//=white
}
}
}
this.image=newBufferedImage(sourceWidth,sourceHeight,BufferedImage.TYPE_BYTE_GRAY);
this.image.getGraphics().drawImage(image,0,0,null);
this.left=left;
this.top=top;
}
@Override
publicbyte[]getRow(inty,byte[]row){
if(y<0||y>=getHeight()){
thrownewIllegalArgumentException("Requestedrowisoutsidetheimage:
"+y);
}
intwidth=getWidth();
if(row==null||row.lengthrow=newbyte[width];
}
image.getRaster().getDataElements(left,top+y,width,1,row);
returnrow;
}
@Override
publicbyte[]getMatrix(){
intwidth=getWidth();
intheight=getHeight();
intarea=width*height;
byte[]matrix=newbyte[area];
image.getRaster().getDataElements(left,top,width,height,matrix);
returnmatrix;
}
@Override
publicbooleanisCropSupported(){
returntrue;
}
@Override
publicLuminanceSourcecrop(intleft,inttop,intwidth,intheight){
returnnewBufferedImageLuminanceSource(image,this.left+left,this.top+top,width,height);
}
@Override
publicbooleanisRotateSupported(){
returntrue;
}
@Override
publicLuminanceSourcerotateCounterClockwise(){
intsourceWidth=image.getWidth();
intsourceHeight=image.getHeight();
AffineTransformtransform=newAffineTransform(0.0,-1.0,1.0,0.0,0.0,sourceWidth);
BufferedImagerotatedImage=newBufferedImage(sourceHeight,sourceWidth,BufferedImage.TYPE_BYTE_GRAY);
Graphics2Dg=rotatedImage.createGraphics();
g.drawImage(image,transform,null);
g.dispose();
intwidth=getWidth();
returnnewBufferedImageLuminanceSource(rotatedImage,top,sourceWidth-(left+width),getHeight(),width);
}
}
importcom.google.zxing.LuminanceSource;
importjava.awt.Graphics2D;
importjava.awt.geom.AffineTransform;
importjava.awt.image.BufferedImage;
publicfinalclassBufferedImageLuminanceSourceextendsLuminanceSource{
privatefinalBufferedImageimage;
privatefinalintleft;
privatefinalinttop;
publicBufferedImageLuminanceSource(BufferedImageimage){
this(image,0,0,image.getWidth(),image.getHeight());
}
publicBufferedImageLuminanceSource(BufferedImageimage,intleft,inttop,intwidth,intheight){
super(width,height);
intsourceWidth=image.getWidth();
intsourceHeight=image.getHeight();
if(left+width>sourceWidth||top+height>sourceHeight){
thrownewIllegalArgumentException("Croprectangledoesnotfitwithinimagedata.");
}
for(inty=top;yfor(intx=left;xif((image.getRGB(x,y)&0xFF000000)==0){
image.setRGB(x,y,0xFFFFFFFF);//=white
}
}
}
this.image=newBufferedImage(sourceWidth,sourceHeight,BufferedImage.TYPE_BYTE_GRAY);
this.image.getGraphics().drawImage(image,0,0,null);
this.left=left;
this.top=top;
}
@Override
publicbyte[]getRow(inty,byte[]row){
if(y<0||y>=getHeight()){
thrownewIllegalArgumentException("Requestedrowisoutsidetheimage:
"+y);
}
intwidth=getWidth();
if(row==null||row.lengthrow=newbyte[width];
}
image.getRaster().getDataElements(left,top+y,width,1,row);
returnrow;
}
@Override
publicbyte[]getMatrix(){
intwidth=getWidth();
intheight=getHeight();
intarea=width*height;
byte[]matrix=newbyte[area];
image.getRaster().getDataElements(left,top,width,height,matrix);
returnmatrix;
}
@Override
publicbooleanisCropSupported(){
returntrue;
}
@Override
publicLuminanceSourcecrop(intleft,inttop,intwidth,intheight){
returnnewBufferedImageLuminanceSource(image,this.left+left,this.top+top,width,height);
}
@Override
publicbooleanisRotateSupported(){
returntrue;
}
@Override
publicLuminanceSourcerotateCounterClockwise(){
intsourceWidth=image.getWidth();
intsourceHeight=image.getHeight();
AffineTransformtransform=newAffineTransform(0.0,-1.0,1.0,0.0,0.0,sourceWidth);
BufferedImagerotatedImage=newBufferedImage(sourceHeight,sourceWidth,BufferedIm