java Springboot 生成 二维码 +logo
程序员文章站
2023-10-16 21:57:17
上码,如有问题或者优化,劳请广友下方留言 1.工具类 1 @RequestMapping("/qrcode") 2 public String qrcode(HttpServletRequest request, HttpServletResponse response) { 3 try { 4 Q ......
上码,如有问题或者优化,劳请广友下方留言
1.工具类
import com.google.zxing.barcodeformat; import com.google.zxing.encodehinttype; import com.google.zxing.client.j2se.matrixtoimageconfig; import com.google.zxing.client.j2se.matrixtoimagewriter; import com.google.zxing.common.bitmatrix; import com.google.zxing.qrcode.qrcodewriter; import com.google.zxing.qrcode.decoder.errorcorrectionlevel; import com.sun.org.apache.xml.internal.security.utils.base64; import org.springframework.util.resourceutils; import javax.imageio.imageio; import javax.servlet.servletoutputstream; import java.awt.*; import java.awt.geom.roundrectangle2d; import java.awt.image.bufferedimage; import java.io.bytearrayoutputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.outputstream; import java.util.hashmap; public class qrcodeutil { public string createqrcode(string content,int width,int height,string path) throws ioexception { string resultimage=""; if(!emptyutils.isempty(content)){ servletoutputstream stream=null; bytearrayoutputstream os=new bytearrayoutputstream(); @suppresswarnings("rawtypes") hashmap<encodehinttype,comparable> hints=new hashmap<>(); hints.put(encodehinttype.character_set,"utf-8");//指定字符编码为“utf-8” hints.put(encodehinttype.error_correction, errorcorrectionlevel.m);//指定二维码的纠错等级为中 hints.put(encodehinttype.margin,2);//指定二维码边距 try { qrcodewriter writer=new qrcodewriter(); bitmatrix bitmatrix=writer.encode(content, barcodeformat.qr_code,width,height,hints); matrixtoimageconfig config = new matrixtoimageconfig(0xff000001, 0xffffffff); bufferedimage bufferedimage= matrixtoimagewriter.tobufferedimage(bitmatrix,config); // bufferedimage bufferedimage = new bufferedimage(width, height, bufferedimage.type_int_rgb); // for (int x = 0; x < width; x++) { // for (int y = 0; y < height; y++) { // bufferedimage.setrgb(x, y, bitmatrix.get(x, y) ? 0xff000000 : 0xffffffff); // } // } graphics2d g2 = bufferedimage.creategraphics(); g2.drawimage(imageio.read(resourceutils.getfile(path)), width / 5 * 2, height / 5 * 2, width / 5, height / 5, null); // logo.png自行设置 g2.setstroke(new basicstroke(5, basicstroke.cap_round, basicstroke.join_round)); g2.draw(new roundrectangle2d.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); outputstream out = new fileoutputstream("d:\\logo\\abb.png"); out.write(os.tobytearray()); resultimage=new string("data:image/png;base64,"+ base64.encode(os.tobytearray())); return resultimage; }catch (exception e){ e.printstacktrace(); }finally { if(stream!=null){ stream.flush(); stream.close(); } } } return null; } }
2.controlelr
1 @requestmapping("/qrcode") 2 public string qrcode(httpservletrequest request, httpservletresponse response) { 3 try { 4 qrcodeutil qrcodeutil=new qrcodeutil(); 5 string path= resourceutils.geturl("classpath:").getpath()+"logo/logo.png"; 6 return qrcodeutil.createqrcode("https://www.cnblogs.com/xikui/",300,300,path); 7 } catch (ioexception e) { 8 e.printstacktrace(); 9 return "异常"; 10 } 11 }
上一篇: Java super关键字
推荐阅读
-
asp.net(C#)使用QRCode生成图片中心加Logo或图像的二维码实例
-
使用PHP生成二维码的两种方法(带logo图像)
-
建议收藏备用:.net core使用QRCoder生成普通二维码和带Logo的二维码详细使用教程,源码已更新至开源模板
-
通过java生成读取二维码详解
-
java实现批量生成二维码
-
利用phpqrcode二维码生成类库合成带logo的二维码并且用合成的二维码生成海报(二)
-
java Springboot 生成 二维码 +logo
-
PHP基于phpqrcode生成带LOGO图像的二维码实例
-
C#生成带logo的二维码
-
Java 生成微信小程序二维码并写入本地(经过反复测试可用)