当前位置: 首页 > 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());}}
}

结果
在这里插入图片描述

相关文章:

  • 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年面试真题解析与实战指南
  • 从“重规模”向“重回报”转变,公募基金迎系统性改革
  • 全国铁路五一假期累计发送1.51亿人次,多项运输指标创历史新高
  • 重温经典|中国首部剪纸动画片《猪八戒吃瓜》创作始末
  • 巴菲特掌舵伯克希尔60年后将卸任CEO,库克:认识他是人生中最珍贵的经历之一
  • 罗志田:文学革命的社会功能与社会反响
  • 酒店民宿一房难求,湖北宣恩文旅局工作人员腾出家中空房给游客救急