Unable to read a barcode using apache camel(无法使用 apache camel 读取条形码)
问题描述
您好,我尝试使用以下代码从图像中读取条码,但我无法读取该文件,因为它包含多个条码.有什么解决办法吗?
Hi i have tried to read a barcode from image using below code but i am unable to read the file as it contains multiple barcodes. Is there any work around for this?
@GetMapping(value = "OCR/Apachecamel")
@ApiOperation(value = "Get result from Barcode Apachecamel library")
public BarcodeInfo GetApachecamelResult() throws Exception {
try {
InputStream barCodeInputStream = new FileInputStream("images/multiple.png");
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(ImageIO.read(barCodeInputStream))));
if (bitmap.getWidth() < bitmap.getHeight()) {
if (bitmap.isRotateSupported()) {
bitmap = bitmap.rotateCounterClockwise();
}
}
return decode(bitmap);
} catch (IOException e) {
throw new BarcodeDecodingException(e);
}
}
private BarcodeInfo decode(BinaryBitmap bitmap) throws BarcodeDecodingException {
Reader reader = new MultiFormatReader();
try {
Result result = reader.decode(bitmap);
return new BarcodeInfo(result.getText(), result.getBarcodeFormat().toString());
} catch (Exception e) {
throw new BarcodeDecodingException(e);
}
}
public static class BarcodeInfo {
private final String text;
private final String format;
public String getText() {
return text;
}
public String getFormat() {
return format;
}
BarcodeInfo(String text, String format) {
this.text = text;
this.format = format;
}
}
public static class BarcodeDecodingException extends Exception {
BarcodeDecodingException(Throwable cause) {
super(cause);
}
}
pom.xml
<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-barcode -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-barcode</artifactId>
<version>2.21.1</version>
</dependency>
错误
附上图片
有人可以告诉我有什么解决方法吗?提前致谢
Could some one let me know is there any workaround for this? Thanks in advance
推荐答案
我认为你应该学习 fundamentals-of-algorithms 和 Java 首先是 TRY_HARDER
并使用 GenericMultipleBarcodeReader
:)
I think you should learn fundamentals-of-algorithms and Java first then just TRY_HARDER
and use GenericMultipleBarcodeReader
:)
public class MbcPoc {
public static void main(String... args) throws NotFoundException, IOException {
List<BarcodeInfo> list = decodeImageWithMBC("fREyt.png");
list.forEach(BarcodeInfo::println);
}
private static List<BarcodeInfo> decodeImageWithMBC(String imgPath) throws NotFoundException, IOException {
BufferedImage img = ImageIO.read(new File(imgPath));
BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(img)));
MultipleBarcodeReader mbReader = new GenericMultipleBarcodeReader(new MultiFormatReader());
Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
List<BarcodeInfo> list = new ArrayList<>();
for (Result result : mbReader.decodeMultiple(bb, hints)) {
list.add(new BarcodeInfo(result.getText(), result.getBarcodeFormat().name()));
}
return list;
}
public static class BarcodeInfo {
private final String text;
private final String format;
BarcodeInfo(String text, String format) {
this.text = text;
this.format = format;
}
public static void println(BarcodeInfo bci) {
System.out.println(bci.text + "/" + bci.format);
}
}
}
这篇关于无法使用 apache camel 读取条形码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法使用 apache camel 读取条形码


- C++ 和 Java 进程之间的共享内存 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01