Java生成条形码和二维码

生成二维码

添加依赖:

<!-- 二维码工具 -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.0</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.0</version>
</dependency>

功能代码:

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;
import org.springframework.core.io.ClassPathResource;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 二维码工具
 */
public class QrcodeUtils {

    /**
     * 生成二维码核心方法
     *
     * @param msg  包含的内容
     * @param size 二维码大小(因为是正方形的,故而长高一致)
     */
    public static BufferedImage createQr(String msg, int size) throws WriterException {
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        // 容错率
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 编码
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // 设置白色边框
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bm = multiFormatWriter.encode(msg, BarcodeFormat.QR_CODE, size, size, hints);
        int w = bm.getWidth(), h = bm.getHeight();
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                image.setRGB(x, y, bm.get(x, y) ? 0xFF000000 : 0xFFFFFF);
            }
        }
        return image;
    }

    /**
     * 给二维码添加logo
     *
     * @param bi         二维码
     * @param logoPath   logo图片名称(位置统一存放于/resources/qrlogo/下)
     * @param widthLogo  logo宽度(如果传0,则表示默认为二维码宽度的20%)
     * @param heightLogo logo高度(如果传0,则表示默认为二维码高度的20%)
     * @throws 如果出错了,除了打印错误外,不抛异常,直接跳过本方法,把原先的二维码return出去
     */
    public static BufferedImage addLogo(BufferedImage bi, String logoPath, int widthLogo, int heightLogo) {
        BufferedImage image = bi;
        try {
            Graphics2D g = image.createGraphics();
            BufferedImage logo = ImageIO.read(new FileInputStream(new ClassPathResource("/qrlogo/" + logoPath + ".png").getFile()));
            // 计算图片放置位置,放置在中间
            int x = (image.getWidth() - widthLogo) / 2;
            int y = (image.getHeight() - heightLogo) / 2;
            // 把logo画进二维码里
            g.drawImage(logo, x, y, widthLogo, heightLogo, null);
            g.drawRoundRect(x, y, widthLogo, heightLogo, 15, 15);
            // 边框宽度
            g.setStroke(new BasicStroke(2));
            // 边框颜色
            g.setColor(Color.WHITE);
            g.drawRect(x, y, widthLogo, heightLogo);
            // 关闭
            g.dispose();
            logo.flush();
            image.flush();
            return image;
        } catch (Exception e) {
            e.printStackTrace();
            return bi;
        }
    }
}

测试:

/**
 * 生成二维码
 */
@Controller
@RequestMapping("qrcode")
public class QrcodeController {

    /**
     * 二维码尺寸
     */
    private static final int QRCODE_SIZE = 500;
    /**
     * 二维码图片格式
     */
    private static final String QRCODE_TYPE = "png";

