志在指尖
用双手敲打未来

java Springboot 生成 二维码 +logo

上码,如有问题或者优化,劳请广友下方留言
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;
}
}

未经允许不得转载:IT技术网站 » java Springboot 生成 二维码 +logo
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载