Java利用Zxing生成二维码的简单实例
Zxing是Google提供的关于条码(一维码、二维码)的解析工具,提供了二维码的生成与解析的方法,现在我简单介绍一下使用Java利用Zxing生成与解析二维码
1、二维码的生成
1.1将Zxing-core.jar包加入到classpath下。
1.2二维码的生成需要借助MatrixToImageWriter类,该类是由Google提供的,可以将该类拷贝到源码中,这里我将该类的源码贴上,可以直接使用。
importcom.google.zxing.common.BitMatrix; importjavax.imageio.ImageIO; importjava.io.File; importjava.io.OutputStream; importjava.io.IOException; importjava.awt.image.BufferedImage; publicfinalclassMatrixToImageWriter{ privatestaticfinalintBLACK=0xFF000000; privatestaticfinalintWHITE=0xFFFFFFFF; privateMatrixToImageWriter(){} publicstaticBufferedImagetoBufferedImage(BitMatrixmatrix){ intwidth=matrix.getWidth(); intheight=matrix.getHeight(); BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB); for(intx=0;x<width;x++){ for(inty=0;y<height;y++){ 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编写生成二维码的实现代码
try{ Stringcontent="120605181003;http://www.cnblogs.com/jtmjx"; Stringpath="C:/Users/Administrator/Desktop/testImage"; MultiFormatWritermultiFormatWriter=newMultiFormatWriter(); Maphints=newHashMap(); hints.put(EncodeHintType.CHARACTER_SET,"UTF-8"); BitMatrixbitMatrix=multiFormatWriter.encode(content,BarcodeFormat.QR_CODE,400,400,hints); Filefile1=newFile(path,"餐巾纸.jpg"); MatrixToImageWriter.writeToFile(bitMatrix,"jpg",file1); }catch(Exceptione){ e.printStackTrace(); }
现在运行后即可生成一张二维码图片,是不是很简单啊?接下来我们看看如何解析二维码
2、二维码的解析
2.1将Zxing-core.jar包加入到classpath下。
2.2和生成一样,我们需要一个辅助类(BufferedImageLuminanceSource),同样该类Google也提供了,这里我同样将该类的源码贴出来,可以直接拷贝使用个,省去查找的麻烦
BufferedImageLuminanceSource 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;y<top+height;y++){ for(intx=left;x<left+width;x++){ if((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.length<width){ row=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); } }
2.3编写解析二维码的实现代码
try{ MultiFormatReaderformatReader=newMultiFormatReader(); StringfilePath="C:/Users/Administrator/Desktop/testImage/test.jpg"; Filefile=newFile(filePath); BufferedImageimage=ImageIO.read(file);; LuminanceSourcesource=newBufferedImageLuminanceSource(image); Binarizerbinarizer=newHybridBinarizer(source); BinaryBitmapbinaryBitmap=newBinaryBitmap(binarizer); Maphints=newHashMap(); hints.put(EncodeHintType.CHARACTER_SET,"UTF-8"); Resultresult=formatReader.decode(binaryBitmap,hints); System.out.println("result="+result.toString()); System.out.println("resultFormat="+result.getBarcodeFormat()); System.out.println("resultText="+result.getText()); }catch(Exceptione){ e.printStackTrace(); }
现在运行后可以看到控制台打印出了二维码的内容。
到此为止,利用Zxing生成和解析二维码就讲述演示完毕,主要为自己做备忘,同时方便有需要的人。呵呵!
以上这篇Java利用Zxing生成二维码的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。