编程知识 cdmana.com

Java: com.google.zxing : javase to generate two dimensional code

rely on

<!-- Generate qr code -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.0</version>
</dependency>

Code example

package com.demo.qrcode;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;


public class QRCodeGenerator {
    


    public static void main(String[] args) throws WriterException, IOException {
    
        //  Set parameters 
        String text = "Hello World";
        int width = 350;
        int height = 350;

        Path path = FileSystems.getDefault().getPath("demo.png");

        //  Generate qr code 
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);

        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);


    }
}

 Insert picture description here

Reference resources
A few lines of code , Easy to achieve Java Generating QR code function !

版权声明
本文为[Peng Shiyu]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225150821733v.html

Scroll to Top