Java二维码、条码生成及解码工具类

news/2024/10/22 18:26:12/

功能描述

引入依赖

<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>

代码

java">package com.qiangesoft.qrcode.utils;import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.GlobalHistogramBinarizer;
import com.google.zxing.datamatrix.encoder.SymbolShapeHint;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;/*** 条码工具类** @author qiangesoft* @date 2024-05-06*/
public class QrcodeUtil {/*** 宽*/private static final int WIDTH = 300;/*** 高*/private static final int HEIGHT = 300;/*** 图片格式*/private static final String FORMAT = "png";/*** 黑色*/private static final int BLACK = 0xFF000000;/*** 白色*/private static final int WHITE = 0xFFFFFFFF;/*** 默认编码*/private static String CHARSET = "UTF-8";/*** 生成二维码** @param content* @param outputStream*/public static void generateQRCode(String content, OutputStream outputStream) {generateQRCode(content, WIDTH, HEIGHT, BLACK, WHITE, outputStream);}/*** 生成二维码** @param content* @param width* @param height* @param outputStream*/public static void generateQRCode(String content, int width, int height, OutputStream outputStream) {generateQRCode(content, width, height, BLACK, WHITE, outputStream);}/*** 生成二维码** @param content* @param width* @param height* @param onColor* @param offColor* @param outputStream*/public static void generateQRCode(String content, int width, int height, int onColor, int offColor, OutputStream outputStream) {// 自定义配置Map<EncodeHintType, Object> hints = new HashMap<>();hints.put(EncodeHintType.CHARACTER_SET, CHARSET);hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);hints.put(EncodeHintType.MARGIN, 1);hints.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_SQUARE);generate(content, BarcodeFormat.QR_CODE, width, height, hints, onColor, offColor, FORMAT, outputStream);}/*** 生成条形码** @param content* @param outputStream*/public static void generateCode128(String content, OutputStream outputStream) {generateCode128(content, WIDTH, HEIGHT, BLACK, WHITE, outputStream);}/*** 生成条形码** @param content* @param outputStream*/public static void generateCode128(String content, int width, int height, OutputStream outputStream) {generateCode128(content, width, height, BLACK, WHITE, outputStream);}/*** 生成条形码** @param content* @param outputStream*/public static void generateCode128(String content, int width, int height, int onColor, int offColor, OutputStream outputStream) {// 自定义配置Map<EncodeHintType, Object> hints = new HashMap<>();hints.put(EncodeHintType.CHARACTER_SET, CHARSET);hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);hints.put(EncodeHintType.MARGIN, 1);hints.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_SQUARE);generate(content, BarcodeFormat.CODE_128, width, height, hints, onColor, offColor, FORMAT, outputStream);}/*** 生成码** @param content       内容* @param barcodeFormat 条码格式* @param width         宽* @param height        高* @param hints         参数* @param onColor       码颜色* @param offColor      底色* @param imageFormat   图片格式* @param outputStream  输出流*/private static void generate(String content, BarcodeFormat barcodeFormat, int width, int height, Map<EncodeHintType, Object> hints, int onColor, int offColor, String imageFormat, OutputStream outputStream) {try {// 生成二维码MultiFormatWriter writer = new MultiFormatWriter();BitMatrix matrix = writer.encode(content, barcodeFormat, width, height, hints);// 写入流中MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(onColor, offColor);MatrixToImageWriter.writeToStream(matrix, imageFormat, outputStream, matrixToImageConfig);} catch (Exception e) {throw new RuntimeException("二维码生成异常");}}/*** 解码** @param inputStream*/public static void decode(InputStream inputStream) {// 自定义配置Map<DecodeHintType, String> hints = new HashMap<>();hints.put(DecodeHintType.CHARACTER_SET, CHARSET);try {// 读图片BufferedImage image = ImageIO.read(inputStream);LuminanceSource source = new BufferedImageLuminanceSource(image);// 解析BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));Result res = new MultiFormatReader().decode(bitmap, hints);System.out.println(res.getText());} catch (Exception e) {throw new RuntimeException(e);}}public static void main(String[] args) throws FileNotFoundException {generateQRCode("aaaaaaaaaa", new FileOutputStream("C:\\Users\\16\\Desktop\\qrcode.png"));generateCode128("aaaaaaaaaa", new FileOutputStream("C:\\Users\\16\\Desktop\\code128.png"));decode(new FileInputStream("C:\\Users\\16\\Desktop\\qrcode.png"));}
}

在这里插入图片描述

在这里插入图片描述


http://www.ppmy.cn/news/1455034.html

相关文章

集合定义和使用方法

一.集合的长度 集合的长度,可以添加和删除,长度也会跟着去发生改变,数组一旦创建完成他的长度就不会发生改变。 二.集合的定义方式 ArrayList<String> list new ArrayList(); 三.集合能存储的数据类型 集合能够存储引用数据类型,存储基本数据类型需要使用包装类: 四…

Golang | Leetcode Golang题解之第70题爬楼梯

题目&#xff1a; 题解&#xff1a; func climbStairs(n int) int {sqrt5 : math.Sqrt(5)pow1 : math.Pow((1sqrt5)/2, float64(n1))pow2 : math.Pow((1-sqrt5)/2, float64(n1))return int(math.Round((pow1 - pow2) / sqrt5)) }

标贝数据采集标注在自动驾驶场景中落地应用实例

AI数据服务作为人工智能和机器学习的基础&#xff0c;在自动驾驶领域中有着重要地位。与其他人工智能应用场景相比&#xff0c;自动驾驶的落地场景相对复杂&#xff0c;想要让汽车本身的算法做到处理更多、更复杂的场景&#xff0c;就需要运用大量场景化高质量AI数据做支撑。标…

Redis 渐进式遍历 -- scan

前言 keys 可以一次性把 Redis 中的所有 key 都获取到&#xff0c;但这个操作比较危险&#xff0c;一次性获取所有的key 很容易会导致 Redis 阻塞。 而通过渐进式遍历&#xff08;不是一个命令就将所有的 key 值拿到&#xff0c;而是每执行一次命令只获取其中的一小部分&#x…

有什么好用的思维导图软件?6个软件教你快速进行思维导图的制作

有什么好用的思维导图软件&#xff1f;6个软件教你快速进行思维导图的制作 以下是六款常用且功能强大的思维导图软件&#xff0c;它们可以帮助您快速制作思维导图&#xff1a; 迅捷画图: 迅捷画图是一款在线思维导图工具&#xff0c;具有直观易用的界面和丰富的功能。用户可…

okcc最新版本会被盗打吗?

OKCC是一款智能外呼系统&#xff0c;它提供了多种安全措施来防止系统被盗打。以下是一些关键的安全配置和管理措施&#xff1a; 立即挂失SIM卡&#xff1a;一旦发现OKCC系统被盗打&#xff0c;应立即联系运营商进行SIM卡的挂失&#xff0c;以阻止盗打者继续使用您的号码进行通信…

肆拾玖坊商业模式分析,新品牌如何采用合伙人模式起盘

坐标&#xff1a;厦门&#xff0c;我是易创客运营肖琳 深耕社交新零售行业10年&#xff0c;主要提供新零售系统工具及顶层商业模式设计、全案策划运营陪跑等。 比茅台盈利模式还牛逼的肆拾玖坊&#xff0c;所有男人都逃不出它的圈套&#xff01;只靠49个男人&#xff0c;用一套…

01.本地工作目录、暂存区、本地仓库三者的工作关系

1.持续集成 1.持续集成CI 让产品可以快速迭代&#xff0c;同时还能保持高质量。 简化工作 2.持续交付 交付 3.持续部署 部署 4.持续集成实现的思路 gitjenkins 5.版本控制系统 1.版本控制系统概述2.Git基本概述3.Git基本命令 2.本地工作目录、暂存区、本地仓库三者的工作关系…