当前位置: 首页 > news >正文

java springboot解析出一个图片的多个二维码

引入

<dependencies><!-- ZXing --><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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>

服务类

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.*;@Service
public class QRCodeReaderService {public List<String> readMultipleQRCodes(MultipartFile file) throws IOException, NotFoundException {// 将 MultipartFile 转换为 BufferedImageBufferedImage bufferedImage = ImageIO.read(file.getInputStream());LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));Map<DecodeHintType, Object> hints = new HashMap<>();hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);hints.put(DecodeHintType.POSSIBLE_FORMATS, Collections.singletonList(BarcodeFormat.QR_CODE));MultiFormatReader multiFormatReader = new MultiFormatReader();GenericMultipleBarcodeReader reader = new GenericMultipleBarcodeReader(multiFormatReader);Result[] results = reader.decodeMultiple(bitmap, hints);List<String> qrContents = new ArrayList<>();if (results != null) {for (Result result : results) {qrContents.add(result.getText());}}return qrContents;}
}

接口

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import java.util.List;@RestController
public class QRCodeController {@Autowiredprivate QRCodeReaderService qrCodeReaderService;@PostMapping("/upload")public ResponseEntity<List<String>> uploadQRCodeImage(@RequestParam("file") MultipartFile file) {try {List<String> qrCodes = qrCodeReaderService.readMultipleQRCodes(file);return ResponseEntity.ok(qrCodes);} catch (Exception e) {e.printStackTrace();return ResponseEntity.status(500).body(Collections.emptyList());}}
}

结果
在这里插入图片描述

http://www.dtcms.com/a/175087.html

相关文章:

  • Linux(十四)进程间通信(IPC),管道
  • 鸿蒙系统被抹黑的深层解析:技术、商业与地缘政治的复杂博弈-优雅草卓伊凡
  • 基于Blender的AI插件——2D图片生成3D模型
  • Android Intent 页面跳转与数据回传示例(附完整源码)
  • 项目整合管理(二)
  • 几何类型(Geometry Types)虽然名称相似,但在结构、维度和用途上是有明显区别的
  • CUDA编程 - 如何在 GPU 上使用 C++ 函数重载 - cppOverload
  • C++学习知识点汇总
  • 前端正则学习记录
  • Winform(12.控件讲解)
  • 解决Hyper-V无法启动Debian 12虚拟机
  • Android Retrofit框架分析(三):自动切换回主线程;bulid的过程;create方法+ServiceMethod源码了解
  • Webview通信系统学习指南
  • 通过Config批量注入对象到Spring IoC容器
  • Qt开发经验 --- 避坑指南(4)
  • 十分钟了解 @MapperScan
  • LeetCode 热题 100 22. 括号生成
  • 大学之大:隆德大学2025.5.6
  • JSON 转换为 Word 文档
  • SLAM算法工程师面经大全:2025年面试真题解析与实战指南
  • 个人Unity自用面经(未完)
  • Three.js 基础与实践
  • JavaSE核心知识点01基础语法01-04(数组)
  • QQMUSIC测试报告
  • 双目标清单——AI与思维模型【96】
  • 智能机器人赋能小天互连IM系统,打造高效办公新生态
  • cephadm部署ceph集群
  • Flowable7.x学习笔记(二十)查看流程办理进度图
  • 从零开始学习人工智能Day6-Python3标准库概览
  • 【AI提示词】六顶思考帽工具专家