java二维码生成的方法
本文实例为大家分享了java二维码的实现代码,供大家参考,具体内容如下
这次用到的jar包是zxing,没有用到core的jar包
先导入zxing.jar包
生成二维码
packagecn.huse.erweima; importjava.io.File; importjava.util.HashMap; importcom.google.zxing.BarcodeFormat; importcom.google.zxing.EncodeHintType; importcom.google.zxing.MultiFormatWriter; importcom.google.zxing.client.j2se.MatrixToImageWriter; importcom.google.zxing.common.BitMatrix; importcom.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /** *生成二维码 * */ publicclassCreateQRCode{ publicstaticvoidmain(String[]args){ intwidth=300; intheight=300; Stringformat="gif"; Stringcontent="www.baidu.com"; //定义二维码的参数 HashMaphints=newHashMap<>(); hints.put(EncodeHintType.CHARACTER_SET,"utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M); try{ BitMatrixmatrix=newMultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height,hints); Filefile=newFile("e:"+File.separator+"new.gif"); MatrixToImageWriter.writeToFile(matrix,format,file); }catch(Exceptione){ e.printStackTrace(); } } }
解析二维码
packagecn.huse.erweima; importjava.awt.image.BufferedImage; importjava.io.File; importjava.io.IOException; importjava.util.HashMap; importjavax.imageio.ImageIO; importcom.google.zxing.BinaryBitmap; importcom.google.zxing.EncodeHintType; importcom.google.zxing.MultiFormatReader; importcom.google.zxing.NotFoundException; importcom.google.zxing.Result; importcom.google.zxing.client.j2se.BufferedImageLuminanceSource; importcom.google.zxing.common.HybridBinarizer; //解析二维码 publicclassReadQRCode{ publicstaticvoidmain(String[]args){ MultiFormatReaderformatReader=newMultiFormatReader(); Filefile=newFile("e:"+File.separator+"new.gif"); try{ BufferedImageimage=ImageIO.read(file); BinaryBitmapbinaryBitmap=newBinaryBitmap(newHybridBinarizer(newBufferedImageLuminanceSource(image))); HashMaphints=newHashMap<>(); hints.put(EncodeHintType.CHARACTER_SET,"utf-8"); Resultresult=formatReader.decode(binaryBitmap,hints); System.out.println(result.toString()); System.out.println(result.getBarcodeFormat()); System.out.println(result.getText()); }catch(NotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。