Java使用Zxing包制作二维码.docx
- 文档编号:5978986
- 上传时间:2023-01-02
- 格式:DOCX
- 页数:12
- 大小:17.04KB
Java使用Zxing包制作二维码.docx
《Java使用Zxing包制作二维码.docx》由会员分享,可在线阅读,更多相关《Java使用Zxing包制作二维码.docx(12页珍藏版)》请在冰豆网上搜索。
Java使用Zxing包制作二维码
域名长度后缀删除日期删除类型
33d.cc3cc2012/9/4Delete今天朋友问我一个二维码的东西,说实话我重来也没接触过,于是上网找了一下,和朋友分享一下...
用qrcode.jar也能做,但是一看小日本的,于是马上闪过,听所zxing挺好用的,于是就去google下了架包
用了这个感觉确实简单,重XX找到写完一个例子只用了,10来分钟........//www.hake.cc
Zxing是Google提供的关于条码(一维码、二维码)的解析工具,提供了二维码的生成与解析的方法,现在我简单介绍一下使用Java利用Zxing生成与解析二维码
1、二维码的生成
1.1将core.jar包加入到classpath下。
1.2二维码的生成需要借助MatrixToImageWriter类,该类是由Google提供的,可以将该类拷贝到源码中,这里我将该类的源码贴上,可以直接使用。
[java]viewplaincopyprint?
importmon.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 for(inty=0;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); } } } importmon.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 for(inty=0;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编写生成二维码的实现代码//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;y for(intx=left;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 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); } } 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 for(intx=left;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 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,BufferedIm
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 使用 Zxing 制作 二维码
![提示](https://static.bdocx.com/images/bang_tan.gif)