您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 市场营销 > Java利用Zxing生成二维码(Domino也适用)
Zxing是Google提供的关于条码(一维码、二维码)的解析工具,提供了二维码的生成与解析的方法,现在我简单介绍一下使用Java利用Zxing生成与解析二维码Jar包下载:、二维码的生成1.1将Zxing-core.jar包加入到classpath下(如果是Domino平台,请参考本人的文库:=prin)。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;xwidth;x++){for(inty=0;yheight;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;=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也提供了,这里我同样将该类的源码贴出来,可以直接拷贝使用个,省去查找的麻烦BufferedImageLuminanceSourceimportcom.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+widthsourceWidth||top+heightsourceHeight){thrownewIllegalArgumentException(Croprectangledoesnotfitwithinimagedata.);}for(inty=top;ytop+height;y++){for(intx=left;xleft+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;}@Overridepublicbyte[]getRow(inty,byte[]row){if(y0||y=getHeight()){thrownewIllegalArgumentException(Requestedrowisoutsidetheimage:+y);}intwidth=getWidth();if(row==null||row.lengthwidth){row=newbyte[width];}image.getRaster().getDataElements(left,top+y,width,1,row);returnrow;}@Overridepublicbyte[]getMatrix(){intwidth=getWidth();intheight=getHeight();intarea=width*height;byte[]matrix=newbyte[area];image.getRaster().getDataElements(left,top,width,height,matrix);returnmatrix;}@OverridepublicbooleanisCropSupported(){returntrue;}@OverridepublicLuminanceSourcecrop(intleft,inttop,intwidth,intheight){returnnewBufferedImageLuminanceSource(image,this.left+left,this.top+top,width,height);}@OverridepublicbooleanisRotateSupported(){returntrue;}@OverridepublicLuminanceSourcerotateCounterClockwise(){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()
本文标题:Java利用Zxing生成二维码(Domino也适用)
链接地址:https://www.777doc.com/doc-5737736 .html