当前位置 : 主页 > 编程语言 > c++ >

QRCodeGenerator.java

来源:互联网 收集:自由互联 发布时间:2021-07-03
QRCodeGenerator.java package helper;import com.google.zxing.BarcodeFormat;import com.google.zxing.MultiFormatWriter;import com.google.zxing.WriterException;import com.google.zxing.client.j2se.MatrixToImageWriter;import com.google.zxing.comm
QRCodeGenerator.java
package helper;

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

import java.io.IOException;
import java.io.OutputStream;

/**
 * 二维码生成器.
 */
public class QRCodeGenerator {

    public static void generateCodeToStream(OutputStream out, String qrCodeString, BarcodeFormat format,
                                            int height, int width, String contentType) throws WriterException, IOException {
        // get a byte matrix for the data
        BitMatrix matrix = null;
        com.google.zxing.Writer writer = new MultiFormatWriter();
        try {
            matrix = writer.encode(qrCodeString, format, width, height);
        } catch (IllegalArgumentException ex) {
            throw new WriterException(ex.getMessage());
        }
        MatrixToImageWriter.writeToStream(matrix, contentType, out);
    }

    public static void main(String[] args) throws Exception {
        java.io.FileOutputStream out = new java.io.FileOutputStream(
                "/tmp/qr_code.png");

        //generateQrCodeToStream(out,"12345678901",BarcodeFormat.UPC_A,30,100,"PNG");
        generateCodeToStream(out, "http://www.hyshi.cn/test?website=x&a=2", BarcodeFormat.QR_CODE, 300, 300, "PNG");
    }
}
上一篇:QRCodeUtil.java
下一篇:java执行bat代码.txt
网友评论