欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > java springboot解析出一个图片的多个二维码

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

2025/5/7 19:32:24 来源:https://blog.csdn.net/qq_42800468/article/details/147742571  浏览:    关键词: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());}}
}

结果
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词