java使用jar包生成二维码的示例代码
使用java进行二维码的生成与读取使用到了谷歌的zxing.jar
第一步导入,maven依赖或者下载指定jar包
com.google.zxing javase 3.2.1
第二步书写二维码生成器的工具类
importjava.awt.Color;
importjava.io.File;
importjava.util.Hashtable;
importcom.google.zxing.EncodeHintType;
importcom.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
*QRCode生成器的格式
*
*@authorai(ahx.show)
*/
publicclassQRCodeFormat{
/**图片大小*/
privateintsize;
/**内容编码格式*/
privateStringencode;
/**错误修正等级(ErrorCollectionLevel)*/
privateErrorCorrectionLevelerrorCorrectionLevel;
/**错误修正等级的具体值*/
privatedoubleerrorCorrectionLevelValue;
/**前景色*/
privateColorforeGroundColor;
/**背景色*/
privateColorbackGroundColor;
/**图片的文件格式*/
privateStringimageFormat;
/**图片的外边距大小(QuietZone)*/
privateintmargin;
/**提供给编码器额外的参数*/
privateHashtablehints;
/**需要添加的图片*/
privateFileicon;
/**
*创建一个带有默认值的QRCode生成器的格式。默认值如下
*
*
*- 图片大小:256px
*- 内容编码格式:UTF-8
*- 错误修正等级:LevelM(有15%的内容可被修正)
*- 前景色:黑色
*- 背景色:白色
*- 输出图片的文件格式:png
*- 图片空白区域大小:0个单位
*
*
*@returnQRCode生成器格式
*/
publicstaticQRCodeFormatNEW(){
returnnewQRCodeFormat();
}
privateQRCodeFormat(){
this.size=256;
this.encode="utf-8";
this.errorCorrectionLevel=ErrorCorrectionLevel.M;
this.errorCorrectionLevelValue=0.15;
this.foreGroundColor=Color.BLACK;
this.backGroundColor=Color.WHITE;
this.imageFormat="png";
this.margin=0;
this.hints=newHashtable();
}
/**
*返回图片的大小。
*
*@return图片的大小
*/
publicintgetSize(){
returnthis.size;
}
/**
*设置图片的大小。图片的大小等于实际内容与外边距的值(建议设置成偶数值)。
*
*@paramsize
*图片的大小
*
*@returnQRCode生成器的格式
*/
publicQRCodeFormatsetSize(intsize){
this.size=size;
returnthis;
}
/**
*返回内容编码格式。
*
*@return内容编码格式
*/
publicStringgetEncode(){
returnencode;
}
/**
*设置内容编码格式。
*
*@paramencode
*内容编码格式
*
*@returnQRCode生成器的格式
*/
publicQRCodeFormatsetEncode(Stringencode){
this.encode=encode;
returnthis;
}
/**
*返回错误修正等级。
*
*@return错误修正等级
*/
publicErrorCorrectionLevelgetErrorCorrectionLevel(){
returnerrorCorrectionLevel;
}
/**
*返回错误修正等级的具体值。
*
*@return错误修正等级的具体值
*/
publicdoublegetErrorCorrectionLevelValue(){
returnerrorCorrectionLevelValue;
}
/**
*设置错误修正等级。其定义如下
*
*
*- L:有7%的内容可被修正
*- M:有15%的内容可被修正
*- Q:有25%的内容可被修正
*- H:有30%的内容可被修正
*
*
*@paramerrorCorrectionLevel
*错误修正等级
*
*@returnQRCode生成器的格式
*/
publicQRCodeFormatsetErrorCorrectionLevel(charerrorCorrectionLevel){
switch(Character.toUpperCase(errorCorrectionLevel)){
case'L':
this.errorCorrectionLevel=ErrorCorrectionLevel.L;
this.errorCorrectionLevelValue=0.07;
break;
case'M':
this.errorCorrectionLevel=ErrorCorrectionLevel.M;
this.errorCorrectionLevelValue=0.15;
break;
case'Q':
this.errorCorrectionLevel=ErrorCorrectionLevel.Q;
this.errorCorrectionLevelValue=0.25;
break;
case'H':
this.errorCorrectionLevel=ErrorCorrectionLevel.H;
this.errorCorrectionLevelValue=0.3;
break;
default:
this.errorCorrectionLevel=ErrorCorrectionLevel.M;
}
returnthis;
}
/**
*返回前景色。
*
*@return前景色
*/
publicColorgetForeGroundColor(){
returnforeGroundColor;
}
/**
*设置前景色。值为十六进制的颜色值(与CSS定义颜色的值相同,不支持简写),可以忽略「#」符号。
*
*@paramforeGroundColor
*前景色的值
*
*@returnQRCode生成器的格式
*/
publicQRCodeFormatsetForeGroundColor(StringforeGroundColor){
try{
this.foreGroundColor=getColor(foreGroundColor);
}
catch(NumberFormatExceptione){
this.foreGroundColor=Color.BLACK;
}
returnthis;
}
/**
*设置前景色。
*
*@paramforeGroundColor
*前景色的值
*
*@returnQRCode生成器的格式
*/
publicQRCodeFormatsetForeGroundColor(ColorforeGroundColor){
this.foreGroundColor=foreGroundColor;
returnthis;
}
/**
*返回背景色。
*
*@return背景色
*/
publicColorgetBackGroundColor(){
returnbackGroundColor;
}
/**
*设置背景色。值为十六进制的颜色值(与CSS定义颜色的值相同,不支持简写),可以忽略「#」符号。
*
*@parambackGroundColor
*前景色的值
*
*@returnQRCode生成器的格式
*/
publicQRCodeFormatsetBackGroundColor(StringbackGroundColor){
try{
this.backGroundColor=getColor(backGroundColor);
}
catch(NumberFormatExceptione){
this.backGroundColor=Color.WHITE;
}
returnthis;
}
/**
*设置背景色。
*
*@parambackGroundColor
*前景色的值
*
*@returnQRCode生成器的格式
*/
publicQRCodeFormatsetBackGroundColor(ColorbackGroundColor){
this.backGroundColor=backGroundColor;
returnthis;
}
/**
*返回图片的文件格式。
*
*@return图片的文件格式
*/
publicStringgetImageFormat(){
returnimageFormat.toUpperCase();
}
/**
*设置图片的文件格式。
*
*@paramimageFormat
*图片的文件格式
*
*@returnQRCode生成器的格式
*/
publicQRCodeFormatsetImageFormat(StringimageFormat){
this.imageFormat=imageFormat;
returnthis;
}
/**
*返回图片的外边距大小。
*
*@return图片的外边距大小
*/
publicintgetMargin(){
returnmargin;
}
/**
*设置图片的外边距大小。
*
*@parammargin
*图片的外边距大小
*
*@returnQRCode生成器的格式
*/
publicQRCodeFormatsetMargin(intmargin){
this.margin=margin;
returnthis;
}
/**
*返回提供给编码器额外的参数。
*
*@return提供给编码器额外的参数
*/
publicHashtablegetHints(){
hints.clear();
hints.put(EncodeHintType.ERROR_CORRECTION,getErrorCorrectionLevel());
hints.put(EncodeHintType.CHARACTER_SET,getEncode());
hints.put(EncodeHintType.MARGIN,getMargin());
returnhints;
}
/**
*返回添加的图片。
*
*@return添加的图片
*/
publicFilegetIcon(){
returnicon;
}
/**
*设置添加的图片。
*
*@paramicon
*添加的图片
*
*@returnQRCode生成器的格式
*/
publicQRCodeFormatsetIcon(Fileicon){
this.icon=icon;
returnthis;
}
/**
*设置添加的图片。
*
*@paramiconPath
*添加的图片
*
*@returnQRCode生成器的格式
*/
publicQRCodeFormatsetIcon(StringiconPath){
returnsetIcon(newFile(iconPath));
}
privateColorgetColor(StringhexString){
if(hexString.charAt(0)=='#'){
returnnewColor(Long.decode(hexString).intValue());
}else{
returnnewColor(Long.decode("0xFF"+hexString).intValue());
}
}
}
第三步使用生成器对象按照指定格式进行生成读取二维码
importjava.awt.AlphaComposite;
importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.Graphics2D;
importjava.awt.RenderingHints;
importjava.awt.color.ColorSpace;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.net.MalformedURLException;
importjava.net.URL;
importjava.nio.charset.Charset;
importjavax.imageio.ImageIO;
importcom.google.zxing.BarcodeFormat;
importcom.google.zxing.BinaryBitmap;
importcom.google.zxing.ChecksumException;
importcom.google.zxing.FormatException;
importcom.google.zxing.LuminanceSource;
importcom.google.zxing.NotFoundException;
importcom.google.zxing.Result;
importcom.google.zxing.WriterException;
importcom.google.zxing.client.j2se.BufferedImageLuminanceSource;
importcom.google.zxing.common.BitMatrix;
importcom.google.zxing.common.HybridBinarizer;
importcom.google.zxing.qrcode.QRCodeReader;
importcom.google.zxing.qrcode.QRCodeWriter;
/**
*QRCode处理器
*@ClassName:QRCode
*@Description:TODO
*@author:ai(ahx.show)
*@date:2016年12月18日下午1:22:50
*/
publicfinalclassQRCode{
/**QRCode生成器格式*/
privateQRCodeFormatformat=null;
/**生成的QRCode图像对象*/
privateBufferedImageqrcodeImage=null;
/**生成的QRCode图片文件*/
privateFileqrcodeFile=null;
/**
*返回生成的QRCode图像对象
*
*@return生成的QRCode图像对象
*/
publicBufferedImagegetQrcodeImage(){
returnqrcodeImage;
}
/**
*返回生成的QRCode图片文件
*
*@return生成的QRCode图片文件
*/
publicFilegetQrcodeFile(){
returnqrcodeFile;
}
privateQRCode(){
}
/**
*使用带默认值的「QRCode生成器格式」来创建一个QRCode处理器。
*
*@paramcontent
*所要生成QRCode的内容
*
*@returnQRCode处理器
*/
publicstaticQRCodeNEW(finalStringcontent){
returnNEW(content,QRCodeFormat.NEW());
}
/**
*使用指定的「QRCode生成器格式」来创建一个QRCode处理器。
*
*@paramcontent
*所要生成QRCode的内容
*@paramformat
*QRCode生成器格式
*
*@returnQRCode处理器
*/
publicstaticQRCodeNEW(finalStringcontent,QRCodeFormatformat){
QRCodeqrcode=newQRCode();
qrcode.format=format;
qrcode.qrcodeImage=toQRCode(content,format);
returnqrcode;
}
/**
*把指定的内容生成为一个QRCode的图片,之后保存到指定的文件中。
*
*@paramf
*指定的文件
*
*@returnQRCode处理器
*/
publicQRCodetoFile(Stringf){
returntoFile(newFile(f),this.format.getIcon());
}
/**
*把指定的内容生成为一个QRCode的图片,之后保存到指定的文件中。
*
*@paramqrcodeFile
*指定的文件
*
*@returnQRCode处理器
*/
publicQRCodetoFile(FileqrcodeFile){
returntoFile(qrcodeFile,this.format.getIcon());
}
/**
*把指定的内容生成为一个QRCode的图片,并在该图片中间添加上指定的图片;之后保存到指定的文件内。
*
*@paramqrcodeFile
*QRCode图片生成的指定的文件
*@paramappendFile
*需要添加的图片。传入的文件路径如果没有(null或者为空)的时候将忽略该参数
*
*@returnQRCode处理器
*/
publicQRCodetoFile(StringqrcodeFile,StringappendFile){
if(null==appendFile||appendFile.length()==0){
returntoFile(newFile(qrcodeFile));
}
returntoFile(newFile(qrcodeFile),newFile(appendFile));
}
/**
*把指定的内容生成为一个QRCode的图片,并在该图片中间添加上指定的图片;之后保存到指定的文件内。
*
*@paramqrcodeFile
*QRCode图片生成的指定的文件
*@paramappendFile
*需要添加的图片。传入的图片不存在的时候将忽略该参数
*
*@returnQRCode处理器
*/
publicQRCodetoFile(FileqrcodeFile,FileappendFile){
try{
if(!qrcodeFile.exists()){
qrcodeFile.getParentFile().mkdirs();
qrcodeFile.createNewFile();
}
if(null!=appendFile&&appendFile.isFile()&&appendFile.length()!=0){
appendImage(ImageIO.read(appendFile));
}
if(!ImageIO.write(this.qrcodeImage,getSuffixName(qrcodeFile),qrcodeFile)){
thrownewRuntimeException("Unexpectederrorwritingimage");
}
}
catch(IOExceptione){
thrownewRuntimeException(e);
}
this.qrcodeFile=qrcodeFile;
returnthis;
}
privatevoidappendImage(BufferedImageappendImage){
appendImage(this.qrcodeImage,appendImage,this.format);
}
privatestaticvoidappendImage(BufferedImageqrcodeImage,BufferedImageappendImage,QRCodeFormatformat){
intbaseWidth=qrcodeImage.getWidth();
intbaseHeight=qrcodeImage.getHeight();
//计算icon的最大边长
//公式为二维码面积*错误修正等级*0.4的开方
intmaxWidth=(int)Math.sqrt(baseWidth*baseHeight*format.getErrorCorrectionLevelValue()*0.4);
intmaxHeight=maxWidth;
//获取icon的实际边长
introundRectWidth=(maxWidth
第四步使用
工具类中的方法使用的静态方法,可以直接使用QRCode.方法进行执行
生成二维码方法
QRCode.NEW(str).toFile(url);
str:二维码中包含的字符串(如果包含地址前缀添加http或https否则不能自动跳转会解析地址字符串)
url:二维码图片生成位置
QRCode.from(url);
url:要解析二维码图片位置
到此这篇关于java使用jar包生成二维码的示例代码的文章就介绍到这了,更多相关javajar包生成二维码内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。