详解ZXing-core生成二维码的方法并解析
二维码无处不在,扫一扫有礼品哦,现在二维码这么流行,想必大家不是很清楚二维码是怎么生成的吧,现在小编通过给大家分享本文帮助大家学习二维码生成的方法。
其实主要是利用goggle发布的jar来使用的此功能。
1、二维码的生成
将Zxing-core.jar包加入到classpath下。
二维码的生成需要借助MatrixToImageWriter类,该类是由Google提供的,可以将该类拷贝到源码中,这里我将该类的源码贴上,可以直接使用。
直接可以生成二维码的代码
publicvoidtest1()throwsException{ Stringcontent="www.baidu.com"; Stringpath="d://"; MultiFormatWritermultiFormatWriter=newMultiFormatWriter();//Zxing是Google提供的关于条码 Maphints=newHashMap(); hints.put(EncodeHintType.CHARACTER_SET,"UTF-8"); BitMatrixbitMatrix=multiFormatWriter.encode(content,BarcodeFormat.QR_CODE,400,400,hints);//这里是照片的大小 Filefile1=newFile(path,"my.jpg"); MatrixToImageWriter.writeToFile(bitMatrix,"jpg",file1); System.out.println("执行完毕"); }
当我们能发现,这个代码拷贝上后发现有一个MatrixToImageWriter报错,所以需要我们去找,但是这里我贴出代码,可以直接使用。
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); } } }
现在就可以d盘的根目录下载看生成的二维码了
二维码解析
和生成一样,我们需要一个辅助类(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); } }
解析二维码的代码
MultiFormatReaderformatReader=newMultiFormatReader(); StringfilePath="图片的路径"; 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()); tch(Exceptione){ e.printStackTrace();
现在这样可以在控制台看到二维码的内容。
以上所述是小编给大家介绍的ZXing-core生成二维码的方法并解析的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!