Spring Boot 集成 zxing 生成条形码与二维码

news2024/9/20 14:41:46

目录

前面我们知道了怎么通过 使用 zxing 生成二维码以及条形码, 由于我们现在都是 web 端的项目了,那么我们看下怎么使用 Spring Boot 集成然后返回给前端展示:

工程源码

对应的工程源码我放到了这里:github源码路径,点击这里查看

开始搭建

这里的整个过程就很简单了,引入依赖包还是和之前一样,另外搭建就两部分:

  • controller 层
  • utils 层

引入依赖

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

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

生成二维码

对应的 controller 代码示例:

@RestController
@RequestMapping(path = "/qrcode")
public class QrCodeController {

    // http://localhost:8080/qrcode/create?content=www.baidu.com
    @GetMapping(path = "/createQrCode")
    public void createQrCode(HttpServletResponse response, @RequestParam("content") String content) {
        try {
            // 创建二维码
            BufferedImage bufferedImage = QrCodeUtils.createImage(content, null, false);

            // 通过流的方式返回给前端
            responseImage(response, bufferedImage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置 可通过 postman 或者浏览器直接浏览
     *
     * @param response      response
     * @param bufferedImage bufferedImage
     * @throws Exception e
     */
    public void responseImage(HttpServletResponse response, BufferedImage bufferedImage) throws Exception {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageOutputStream imageOutput = ImageIO.createImageOutputStream(byteArrayOutputStream);
        ImageIO.write(bufferedImage, "jpeg", imageOutput);
        InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());

        OutputStream outputStream = response.getOutputStream();
        response.setContentType("image/jpeg");
        response.setCharacterEncoding("UTF-8");
        IOUtils.copy(inputStream, outputStream);
        outputStream.flush();
    }
}

对应的 工具类 QrCodeUtils

@Component
public class QrCodeUtils {

    private static final String CHARSET = "UTF-8";
    private static final String FORMAT_NAME = "JPG";

    /**
     * 二维码尺寸
     */
    private static final int QRCODE_SIZE = 300;

    /**
     * LOGO宽度
     */
    private static final int WIDTH = 60;

    /**
     * LOGO高度
     */
    private static final int HEIGHT = 60;

    /**
     * 创建二维码图片
     *
     * @param content    内容
     * @param logoPath   logo
     * @param isCompress 是否压缩Logo
     * @return 返回二维码图片
     * @throws WriterException e
     * @throws IOException     BufferedImage
     */
    public static BufferedImage createImage(String content, String logoPath, boolean isCompress) throws WriterException, IOException {
        Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        // 设置二维码的错误纠正级别 高
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 设置字符集
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        // 设置边距
        hints.put(EncodeHintType.MARGIN, 1);
        // 生成二维码
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        if (logoPath == null || "".equals(logoPath)) {
            return image;
        }

        // 在二维码中增加 logo
        QrCodeUtils.insertImage(image, logoPath, isCompress);
        return image;
    }

    /**
     * 添加Logo
     *
     * @param source     二维码图片
     * @param logoPath   Logo
     * @param isCompress 是否压缩Logo
     * @throws IOException void
     */
    private static void insertImage(BufferedImage source, String logoPath, boolean isCompress) throws IOException {
        File file = new File(logoPath);
        if (!file.exists()) {
            return;
        }

        Image src = ImageIO.read(new File(logoPath));
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        // 压缩LOGO
        if (isCompress) {
            if (width > WIDTH) {
                width = WIDTH;
            }

            if (height > HEIGHT) {
                height = HEIGHT;
            }

            Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            // 绘制缩小后的图
            g.drawImage(image, 0, 0, null);
            g.dispose();
            src = image;
        }

        // 插入LOGO
        Graphics2D graph = source.createGraphics();
        int x = (QRCODE_SIZE - width) / 2;
        int y = (QRCODE_SIZE - height) / 2;
        graph.drawImage(src, x, y, width, height, null);
        Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
        graph.setStroke(new BasicStroke(3f));
        graph.draw(shape);
        graph.dispose();
    }

    /**
     * 生成带Logo的二维码
     *
     * @param content    二维码内容
     * @param logoPath   Logo
     * @param destPath   二维码输出路径
     * @param isCompress 是否压缩Logo
     * @throws Exception void
     */
    public static void create(String content, String logoPath, String destPath, boolean isCompress) throws Exception {
        BufferedImage image = QrCodeUtils.createImage(content, logoPath, isCompress);
        mkdirs(destPath);
        ImageIO.write(image, FORMAT_NAME, new File(destPath));
    }

    /**
     * 生成不带Logo的二维码
     *
     * @param content  二维码内容
     * @param destPath 二维码输出路径
     */
    public static void create(String content, String destPath) throws Exception {
        QrCodeUtils.create(content, null, destPath, false);
    }

    /**
     * 生成带Logo的二维码,并输出到指定的输出流
     *
     * @param content    二维码内容
     * @param logoPath   Logo
     * @param output     输出流
     * @param isCompress 是否压缩Logo
     */
    public static void create(String content, String logoPath, OutputStream output, boolean isCompress) throws Exception {
        BufferedImage image = QrCodeUtils.createImage(content, logoPath, isCompress);
        ImageIO.write(image, FORMAT_NAME, output);
    }

    /**
     * 生成不带Logo的二维码,并输出到指定的输出流
     *
     * @param content 二维码内容
     * @param output  输出流
     * @throws Exception void
     */
    public static void create(String content, OutputStream output) throws Exception {
        QrCodeUtils.create(content, null, output, false);
    }

    /**
     * 二维码解析
     *
     * @param file 二维码
     * @return 返回解析得到的二维码内容
     * @throws Exception String
     */
    public static String parse(File file) throws Exception {
        BufferedImage image;
        image = ImageIO.read(file);
        if (image == null) {
            return null;
        }
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result;
        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
        result = new MultiFormatReader().decode(bitmap, hints);
        return result.getText();
    }

    /**
     * 二维码解析
     *
     * @param path 二维码存储位置
     * @return 返回解析得到的二维码内容
     * @throws Exception String
     */
    public static String parse(String path) throws Exception {
        return QrCodeUtils.parse(new File(path));
    }

    /**
     * 判断路径是否存在,如果不存在则创建
     *
     * @param dir 目录
     */
    public static void mkdirs(String dir) {
        if (dir != null && !"".equals(dir)) {
            File file = new File(dir);
            if (!file.isDirectory()) {
                file.mkdirs();
            }
        }
    }
}

测试

生成的二维码

生成条形码

对应的 controller 代码示例:

@RestController
@RequestMapping(path = "/barcode")
public class BarCodeController {

    @Autowired
    BarCodeUtils barCodeUtils;

    // http://localhost:8080/barcode/createCode?content=987654132&barCodeWord=123456789
    @GetMapping(path = "/createCode")
    public void createQrCode(HttpServletResponse response, @RequestParam("content") String content, @RequestParam("content") String barCodeWord) {
        try {
            // 创建二维码
            ByteArrayOutputStream byteArrayOutputStream = barCodeUtils.barcodeGenerator(content, barCodeWord);

            // 通过流的方式返回给前端
            InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());

            OutputStream outputStream = response.getOutputStream();
            response.setContentType("image/jpeg");
            response.setCharacterEncoding("UTF-8");
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

对应的 工具类 BarCodeUtils

@Component
public class BarCodeUtils {

    /**
     * 条形码宽度
     */
    private static final int WIDTH = 200;

    /**
     * 条形码高度
     */
    private static final int HEIGHT = 50;


    /**
     * 生成条形码,并加文字,以流的方式返回
     *
     * @param content     内容
     * @param barCodeWord 二维码的文字
     * @return ByteArrayOutputStream
     */
    public ByteArrayOutputStream barcodeGenerator(String content, String barCodeWord) {
        // 设置条形码参数
        HashMap<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 设置纠错级别为L(低)
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符编码为UTF-8

        try {
            // 生成条形码的矩阵
            BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, WIDTH, HEIGHT, hints);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(matrix);
            //底部加单号
            BufferedImage image = this.insertWords(bufferedImage, barCodeWord);
            if (Objects.isNull(image)) {
                throw new RuntimeException("条形码加文字失败");
            }
            ImageIO.write(image, "png", outputStream);
            return outputStream;
        } catch (WriterException | IOException e) {
            throw new RuntimeException("条形码生成失败", e);
        }
    }

    private BufferedImage insertWords(BufferedImage image, String words) {
        // 新的图片,把带logo的二维码下面加上文字
        if (StringUtils.hasLength(words)) {
            BufferedImage outImage = new BufferedImage(WIDTH, HEIGHT + 20, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = outImage.createGraphics();
            // 抗锯齿
            this.setGraphics2D(g2d);
            // 设置白色
            this.setColorWhite(g2d);
            // 画条形码到新的面板
            g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
            // 画文字到新的面板
            Color color = new Color(0, 0, 0);
            g2d.setColor(color);
            // 字体、字型、字号
            g2d.setFont(new Font("微软雅黑", Font.PLAIN, 16));
            //文字长度
            int strWidth = g2d.getFontMetrics().stringWidth(words);
            //总长度减去文字长度的一半  (居中显示)
            int wordStartX = (WIDTH - strWidth) / 2;
            //height + (outImage.getHeight() - height) / 2 + 12
            int wordStartY = HEIGHT + 20;
            // time 文字长度

            // 画文字
            g2d.drawString(words, wordStartX, wordStartY);
            g2d.dispose();
            outImage.flush();
            return outImage;
        }
        return null;
    }

    /**
     * 设置 Graphics2D 属性  (抗锯齿)
     *
     * @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制
     */
    private void setGraphics2D(Graphics2D g2d) {
        // 消除画图锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 消除文字锯齿
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        Stroke s = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
        g2d.setStroke(s);
    }

    private void setColorWhite(Graphics2D g2d) {
        g2d.setColor(Color.WHITE);
        //填充整个屏幕
        g2d.fillRect(0, 0, WIDTH, HEIGHT + 20);
        //设置笔刷
        g2d.setColor(Color.BLACK);
    }
}

测试

访问返回对应的条码

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1794868.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Git篇 二】idea中使用git合并分支(拉取分支)

idea中使用git合并分支 前言idea使用git合并分支1) 将主分支&#xff08;master&#xff09;更新到自己的分支&#xff08;dev&#xff09;① checkout到自己分支② 目标分支&#xff08;dev&#xff09;更新到当前分支&#xff08;dev_KC240524&#xff09;③ 当前分支出现“绿…

智能座舱车载数字人解决方案,低资源占用

随着智能汽车的快速发展&#xff0c;人们对汽车的需求已经超越了单纯的交通工具定义&#xff0c;而是更加追求个性化、智能化的出行体验。在这样的背景下&#xff0c;美摄科技凭借其卓越的技术实力和创新能力&#xff0c;推出了面向企业的智能座舱车载数字人解决方案&#xff0…

pytorch——猫狗识别

猫狗识别 训练模型导入需要的包数据加载数据预处理加载数据集并返回对应的图像和标签提取标签信息创建训练和测试的数据加载器图像分类CNN的卷积神经网络模型MYVGG的卷积神经网络模型AlexNet的卷积神经网络模型 训练过程测试过程定义了一个主函数 测试模型导入需要的库加载之前…

【Pytorch】深入Pytorch模型的训练、log、可视化

文章目录 模型训练的模板综合案例-Pytorch 官网demo优化记录日志解析日志增加tensorboard数据记录保存训练曲线模型参数可视化增加wandb数据记录模型训练的模板 综合案例-Pytorch 官网demo pytorch 官网tutorial-quickstart https://blog.csdn.net/weixin_39107270/article/de…

服务器硬件基础知识学习

服务器硬件基础知识涵盖了从CPU到存储&#xff0c;再到网络连接和总线技术等关键组件。 1. 处理器 - 两大流派&#xff1a;我们常用的处理器主要分为Intel和AMD两大阵营。Intel的Xeon系列和AMD的EPYC系列都是专为服务器设计的&#xff0c;它们支持多核处理&#xff0c;能够应对…

Camtasia Studio2024破解汉化版crack安装包下载地址

在当今数字化时代&#xff0c;视频内容已成为传播信息和吸引观众的重要方式。无论是企业宣传、在线教育还是个人创作&#xff0c;一款功能强大的视频编辑软件都是必不可少的工具。而Camtasia Studio2024作为业界领先的视频编辑软件&#xff0c;其永久免费版及最新版本的功能更是…

程序员为什么要学习AI大模型?

前言 在科技浪潮的推动下&#xff0c;人工智能&#xff08;AI&#xff09;技术已经成为推动软件行业发展的核心动力。而在AI技术的众多分支中&#xff0c;AI大模型以其巨大的潜力和广泛的应用场景&#xff0c;逐渐成为了程序员们关注的焦点。本文将从程序员的角度出发&#xf…

MoE-LLaVA:为大型视觉-语言模型引入专家混合

随着人工智能技术的飞速发展&#xff0c;大型视觉-语言模型&#xff08;LVLMs&#xff09;在图像理解和自然语言处理方面展现出了巨大的潜力。这些模型通过结合图像编码器和语言模型&#xff0c;能够处理包括图像描述、视觉问答和图像字幕生成等在内的多种任务。然而&#xff0…

Python办公自动化文件自动分类

Python文件自动分类是指使用Python编程语言编写程序&#xff0c;根据特定的规则或条件将计算机上的文件自动归类到不同的文件夹中。这种功能在日常工作中非常有用&#xff0c;特别是在处理大量不同格式的文件时&#xff0c;可以帮助用户快速整理和检索文件。 在Python中&#…

申请到对账全流程贯通,报销竟能如此丝滑?

差旅一键出行&#xff0c;报销太烦恼&#xff1f;面对成堆的发票和繁琐的报销流程&#xff0c;不是错漏&#xff0c;便是杂乱&#xff0c;出行人郁闷&#xff0c;财务也头疼。今天带大家看看&#xff0c;当申请到对账全流程贯通后&#xff0c;员工出差将是怎样的新体验? 一、一…

C++11:列表初始化 初始化列表initializer_list

前言 2003年C标准委员会曾经提交了一份技术勘误表&#xff08;简称TC1&#xff09;&#xff0c;使得C03这个名字取代了C98成为了C11前最新的C标准名称。不过由于C03主要是对C98标准中的漏洞进行修复&#xff0c;语言的核心部分则没有改动&#xff0c;因此人们习惯性的把两个标准…

扣子工作流实战案例教程,手把手教你搭建一个图书管理工作流

&#x1f9d9;‍♂️ 诸位好&#xff0c;吾乃斜杠君&#xff0c;编程界之翘楚&#xff0c;代码之大师。算法如流水&#xff0c;逻辑如棋局。 &#x1f4dc; 吾之教程&#xff0c;内含诸般技术之秘诀。吾欲以此笔记&#xff0c;传授编程之道&#xff0c;助汝解技术难题。 &#…

STM32 音乐播放器之音频入门实验(pwm、dac、.wav、.mp3)

1.pwm实现简易电子琴实验 1.改变PWM频率&#xff0c;输出不同音调 2.改变占空比&#xff0c;调节音量大小 3.按键弹奏&#xff0c;支持按按键录取弹奏音 4.播放:中高低音&#xff1b;录取音&#xff1b;指定歌曲 5.支持按上一首&#xff0c;下一首&#xff0c;调弹奏速度&#…

C++设计模式-外观模式,游戏引擎管理多个子系统,反汇编

运行在VS2022&#xff0c;x86&#xff0c;Debug下。 30. 外观模式 为子系统定义一组统一的接口&#xff0c;这个高级接口会让子系统更容易被使用。应用&#xff1a;如在游戏开发中&#xff0c;游戏引擎包含多个子系统&#xff0c;如物理、渲染、粒子、UI、音频等。可以使用外观…

数据分析每周挑战——心衰患者特征数据集

这是一篇关于医学数据的数据分析&#xff0c;但是这个数据集数据不是很多。 背景描述 本数据集包含了多个与心力衰竭相关的特征&#xff0c;用于分析和预测患者心力衰竭发作的风险。数据集涵盖了从40岁到95岁不等年龄的患者群体&#xff0c;提供了广泛的生理和生活方式指标&a…

Web3开发框架分析

Web3开发框架旨在简化区块链和去中心化应用&#xff08;DApp&#xff09;的开发过程&#xff0c;为开发者提供必要的工具和库。以下是一些主要的Web3开发框架。北京木奇移动技术有限公司&#xff0c;专业的软件外包开发公司&#xff0c;欢迎交流合作。 1.Truffle Suite Truffl…

GB28181安防视频融合汇聚平台EasyCVR如何实现视频画面自定义标签?

安防视频融合汇聚平台EasyCVR兼容性强&#xff0c;可支持Windows系统、Linux系统以及国产化操作系统等&#xff0c;平台既具备传统安防视频监控的能力&#xff0c;也具备接入AI智能分析的能力&#xff0c;可拓展性强、视频能力灵活&#xff0c;能对外分发RTMP、RTSP、HTTP-FLV、…

25 - 销售分析III(高频 SQL 50 题基础版)

25 - 销售分析III -- where 是分组之前筛选数据 -- having 是分组之后筛选数据selectp.product_id,p.product_name fromSales s left join Product p on s.product_idp.product_id group byproduct_id havingmin(sale_date) >"2019-01-01" and max(sale_date)&…

使python技能飙升的5个实用F-String技巧

前言 python中存在着一些奇奇怪怪的语法&#xff0c;了解这些语法可以使我们在编写程序时事半功倍&#xff0c;不了解的则会使我们产生疑惑。今天&#xff0c;我们来了解一下python中关于f-string的5个实用的小技巧。 技巧一:数字分隔 num 1000000000 print(-*20)print(f{n…

传统的老程序员转向人工智能需要准备好这三件事情,你知道吗?

前言 人类文明的进步有时候快的吓人&#xff0c;在我们父辈上街买菜还是以一毛为计量买菜的时代&#xff0c;其实过去了也没有多长时间。现在买菜接近10块为准了&#xff0c;正常在集市上喊着怎么卖&#xff1f;摊主喊着三斤&#xff0c;包含的意思是10块钱三斤。相隔这么多年…