java Springboot 生成二维码

上码,如有问题或者优化,劳请广友下方留言

1.工具类
复制代码
importcom.google.zxing.BarcodeFormat;
importcom.google.zxing.EncodeHintType;
importcom.google.zxing.client.j2se.MatrixToImageConfig;
importcom.google.zxing.client.j2se.MatrixToImageWriter;
importcom.google.zxing.common.BitMatrix;
importcom.google.zxing.qrcode.QRCodeWriter;
importcom.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
importcom.sun.org.apache.xml.internal.security.utils.Base64;
importorg.springframework.util.ResourceUtils;
importjavax.imageio.ImageIO;
importjavax.servlet.ServletOutputStream;
importjava.awt.*;
importjava.awt.geom.RoundRectangle2D;
importjava.awt.image.BufferedImage;
importjava.io.ByteArrayOutputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.HashMap;
publicclassQrCodeUtil{
publicStringcreateQRCode(Stringcontent,intwidth,intheight,Stringpath)throwsIOException{
StringresultImage=””;
if(!EmptyUtils.isEmpty(content)){
ServletOutputStreamstream=null;
ByteArrayOutputStreamos=newByteArrayOutputStream();
@SuppressWarnings(“rawtypes”)
HashMap<EncodeHintType,Comparable>hints=newHashMap<>();
hints.put(EncodeHintType.CHARACTER_SET,”utf-8″);//指定字符编码为“utf-8”
hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);//指定二维码的纠错等级为中
hints.put(EncodeHintType.MARGIN,2);//指定二维码边距

java


try{
QRCodeWriterwriter=newQRCodeWriter();
BitMatrixbitMatrix=writer.encode(content,BarcodeFormat.QR_CODE,width,height,hints);
MatrixToImageConfigconfig=newMatrixToImageConfig(0xFF000001,0xFFFFFFFF);
BufferedImagebufferedImage=MatrixToImageWriter.toBufferedImage(bitMatrix,config);
//BufferedImagebufferedImage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//for(intx=0;x<width;x++){
//for(inty=0;y<height;y++){
//bufferedImage.setRGB(x,y,bitMatrix.get(x,y)?0xFF000000:0xFFFFFFFF);
//}
//}
Graphics2Dg2=bufferedImage.createGraphics();
g2.drawImage(ImageIO.read(ResourceUtils.getFile(path)),width/5*2,height/5*2,width/5,height/5,null);//logo.png自行设置
g2.setStroke(newBasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
g2.draw(newRoundRectangle2D.Float(width/5*2,height/5*2,width/5,height/5,20,20));
g2.dispose();
g2.setColor(Color.white);
g2.setBackground(Color.white);
bufferedImage.flush();
ImageIO.write(bufferedImage,”png”,os);
OutputStreamout=newFileOutputStream(“D:\logo\abb.png”);
out.write(os.toByteArray());
resultImage=newString(“data:image/png;base64,”+Base64.encode(os.toByteArray()));
returnresultImage;
}catch(Exceptione){
e.printStackTrace();
}finally{
if(stream!=null){
stream.flush();
stream.close();
}
}
}
returnnull;
}
}

© 版权声明
好牛新坐标
版权声明:
1、IT大王遵守相关法律法规,由于本站资源全部来源于网络程序/投稿,故资源量太大无法一一准确核实资源侵权的真实性;
2、出于传递信息之目的,故IT大王可能会误刊发损害或影响您的合法权益,请您积极与我们联系处理(所有内容不代表本站观点与立场);
3、因时间、精力有限,我们无法一一核实每一条消息的真实性,但我们会在发布之前尽最大努力来核实这些信息;
4、无论出于何种目的要求本站删除内容,您均需要提供根据国家版权局发布的示范格式
《要求删除或断开链接侵权网络内容的通知》:https://itdw.cn/ziliao/sfgs.pdf,
国家知识产权局《要求删除或断开链接侵权网络内容的通知》填写说明: http://www.ncac.gov.cn/chinacopyright/contents/12227/342400.shtml
未按照国家知识产权局格式通知一律不予处理;请按照此通知格式填写发至本站的邮箱 wl6@163.com

相关文章