Java 生成二维码

Java生成二维码工具

使用google条码解析库zxing.
功能: 为给定字符串生成二维码
1. 返回BufferImage格式
2. 写入到指定文件中
3. 写入到OutputStream中
代码如下:

package org.ramer.common.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

/**
 *  The QR code util.
 *
 * @author RAMER
 * @date Apr 19, 2017
 */
public class QRcodeUtil{
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    /**
     * Generate QR code img.
     *
     * @param content the content
     * @param width the width
     * @param height the height
     * @return the buffered image
     */
    public static BufferedImage generateQRcodeImg(String content, Integer width, Integer height) {
        BitMatrix bitMatrix = null;
        Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 0);
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        try {
            bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        } catch (WriterException e) {
            e.printStackTrace();
            return null;
        }
        int w = bitMatrix.getWidth();
        int h = bitMatrix.getHeight();
        BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_BGR);
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
            }
        }
        return bufferedImage;
    }

    /**
     * Generate QR code file.
     *
     * @param content the content
     * @param width the width
     * @param height the height
     * @param format the format,eg: gif,jpeg
     * @param file the file
     * @return the file   null ,if exec error.
     */
    public static File generateQRcodeFile(String content, Integer width, Integer height, 
        String format, File file) {
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        BufferedImage bufferedImage = generateQRcodeImg(content, width, height);
        try {
            ImageIO.write(bufferedImage, format, file);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return file;
    }

    /**
     * Write to stream.
     *
     * @param content the content
     * @param width the width
     * @param height the height
     * @param format the format,eg: gif,jpeg
     * @param outputStream the output stream
     */
    public static void writeToStream(String content, Integer width, Integer height,
        String format,OutputStream outputStream) {
        BufferedImage bufferedImage = generateQRcodeImg(content, width, height);
        try {
            ImageIO.write(bufferedImage, format, outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

测试:

public class QRcodeUtilTest{

    @Test
    public void testGenerateQRcodeFile() {
        File file = new File("Z:/Desktop/qrcode-ramer.jpeg");
        QRcodeUtil.generateQRcodeFile("ramer", 80, 80, "jpeg", file);
    }

    @Test
    public void testWriteToStream() {
        try (FileOutputStream outputStream = new FileOutputStream(
            new File("Z:/Desktop/qrcode2-ramer.jpeg"))) {
            QRcodeUtil.writeToStream("ramer", 80, 80, "jpeg", outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页