美文网首页
生成二维码

生成二维码

作者: _FireFly_ | 来源:发表于2020-10-10 09:26 被阅读0次
    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 {
        private static final String QR_CODE_IMAGE_PATH = "D:/twocode/MyQRCode3.png";
    
        private static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
    
            BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
    
            Path path = FileSystems.getDefault().getPath(filePath);
    
            MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    
        }
        public static void main(String[] args) {
            try {
                generateQRCodeImage("This is my first QR Code GBF2", 350, 350, QR_CODE_IMAGE_PATH);
            } catch (WriterException e) {
                System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
            } catch (IOException e) {
                System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
            }
    
        }
    
    }
    

    相关文章

      网友评论

          本文标题:生成二维码

          本文链接:https://www.haomeiwen.com/subject/kvsdpktx.html