Unable to read a barcode using apache camel

后端 未结 1 1605
长发绾君心
长发绾君心 2021-01-24 09:53

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?



        
1条回答
  •  一个人的身影
    2021-01-24 10:32

    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 list = decodeImageWithMBC("fREyt.png");
            list.forEach(BarcodeInfo::println);
        }
    
        private static List 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 hints = new Hashtable<>();
            hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
            List 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);
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题