    /**
     * 生成二维码
     * 使用微信扫描会得到msg内容
     *
     * @param msg         二维码包含的内容
     * @param logo        logo图片名称(位置统一存放于/resources/qrlogo/下)(不传则表示不插入logo)
     * @param logoPercent 图片占据二维码的百分比(0-1数值),不传默认0.2
     */
    @RequestMapping("create")
    public void create(String msg, String logo, String logoPercent, HttpServletResponse response) {
        try {
            response.setContentType("image/" + QRCODE_TYPE);
            OutputStream os = response.getOutputStream();
            BufferedImage bi = QrcodeUtils.createQr(msg, QRCODE_SIZE);
            if (logo != null) {
                // 图片大小
                int logoLenght = QRCODE_SIZE;
                double p = 0.2;
                if (logoPercent != null) {
                    try {
                        p = Double.parseDouble(logoPercent);
                    } catch (Exception e) {
                        p = 0.2;
                    }
                }
                logoLenght = (int) (logoLenght * p);
                bi = QrcodeUtils.addLogo(bi, logo, logoLenght, logoLenght);
            }
            ImageIO.write(bi, QRCODE_TYPE, os);
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

生成条形码

添加依赖:

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.0</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.0</version>
</dependency>
<!--> thumbnailator <-->
<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.8</version>
</dependency>

功能代码:

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.EnumMap;
import java.util.Map;

/**
 * 条形码生成/解析工具类
 */
public class BarcodeUtils {

    /**
     * 条形码默认宽度
     */
    private static final int BARCODE_WIDTH = 180;

    /**
     * 条形默认高度
     */
    private static final int BARCODE_HEIGHT = 70;


    /**
     * 默认条形码文件格式
     */
    private static final String FORMAT = "png";

    /**
     * 条形条形码参数
     */
    private static final EnumMap<EncodeHintType, Object> ENCODE_HINTS = new EnumMap<>(EncodeHintType.class);

    /**
     * 解析条形码参数
     */
    private static final EnumMap<DecodeHintType, Object> DECODE_HINTS = new EnumMap<>(DecodeHintType.class);

    /**
     * 微软雅黑常规字体库 字体库
     */
    private static final String BASE_FONT = "font/msyh.ttc";

    static {
        /* 字符编码 */
        ENCODE_HINTS.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        /* 容错等级 L、M、Q、H 其中 L 为最低, H 为最高 */
        ENCODE_HINTS.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        /* 字符编码 */
        DECODE_HINTS.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        /* 优化精度 */
        DECODE_HINTS.put(DecodeHintType.TRY_HARDER, true);
    }

    private BarcodeUtils() {
    }

    /**
     * @param text     文本内容
     * @param filePath 生成图片路径
     * @throws WriterException
     * @throws IOException
     * @title 生成条形图片
     * @description 根据文本内容生成默认格式的二维图图片
     */
    public static void create(String text, String filePath) throws WriterException, IOException {
        BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.CODE_128, BARCODE_WIDTH, BARCODE_HEIGHT, ENCODE_HINTS);
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, path);
    }

    /**
     * @param text 文本内容
     * @return 字节输出流
     * @throws WriterException
     * @throws IOException
     * @title 生成条形码图片
     * @description 根据文本内容生成条形码图片的字节输出流
     */
    public static ByteArrayOutputStream create(String text) throws WriterException, IOException {
        BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.CODE_128, BARCODE_WIDTH, BARCODE_HEIGHT, ENCODE_HINTS);
        return writeToStream(bitMatrix, FORMAT);
    }

