java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot ZXing二维码和条形码

SpringBoot整合ZXing实现二维码和条形码的创建

作者:-代号9527

如今我们越来越多的东西需要用到二维码或者条形码,商品的条形码,付款的二维码等等,所以本文小编给大家介绍了SpringBoot整合ZXing实现二维码和条形码的创建,文章通过代码示例给大家介绍的非常详细,需要的朋友可以参考下

以下为整合zxing实现二维码和条形码的生成。

1、引入依赖

引入ZXing依赖的坐标:

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.4.1</version> 
</dependency>

2、Service层实现

Service接口略,实现类:

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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LLG
 * @date 2023/12/8
 */
@Service
@Slf4j
public class CodeService {

    /**
     * 生成二维码
     * @param data   扫描二维码后得到的信息
     * @param width  二维码的宽
     * @param height 二维码的高
     * @return image
     */
    public BufferedImage generateCode(String data, int width, int height) {
        BufferedImage image = null;
        try {
            Map<EncodeHintType, Object> hints = new HashMap<>();
            //字符编码
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            //错误纠正级别
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            //二维码边距
            hints.put(EncodeHintType.MARGIN, 2);
            MultiFormatWriter writer = new MultiFormatWriter();
            //样式选择QR_CODE,其余枚举类中的可选样式可自己玩
            BitMatrix bitMatrix = writer.encode(data, BarcodeFormat.QR_CODE, width, height, hints);
            image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
            //每个框框的颜色
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    image.setRGB(i, j, bitMatrix.get(i, j) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
                }
            }
            /*以下为选择保存二维码
            String filePath = "test.png";
            File codeFile = new File(filePath);
            ImageIO.write(image,"png",codeFile);
            log.info("QR码生成成功,保存路径{}",filePath);*/
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;

    }

    /**
     * 生成条形码
     * @param data   扫描二维码后得到的信息
     * @param width  条形码的宽
     * @param height 条形码的高
     * @return image
     */
    public BufferedImage generateBarCode(String data, int width, int height) {
        BufferedImage image = null;
        try {
            Map<EncodeHintType, Object> hints = new HashMap<>();
            //字符编码
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            MultiFormatWriter writer = new MultiFormatWriter();
            //码的样式这次选CODE_128
            BitMatrix bitMatrix = writer.encode(data, BarcodeFormat.CODE_128, width, height, hints);
            image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    image.setRGB(i, j, bitMatrix.get(i, j) ? 0 : 0xFFFFFF);   //黑白条形码
                }
            }
        } catch (WriterException e) {
            e.printStackTrace();
        }

        return image;
    }

 
}

3、Controller

写个简单API调用下:

@RestController
public class CodeController {

    @Resource
    private CodeService codeService;

    @GetMapping("/code/image")
    public void getCodeImage(HttpServletRequest request, HttpServletResponse response){
        //测试数据
        String data = "code9527-test!";
        BufferedImage image = codeService.generateCode(data, 100, 100);
        try {
            ImageIO.write(image,"png",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/code/barCode")
    public void getBarCode(HttpServletRequest request,HttpServletResponse response){
        //条形码中的内容
        String data = "TB20231208154900";
        BufferedImage barCode = codeService.generateBarCode(data, 400, 100);
        try {
            ImageIO.write(barCode,"png",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4、效果

二维码:

条形码:

以上就是SpringBoot整合ZXing实现二维码和条形码的创建的详细内容,更多关于SpringBoot ZXing二维码和条形码的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文