• java生成二维码


    慕课网视频:https://www.imooc.com/learn/531

    1.二维码中黑点代表二进制的1,空白代表0,通过0和1的组合,在二维上对数据进行生成。

    2.二维码分类:线性堆叠式二维码、矩阵式二维码、邮政码。

    • 线性堆叠式二维码:建立在一维码基础上,按需要堆积成两行或多行。
    • 矩阵式二维码(最常用):在一个矩阵空间通过黑、白像素在矩阵中的不同分布进行编码。用点表示二进制1,空白表示二进制0。
    • 邮政码:通过不同长度的条进行编码,主要用于邮政编码。

    3.二维码优缺点:

    优点:

    1. 高密度编码,信息容量大。
    2. 编码范围广。
    3. 容错能力强。
    4. 译码可靠性高。
    5. 可引入加密措施。
    6. 成本低,易制作,持久耐用。

    缺点:

    1. 二维码技术成为手机病毒、钓鱼网站传播的新渠道。
    2. 信息泄露

    4.Qr code:与其他二维码相比,具有识读速度快,数据密度大,占用空间小的优势。

    5.使用zxing生成二维码

    引入相关jar包(3.3.0版本最低要求jdk1.7)

    <dependencies>
          <!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.0</version>
        </dependency>
      </dependencies>
      
      <build>
          <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <version>3.5</version>
                  <configuration>
                      <source>1.7</source>
                      <target>1.7</target>
                      <encoding>UTF-8</encoding>
                  </configuration>
              </plugin>
          </plugins>
      </build>

    调用代码

    public class QrcodeUtil {
    
        private int width = 300;//二维码宽
        
        private int height = 300;//二维码高
        
        private String format = "png";//二维码文件类型
        
        private String content = "";//二维码内容
        
        private Map<EncodeHintType, Object> hints = new HashMap<>();//二维码参数
    
        {
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");//字符集编码
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//纠错能力.L最小
            hints.put(EncodeHintType.MARGIN, new Integer(2));//二维码边框
        }
        
        public static void main(String[] args) {
            QrcodeUtil qu = new QrcodeUtil();
            try {
                qu.createQrCode("hello", "d://1.png");
            } catch (WriterException | IOException e) {
                e.printStackTrace();
            }
        }
        
        public void createQrCode(String contents, String file) throws WriterException, IOException {
            BitMatrix bitMatrix = createBitMatrix(contents, hints);
            Path path = new File(file).toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, format, path);
        }
        
        private BitMatrix createBitMatrix(String contents, Map<EncodeHintType, Object> hints) throws WriterException{
    
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
            
            return bitMatrix;
        }
        
        public QrcodeUtil(int width, int height) {
            this.width = width;
            this.height = height;
        }
        
        public QrcodeUtil(int width, int height, String format) {
            this.width = width;
            this.height = height;
            this.format = format;
        }
        
        public QrcodeUtil() {}
        
        public void setWidth(int width) {
            this.width = width;
        }
    
        public void setHeight(int height) {
            this.height = height;
        }
    
        public void setFormat(String format) {
            this.format = format;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
    }

     6.使用QrCode生成二维码

    QrCode对二维码的生成与解析分别由两个jar包创建。

    生成二维码地址http://www.swetake.com/qrcode/

    解析二维码地址https://zh.osdn.net/projects/qrcode/

    生成代码(可以适用jdk1.4)

    public class Test02 {
    
        public static void main(String[] args) throws IOException {
            int version = 7;
            int width = 67 + 12 * (version - 1);
            int height = 67 + 12 * (version - 1);
            
            Qrcode x = new Qrcode();
            
            x.setQrcodeEncodeMode('M');//纠错等级
            x.setQrcodeEncodeMode('B');//N代表数据,A代表a-z,B代表其他字符
            x.setQrcodeVersion(version);//版本
            
            String content = "hello world!";
            
            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);
            
            byte[] d = content.getBytes();//如果带有汉字,需要制定编码为gb2312,getBytes("gb2312")
            
            int pixoff = 2;//偏移量
            
            if(d.length > 0 && d.length < 120) {
                boolean[][] s = x.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("d://2.png"));
            
        }
        
    }
  • 相关阅读:
    asp.net core文件上传与下载
    asp.net Core1.1版本生成超链接/a链接标签的方式
    CentOS 7.2 64位上装mysql
    CentOS7 yum 安装 Nginx最新版本
    Mysql Mariadb 密码问题
    关闭selinux
    OTRS
    CentOS查看一共安装了多少软件包,是那些软件包
    CentOS删除安装的程序
    centos7 上安装mysql5.7后登录报错ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: Yes 或者No)
  • 原文地址:https://www.cnblogs.com/yxth/p/10255080.html
Copyright © 2020-2023  润新知