    /**
     * @param text     文本内容
     * @param filePath 生成图片文件路径
     * @param width    宽度
     * @param height   高度
     * @title 生成条形码图片
     * @description 根据文本内容,自定义宽度高度,生成所需要的条形码图片
     */
    public static void create(String text, String filePath, int width, int height) throws WriterException, IOException {
        BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.CODE_128, width, height, ENCODE_HINTS);
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, path);
    }

    /**
     * @param text     文本内容
     * @param filePath 生成图片文件路径
     * @param width    宽度
     * @param height   高度
     * @param onColor  条形码颜色
     * @param offColor 条形码背景颜色
     * @title 生成条形图片
     * @description 根据文本内容,自定义宽度高度,生成所需要的条形码图片
     */
    public static void create(String text, String filePath, int width, int height, Color onColor, Color offColor) throws WriterException, IOException {
        BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.CODE_128, width, height, ENCODE_HINTS);
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, path, new MatrixToImageConfig(onColor.getRGB(), offColor.getRGB()));
    }

    /**
     * @param text   文本内容
     * @param width  宽度
     * @param height 高度
     * @return 字节输出流
     * @title 生成条形码图片
     * @description 根据文本内容生成条形码图片的字节输出流
     */
    public static ByteArrayOutputStream create(String text, int width, int height) throws WriterException, IOException {
        BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, ENCODE_HINTS);
        return writeToStream(bitMatrix, FORMAT);
    }

    /**
     * @param text     文本内容
     * @param filePath 生成图片文件路径
     * @param width    宽度
     * @param height   高度
     * @param format   图片格式
     * @title 生成条形码图片
     * @description 根据文本内容,自定义宽度高度,自定义图片格式,生成所需要的条形码图片
     */
    public static void create(String text, String filePath, int width, int height, String format) throws WriterException, IOException {
        BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, ENCODE_HINTS);
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, format, path);
    }

    /**
     * @param text   文本内容
     * @param width  宽度
     * @param height 高度
     * @param format 自定义图片格式
     * @return 字节输出流
     * @title 生成条形图片
     * @description 根据文本内容生成条形图片的字节输出流
     */
    public static ByteArrayOutputStream create(String text, int width, int height, String format) throws WriterException, IOException {
        BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, ENCODE_HINTS);
        return writeToStream(bitMatrix, format);
    }

    /**
     * @param text      文本内容
     * @param filePath  生成图片文件路径
     * @param width     宽度
     * @param height    高度
     * @param format    图片格式
     * @param hintTypes 配置信息
     * @title 生成条形码图片
     * @description 根据文本内容,自定义宽度高度,自定义图片格式,自定义配置信息,生成所需要的条形码图片
     */
    public static void create(String text, String filePath, int width, int height, String format, EnumMap<EncodeHintType, Object> hintTypes) throws WriterException, IOException {
        BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, hintTypes);
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, format, path);
    }

    /**
     * @param text      文本内容
     * @param width     宽度
     * @param height    高度
     * @param format    图片格式
     * @param hintTypes 配置信息
     * @return 字节输出流
     * @throws WriterException
     * @throws IOException
     * @title 生成条形码图片
     */
    public static ByteArrayOutputStream create(String text, int width, int height, String format, EnumMap<EncodeHintType, Object> hintTypes) throws WriterException, IOException {
        BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, hintTypes);
        return writeToStream(bitMatrix, format);
    }

    /**
     * 生成带文字的条形图片
     *
     * @param text     文本内容
     * @param filePath 生成文件存放路径
     */
    public static void createWithText(String text, String filePath) throws IOException, WriterException {
        createWidthLogo(text, BARCODE_WIDTH, BARCODE_HEIGHT, FORMAT, ENCODE_HINTS)
                .toFile(filePath);
    }


    /**
     * @param filePath 图片路径
     * @return 文本信息
     * @throws IOException
     * @throws FormatException
     * @throws ChecksumException
     * @throws NotFoundException
     * @title 解析条形码
     * @description 解析条形,获取其中文本信息
     */
    public static String read(String filePath) throws IOException, FormatException, ChecksumException, NotFoundException {
        BinaryBitmap binaryBitmap = createBinaryBitmap(new FileInputStream(filePath));
        return readBarcode(binaryBitmap, DECODE_HINTS).getText();
    }

    /**
     * @param inputStream 输入流
     * @return 文本信息
     * @throws IOException
     * @throws FormatException
     * @throws ChecksumException
     * @throws NotFoundException
     * @title 解析条形码
     * @description 解析条形码,获取其中文本信息
     */
    public static String read(InputStream inputStream) throws IOException, FormatException, ChecksumException, NotFoundException {
        BinaryBitmap binaryBitmap = createBinaryBitmap(inputStream);
        return readBarcode(binaryBitmap, DECODE_HINTS).getText();
    }

    /**
     * @param filePath    图片路径
     * @param decodeHints 解析配置
     * @return 文本信息
     * @throws FormatException
     * @throws ChecksumException
     * @throws NotFoundException
     * @throws IOException
     * @title 解析条形码
     * @description 解析条形码,获取其中文本信息
     */
    private static String read(String filePath, Map<DecodeHintType, Object> decodeHints) throws FormatException, ChecksumException, NotFoundException, IOException {
        BinaryBitmap binaryBitmap = createBinaryBitmap(new FileInputStream(filePath));
        return readBarcode(binaryBitmap, decodeHints).getText();
    }

    /**
     * @param inputStream 输入流
     * @return 文本信息
     * @throws IOException
     * @throws FormatException
     * @throws ChecksumException
     * @throws NotFoundException
     * @title 解析条形码
     * @description 解析条形码,获取其中文本信息
     */
    public static String read(InputStream inputStream, Map<DecodeHintType, Object> decodeHints) throws IOException, FormatException, ChecksumException, NotFoundException {
        BinaryBitmap binaryBitmap = createBinaryBitmap(inputStream);
        return readBarcode(binaryBitmap, decodeHints).getText();
    }


    /**
     * @param text          文本内容
     * @param barcodeFormat 条形码格式
     * @param width         宽度
     * @param height        高度
     * @param hintTypes     配置
     * @return BitMatrix
     * @throws WriterException
     * @title 生成BitMatrix
     */
    private static BitMatrix createBitMatrix(String text, BarcodeFormat barcodeFormat, int width, int height, EnumMap<EncodeHintType, Object> hintTypes) throws WriterException {
        MultiFormatWriter writer = new MultiFormatWriter();
        return writer.encode(text, barcodeFormat, width, height, hintTypes);
    }

    /**
     * 转成字符输出流
     *
     * @param bitMatrix bitMatrix
     * @param format    图片格式
     * @return
     * @throws IOException
     */
    private static ByteArrayOutputStream writeToStream(BitMatrix bitMatrix, String format) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, format, outputStream);
        return outputStream;
    }

    /**
     * 解析条形码
     *
     * @param binaryBitmap 条形码图类
     * @return 解析信息
     * @throws FormatException
     * @throws ChecksumException
     * @throws NotFoundException
     */
    private static Result readBarcode(BinaryBitmap binaryBitmap, Map<DecodeHintType, Object> decodeHints) throws FormatException, ChecksumException, NotFoundException {
        MultiFormatReader reader = new MultiFormatReader();
        return reader.decode(binaryBitmap, decodeHints);
    }

    /**
     * 生成BinaryBitmap
     *
     * @param inputStream 输入流
     * @return
     * @throws IOException
     */
    private static BinaryBitmap createBinaryBitmap(InputStream inputStream) throws IOException {
        BufferedImage bufferedImage = ImageIO.read(inputStream);
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        Binarizer binarizer = new HybridBinarizer(source);
        return new BinaryBitmap(binarizer);
    }

    /**
     * 生成带有文本的条形码
     *
     * @param text         生成文本
     * @param qrCodeWidth  条形码宽度
     * @param qrCodeHeight 条形码高度
     * @param format       图片格式
     * @param encodeHints  条形码配置
     * @return 输出流
     * @throws WriterException
     * @throws IOException
     */
    private static Thumbnails.Builder<BufferedImage> createWidthLogo(String text, int qrCodeWidth, int qrCodeHeight, String format,
                                                                     EnumMap<EncodeHintType, Object> encodeHints) throws WriterException, IOException {
        BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.CODE_128, qrCodeWidth, qrCodeHeight, encodeHints);
        /* 生成条形码的bufferedImage */
        BufferedImage barcode = MatrixToImageWriter.toBufferedImage(bitMatrix);
        /* 创建图片构件对象 */
        BufferedImage bg = new BufferedImage(qrCodeWidth, qrCodeHeight + 10, BufferedImage.TYPE_INT_RGB);
        Graphics bgGraphics = bg.getGraphics();
        bgGraphics.setColor(Color.WHITE);
        bgGraphics.fillRect(0, 0, qrCodeWidth, qrCodeHeight);
        Thumbnails.Builder<BufferedImage> builder = Thumbnails.of(bg);
        String fontFile = BarcodeUtils.class.getClassLoader()
                .getResource(BASE_FONT)
                .getPath();
        Font font = new Font(fontFile, Font.PLAIN, 12);
        BufferedImage textImage = new BufferedImage(qrCodeWidth, font.getSize(), BufferedImage.TYPE_INT_ARGB);
        //获得图片的笔刷
        Graphics graphics = textImage.getGraphics();
        // 背景颜色
        bgGraphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, qrCodeWidth, font.getSize());
        graphics.setColor(Color.BLACK);
        //文字居中
        FontMetrics fm = graphics.getFontMetrics();
        graphics.setFont(font);
        graphics.drawString(text, (qrCodeWidth - fm.stringWidth(text)) / 2, font.getSize());
        BufferedImage textBufferedImage = Thumbnails.of(textImage).size(qrCodeWidth, font.getSize()).asBufferedImage();
        /* 设置l位置居中,不透明  */
        builder.watermark(Positions.CENTER, barcode, 1F)
                .watermark(Positions.BOTTOM_CENTER, textBufferedImage, 1F)
                .scale(1F)
                .outputFormat(format);
        return builder;
    }

    public static void main(String[] args) throws IOException, WriterException {
        // 生成带文本的条形码,其他方法请查看代码的注释
        BarcodeUtils.createWithText("GVB125828", "/qrlogo/barcode.png");
    }
}

 

posted @ 2022-06-11 14:39  残城碎梦  阅读(249)  评论(0编辑  收藏  举报