提交 9d1e0213 authored 作者: zhoushaopan's avatar zhoushaopan

[二维码]识别二维码

上级 b13b0dea
package com.tykj.dev.device.zxing.controller;
import com.google.zxing.NotFoundException;
import com.tykj.dev.device.zxing.util.ZXingUtil;
import com.tykj.dev.device.zxing.vo.ZxingFile;
import com.tykj.dev.device.zxing.vo.ZxingTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
/**
* DATE:2021-7-20
* Author:zsp
*/
@RestController
@RequestMapping("/zxing")
public class ZxingController {
@Autowired
private ZXingUtil zXingUtil;
@PostMapping("/decode")
public ZxingTaskVo decode(@RequestBody ZxingFile file) throws NotFoundException, IOException {
ZxingTaskVo taskVo = zXingUtil.decode(file.getFilePath());
return taskVo;
}
}
......@@ -52,7 +52,7 @@ public class ZXingUtil {
/**
* 解析二维码
*
* @param file 文件名
* @param filePath 文件名
* @return 二维码内容
* @throws IOException
* @throws NotFoundException
......@@ -68,8 +68,8 @@ public class ZXingUtil {
// Result result = new MultiFormatReader().decode(bitmap, decodeHints);
// return result.getText();
// }
public ZxingTaskVo decode(MultipartFile file) throws IOException, com.google.zxing.NotFoundException {
BufferedImage bufferedImage = ImageIO.read(new FileInputStream((File)file));
public ZxingTaskVo decode(String filePath) throws IOException, com.google.zxing.NotFoundException {
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filePath));
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap bitmap = new BinaryBitmap(binarizer);
......@@ -96,6 +96,7 @@ public class ZXingUtil {
Map<EncodeHintType, Object> encodeHints = new HashMap<EncodeHintType, Object>();
encodeHints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
encodeHints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
// encodeHints.put(EncodeHintType.MARGIN, 0);
encodeHints.put(EncodeHintType.MARGIN, 0);
BitMatrix bitMatrix = new MultiFormatWriter().encode(String.valueOf(taskId),
BarcodeFormat.QR_CODE, width, height, encodeHints);
......
package com.tykj.dev.device.zxing.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* DATE:2021-7-20
* Author:zsp
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ZxingFile {
private String filePath;
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论