Невозможно прочитать штрих-код, используя apache верблюд - PullRequest
1 голос
/ 25 февраля 2020

Привет! Я попытался прочитать штрих-код с изображения, используя приведенный ниже код, но я не могу прочитать файл, так как он содержит несколько штрих-кодов. Есть ли способ обойти это?

@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);
        }
    }

пом. 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>

Ошибка enter image description here

Изображение прилагается enter image description here

Может кто-нибудь сообщить мне, есть ли обходной путь для этого? Заранее спасибо

1 Ответ

1 голос
/ 25 февраля 2020

Я думаю, вы должны изучить основы алгоритмов и Java сначала, затем просто TRY_HARDER и использовать 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);
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...