• 二维码的生成--后台版


    描述:Java生成二维码主要有两种方式,其实就是引用两种不同的jar

    通过Qrcode生成二维码

    引入jar包

    <dependency>
        <groupId>com.uqihong</groupId>
        <artifactId>qdcode</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.uqihong</groupId>
        <artifactId>qdcodeSwetake</artifactId>
        <version>1.0.0</version>
    </dependency>
    

    通用代码

    public void encoderQRCode(String content, String imgPath) {//content表示二维码中需要展示的内容,一般为一个连接;imgPath表示二维码生成后输出的路径,格式例如:D:/dev/qrcode/图片名称.png
    		try {
    			Qrcode qrcodeHandler = new Qrcode();
    			// 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小 
                            //特别注意:这里的L,M,Q,H的设置直接影响着content中所包含的字符,百分比越小,content中所能包含的字符越多,但是上限是多少没有测过
    			qrcodeHandler.setQrcodeErrorCorrect('L');
    			qrcodeHandler.setQrcodeEncodeMode('B');
    			qrcodeHandler.setQrcodeVersion(5);
    			System.out.println(content);
    			//	           int imgSize = 67 + 12 * (size - 1);
    			byte[] contentBytes = content.getBytes("gb2312");
    			BufferedImage bufImg = new BufferedImage(115, 115, BufferedImage.TYPE_INT_RGB);
    			Graphics2D gs = bufImg.createGraphics();
    			gs.setBackground(Color.WHITE);
    			gs.clearRect(0, 0, 115, 115);
    			// 设定图像颜色> BLACK 
    			gs.setColor(Color.BLACK);
    			// 设置偏移量 不设置可能导致解析出错 
    			int pixoff = 2;
    			// 输出内容> 二维码 
    			if (contentBytes.length > 0 && contentBytes.length < 800) {
    				boolean[][] codeOut = qrcodeHandler.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,120 ]. ");
    			}
    			gs.dispose();
    			bufImg.flush();
                            //创建file对象,为流的写入做准备
    			File imgFile = new File(imgPath);
    			// 生成二维码QRCode图片 
    			ImageIO.write(bufImg, "png", imgFile);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    

    通过ziwing生成

    引入jar

    <dependency>
              <groupId>com.google.zxing</groupId>
              <artifactId>core</artifactId>
              <version>3.0.0</version>
          </dependency>
         
         <dependency>
              <groupId>com.google.zxing</groupId>
              <artifactId>javase</artifactId>
              <version>3.0.0</version>
          </dependency>
    

    通用代码

    public void encoderQRCode(String content, String imgPath) {//content表示二维码中需要展示的内容,一般为一个连接;imgPath表示二维码生成后输出的路径,格式例如:D:/dev/qrcode/图片名称.png
    		try {
    			Qrcode qrcodeHandler = new Qrcode();
    			// 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小 
                            //特别注意:这里的L,M,Q,H的设置直接影响着content中所包含的字符,百分比越小,content中所能包含的字符越多,但是上限是多少没有测过
    			qrcodeHandler.setQrcodeErrorCorrect('L');
    			qrcodeHandler.setQrcodeEncodeMode('B');
    			qrcodeHandler.setQrcodeVersion(5);
    			System.out.println(content);
    			//	           int imgSize = 67 + 12 * (size - 1);
    			byte[] contentBytes = content.getBytes("gb2312");
    			BufferedImage bufImg = new BufferedImage(115, 115, BufferedImage.TYPE_INT_RGB);
    			Graphics2D gs = bufImg.createGraphics();
    			gs.setBackground(Color.WHITE);
    			gs.clearRect(0, 0, 115, 115);
    			// 设定图像颜色> BLACK 
    			gs.setColor(Color.BLACK);
    			// 设置偏移量 不设置可能导致解析出错 
    			int pixoff = 2;
    			// 输出内容> 二维码 
    			if (contentBytes.length > 0 && contentBytes.length < 800) {
    				boolean[][] codeOut = qrcodeHandler.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,120 ]. ");
    			}
    			gs.dispose();
    			bufImg.flush();
                            //创建file对象,为流的写入做准备
    			File imgFile = new File(imgPath);
    			// 生成二维码QRCode图片 
    			ImageIO.write(bufImg, "png", imgFile);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
  • 相关阅读:
    关于学习Knockoutjs--入门(一)
    h5移动端前端性能优化
    VS2015常用快捷键总结
    51nod1196 字符串的数量
    51nod1189 阶乘分数
    51nod1161 Partial Sums
    51nod1040 矩阵相乘结果的判断
    51nod 1125 交换机器的最小代价
    51nod 1120 机器人走方格 V3
    51nod 1040 最大公约数之和
  • 原文地址:https://www.cnblogs.com/nikeodong/p/7219290.html
Copyright © 2020-2023  润新知