欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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     }