Java生成二维码的实例代码
使用开源的一维/二维码图形处理库zxingGitHub地址
引入依赖
com.google.zxing core 3.3.0 com.google.zxing javase 3.3.0
封装工具类
packagecom.app.utils;
importjava.awt.Color;
importjava.awt.Graphics2D;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.util.HashMap;
importjava.util.Map;
importjavax.imageio.ImageIO;
importcom.google.zxing.BarcodeFormat;
importcom.google.zxing.EncodeHintType;
importcom.google.zxing.MultiFormatWriter;
importcom.google.zxing.client.j2se.MatrixToImageConfig;
importcom.google.zxing.client.j2se.MatrixToImageWriter;
importcom.google.zxing.common.BitMatrix;
importcom.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
*@title生成二维码工具类
*@authorzch
*@discribtion
*@Date2020年1月3日下午4:26:05
*@visionV1.0
*/
publicclassQRCodeUtil
{
privatestaticfinalintwidth=200;//图像宽度
privatestaticfinalintheight=200;//图像高度
privatestaticfinalintON_COLOR=0xFF000001;
privatestaticfinalintOFF_COLOR=0xFFFFFFFF;
/**
*@title生成二维码图片
*@discribtion
*@authorzch
*@Date2020年1月3日下午3:27:21
*@paramwidth二维码宽度,默认为200
*@paramheight二维码高度,默认为200
*@paramcontent二维码内容,必填
*@paramlogoPathlogo图片路径,若为空则生成不带logo的二维码
*@paramimgPath生成二维码文件夹路径
*@paramimgName生成二维码图片名称,必填
*@paramsuffix生成二维码图片后缀类型,例如:gif,必填
*@visionV1.0
*/
publicstaticbooleangenerateQRImage(Integerwidth,Integerheight,Stringcontent,StringlogoPath,StringimgPath,StringimgName,Stringsuffix)
{
if(content==null||imgName==null||suffix==null)
{
returnfalse;
}
try
{
width=width==null?QRCodeUtil.width:width;
height=height==null?QRCodeUtil.height:height;
if(logoPath!=null&&!"".equals(logoPath.trim()))
{
QREncode(width,height,content,logoPath,imgPath,imgName,suffix);
}
else
{
QREncode(width,height,content,imgPath,imgName,suffix);
}
returntrue;
}
catch(Exceptione)
{
e.printStackTrace();
returnfalse;
}
}
/**
*@title生成二维码
*@discribtion
*@authorzch
*@Date2020年1月3日下午3:27:21
*@visionV1.0
*/
privatestaticvoidQREncode(intwidth,intheight,Stringcontent,StringimgPath,StringimgName,Stringsuffix)
throwsException
{
FilefilePath=newFile(imgPath);
if(!filePath.exists())
{
filePath.mkdirs();
}
FileimageFile=newFile(imgPath,imgName);
Maphints=newHashMap<>();
//内容编码格式
hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
//指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.H);
//设置二维码边的空度,非负数
hints.put(EncodeHintType.MARGIN,1);
BitMatrixbitMatrix=newMultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height,hints);
MatrixToImageWriter.writeToPath(bitMatrix,suffix,imageFile.toPath());//输出原图片
}
/**
*@title生成带logo的二维码
*@discribtion
*@authorzch
*@Date2020年1月3日下午3:27:21
*@visionV1.0
*/
privatestaticvoidQREncode(intwidth,intheight,Stringcontent,StringlogoPath,StringimgPath,StringimgName,Stringsuffix)
throwsException
{
FilefilePath=newFile(imgPath);
if(!filePath.exists())
{
filePath.mkdirs();
}
FileimageFile=newFile(imgPath,imgName);
Maphints=newHashMap<>();
//内容编码格式
hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
//指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.H);
//设置二维码边的空度,非负数
hints.put(EncodeHintType.MARGIN,1);
BitMatrixbitMatrix=newMultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height,hints);
MatrixToImageConfigmatrixToImageConfig=newMatrixToImageConfig(ON_COLOR,OFF_COLOR);
BufferedImagebufferedImage=LogoMatrix(MatrixToImageWriter.toBufferedImage(bitMatrix,matrixToImageConfig),newFile(logoPath));
ImageIO.write(bufferedImage,suffix,imageFile);//输出带logo图片
}
/**
*@title二维码图片添加logo
*@discribtion
*@authorzch
*@Date2020年1月3日下午3:27:21
*@parammatrixImage源二维码图片
*@paramlogoFilelogo图片
*@visionV1.0
*/
privatestaticBufferedImageLogoMatrix(BufferedImagematrixImage,FilelogoFile)
throwsIOException
{
//读取二维码图片,并构建绘图对象
Graphics2Dgs=matrixImage.createGraphics();
intmatrixWidth=matrixImage.getWidth();
intmatrixHeigh=matrixImage.getHeight();
intratioWidth=matrixWidth*2/10;
intratioHeight=matrixHeigh*2/10;
//读取Logo图片
BufferedImagelogo=ImageIO.read(logoFile);
intlogoWidth=logo.getWidth(null)>ratioWidth?ratioWidth:logo.getWidth(null);
intlogoHeight=logo.getHeight(null)>ratioHeight?ratioHeight:logo.getHeight(null);
intx=(matrixWidth-logoWidth)/2;
inty=(matrixHeigh-logoHeight)/2;
//绘制
gs.drawImage(logo,x,y,logoWidth,logoHeight,null);
gs.setColor(Color.BLACK);
gs.setBackground(Color.WHITE);
gs.dispose();
matrixImage.flush();
returnmatrixImage;
}
}
测试生成二维码
QRCodeUtil.generateQRImage(null,null,"https://blog.csdn.net/qq_34928194",null,"E:/","test.gif","gif");
以上就是Java生成二维码的实例代码的详细内容,更多关于Java生成二维码的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。