zoukankan      html  css  js  c++  java
  • java QR Code 方式生成二维码的实例

    生成二维码的方式很多种,这里我只是贴出我根据百度,自己整理的 用java qr code 方式生成二维码

    一、普通二维码

    public QrCodeUtils{
    
    /**
         * 生成二维码图片
         *
         * @param content
         */
        public static void createQrCode(String content) {
    
            try {
                Qrcode qrcode = new Qrcode();
                // 设置二维码排错率, L(7%)、M(15%)、Q(25%)、H(30%) 排错率越高可存储的信息越少,但对二维码的清晰度要求比较低
                qrcode.setQrcodeErrorCorrect('M');
                // N代表数字,A代表字符a-Z,B代表其他字符
                qrcode.setQrcodeEncodeMode('B');
                // 设置二维码版本,取值范围为0-40,值越大尺寸越大,存储的信息越大
                qrcode.setQrcodeVersion(7);
    
                // 设置图片高宽度
                BufferedImage bufferedImage = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB);
    
                // 创建一个 Graphics2D,画图
                Graphics2D gs = bufferedImage.createGraphics();
                // 设置图片的背景色
                gs.setBackground(Color.WHITE);
                gs.clearRect(0, 0, 139, 139);
    
                // 设置图片颜色
                gs.setColor(Color.black);
    
                // 将输出内容转为byte类型
                byte[] contentBytes = content.getBytes("utf-8");
    
                // 设置偏移量,不设置可能导致解析出错
                int pixoff = 2;
    
                // 输出内容
                if (contentBytes.length > 0 && contentBytes.length < 130) {
                    boolean[][] codeOut = qrcode.calQrcode(contentBytes);
                    for (int i = 0; i < codeOut.length; i++) {
                        for (int j = 0; j < codeOut.length; j++) {
                            if (codeOut[j][i]) {
                                gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
                            }
                        }
                    }
                } else {
                    System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,130 ]. ");
                    return;
                }
    
                // 没有logo的二维码
                gs.dispose();
                bufferedImage.flush();
    
    
    //            ImageIO.write(bufImg, "jpg", response.getOutputStream());  生成图片输出流中
    
                // 生成二维码QRCode图片
                File imgFile = new File("D:/qrCode/");
                // 生成的图片在D盘下,名为 qrCode.png
                ImageIO.write(bufferedImage, "png", imgFile);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }

      public static void main(String[] args) {
         QrCodeUtils.createQrCode("123456789_jo");
        }
    }

    二 、生成一个带logo的二维码

    public QrCodeLogoUtil{
     public static void createLogoQrcode(String content, String logoPath) {
            try {
    
                Qrcode logoQrCode = new Qrcode();
                logoQrCode.setQrcodeErrorCorrect('M');
                logoQrCode.setQrcodeEncodeMode('B');
                logoQrCode.setQrcodeVersion(7);
    
                BufferedImage bufferedImage = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB);
                Graphics2D gs = bufferedImage.createGraphics();
                gs.setBackground(Color.WHITE);
                gs.setColor(Color.BLUE);
                gs.clearRect(0, 0, 139, 139);
    
                byte[] contents = content.getBytes("utf-8");
    
                int opx = 2;
                if (contents.length > 0 && contents.length < 130) {
                    boolean[][] codeOut = logoQrCode.calQrcode(contents);
                    for (int i = 0; i < codeOut.length; i++) {
                        for (int j = 0; j < codeOut.length; j++) {
                            if (codeOut[j][i]) {
                                gs.fillRect(j * 3 + opx, i * 3 + opx, 3, 3);
                            }
                        }
                    }
                } else {
                    System.err.println("QRCode content bytes length = " + contents.length + " not in [ 0,130 ]. ");
                    return;
                }
    
                // logo图片高度
                Image logo = ImageIO.read(new File(logoPath));
                int widthLogo = logo.getWidth(null) > bufferedImage.getWidth() * 2 / 10 ? (bufferedImage.getWidth() * 2 / 10) : logo.getWidth(null);
                int heightLogo = logo.getHeight(null) > bufferedImage.getHeight() * 2 / 10 ? (bufferedImage.getHeight() * 2 / 10) : logo.getWidth(null);
    
                // 设置logo图片的位置
                int x = (bufferedImage.getWidth() - widthLogo) / 2;
                int y = (bufferedImage.getHeight() - heightLogo) / 2;
    
                gs.drawImage(logo,x,y,widthLogo,heightLogo,null);
    
                gs.dispose();
                bufferedImage.flush();
                // 生成二维码QRCode图片
                File imgFile = new File("D:/logoQrCode/");
                // 生成的图片在D盘下,名为 qrCode.png
                ImageIO.write(bufferedImage, "png", imgFile);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
       public static void main(String[] args) {
    QrCodeUtils.createLogoQrcode("liTing_232323","D:/picture/logo_qrcode.jpg");
    }

       }

    以上,二维码生成的位置,是自己配置的 。

    带logo二维码生成代码中,logo的图片是我本地的图片。

    以上代码生成的二维码,可以用 微信的扫一扫功能, 可以得到你设置的content值。

  • 相关阅读:
    HDU-2262 Where is the canteen 概率DP,高斯消元
    HDU-4418 Time travel 概率DP,高斯消元
    无人驾驶相关数据集
    C++——编译器运行过程
    C++——Struct 和 Union区别
    常用linux指令
    无人驾驶——定位
    Ubuntu 没有 无线网 RTL8821ce 8111 8186
    无人驾驶之传感器融合算法
    LIN通讯
  • 原文地址:https://www.cnblogs.com/jxwy/p/7463335.html
Copyright © 2011-2022 走看看