相关文章推荐
越狱的刺猬  ·  滕白莹·  7 月前    · 
温暖的煎饼  ·  吉林普法网·  1 年前    · 
英俊的刺猬  ·  天狮集团董事长李金元“消失”的一年半:权健倒 ...·  1 年前    · 
完美的苦瓜  ·  mac vscode 设置字体大小-掘金·  2 年前    · 
温柔的酱牛肉  ·  漫剧_动漫大全_动画在线观看_快看漫画·  2 年前    · 
Code  ›  Java生成二维码开发者社区
string 二维码 qrcode
https://cloud.tencent.com/developer/article/1431928
强健的豆浆
2 年前
作者头像
JAVA葵花宝典
0 篇文章

Java生成二维码

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > JAVA葵花宝典 > Java生成二维码

Java生成二维码

作者头像
JAVA葵花宝典
发布 于 2019-05-24 20:40:28
720 0
发布 于 2019-05-24 20:40:28
举报

作者:许你一世流离 原文:https://blog.csdn.net/weixin_39936341/article/details/82910051

1,下载jar包(QRCode.jar)

maven依赖

<dependency>   <groupId>QRCode</groupId>   <artifactId>QRCode</artifactId>   <version>3.0</version></dependency>

2,编写实体类实现二维码的生成

二维码工具类

public class CreateQRCode {
    /**     * 创建二维码     * @param qrData 生成二维码中要存储的信息     * @param path   二维码图片存储路径 eg:"D:/qrcode.png"     * @throws Exception     */    public static boolean creatQrcode(String qrData, String path) {        try {            Qrcode qrcode = new Qrcode();            qrcode.setQrcodeErrorCorrect('M');//纠错等级(分为L、M、H三个等级)            qrcode.setQrcodeEncodeMode('B');//N代表数字,A代表a-Z,B代表其它字符            qrcode.setQrcodeVersion(7);//版本
            //设置一下二维码的像素            int width = 67 + 12 * (7 - 1);            int height = 67 + 12 * (7 - 1);            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);            //绘图            Graphics2D gs = bufferedImage.createGraphics();            gs.setBackground(Color.WHITE);            gs.setColor(Color.BLACK);            gs.clearRect(0, 0, width, height);//清除下画板内容
            //设置下偏移量,如果不加偏移量,有时会导致出错。            int pixoff = 2;
            byte[] d = qrData.getBytes("utf-8");            if (d.length > 0 && d.length < 120) {                boolean[][] s = qrcode.calQrcode(d);                for (int i = 0; i < s.length; i++) {                    for (int j = 0; j < s.length; j++) {                        if (s[j][i]) {                            gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);                        }                    }                }            }            gs.dispose();            bufferedImage.flush();            ImageIO.write(bufferedImage, "png", new File(path));            return true;        } catch (IOException e) {            e.printStackTrace();            return false;        }    }
    /**     * 解析二维码(QRCode)     *     * @param imgPath 图片路径     * @return     */    public static String decoderQRCode(String imgPath) {        //QRCode 二维码图片的文件        File imageFile = new File(imgPath);        BufferedImage bufImg = null;        String content = null;        try {            bufImg = ImageIO.read(imageFile);            QRCodeDecoder decoder = new QRCodeDecoder();            content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8");        } catch (IOException e) {            System.out.println("Error: " + e.getMessage());            e.printStackTrace();        } catch (DecodingFailedException dfe) {            System.out.println("Error: " + dfe.getMessage());            dfe.printStackTrace();        }        return content;    }
}

二维码基础类

class TwoDimensionCodeImage implements QRCodeImage {    //BufferedImage作用将一幅图片加载到内存中    BufferedImage bufImg;    public TwoDimensionCodeImage(BufferedImage bufImg) {        this.bufImg = bufImg;    }
    @Override    public int getWidth() {        return bufImg.getWidth();//返回像素宽度    }
    @Override    public int getHeight() {        return bufImg.getHeight();//返回像素高度    }
    @Override    public int getPixel(int i, int i1) {        return bufImg.getRGB(i, i1);//得到长宽值,即像素值,i,i1代表像素值    }}

3.controller调用

package com.st.project.controller;
import com.st.project.common.AjaxResult;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import static com.st.project.common.CreateQRCode.creatQrcode;import static com.st.project.common.CreateQRCode.decoderQRCode;
/** * 创建二维码 */@Controller@RequestMapping("/qrcode")public class QrcodeController {    @Value("${portals.upload.image.path}")    private String qrcodePath; //二维码存储路径
    /**     * 创建二维码     * @return     */    @ResponseBody    @PostMapping("/add.dd")    public AjaxResult addQrcode(HttpServletRequest request){        AjaxResult ajaxResult = new AjaxResult();        ajaxResult.setState(false);        String qrData=request.getParameter("qrData");        String qrSuffix=request.getParameter("qrSuffix");        String qrcode=System.currentTimeMillis()+"."+qrSuffix;        String path=qrcodePath+qrcode;        boolean getQrcode=creatQrcode(qrData,path);        if(getQrcode==true){            ajaxResult.setState(true);            ajaxResult.setData(qrcode);        }        return ajaxResult;    }
    /**     * 解析二维码     * @return     */    @ResponseBody    @PostMapping("/decoder.dd")    public AjaxResult decoderQrcode(HttpServletRequest request){        AjaxResult ajaxResult = new AjaxResult();        ajaxResult.setState(false);        String qrcode=request.getParameter("qrcode");
 
推荐文章
越狱的刺猬  ·  滕白莹
7 月前
温暖的煎饼  ·  吉林普法网
1 年前
英俊的刺猬  ·  天狮集团董事长李金元“消失”的一年半:权健倒了,华堂被拆
1 年前
完美的苦瓜  ·  mac vscode 设置字体大小-掘金
2 年前
温柔的酱牛肉  ·  漫剧_动漫大全_动画在线观看_快看漫画
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号