ZXing 2.0发布与在线ZXing解码器有什么不同吗?

发布于 2025-01-04 07:24:02 字数 6504 浏览 1 评论 0原文

我正在尝试使用 ZXing 库开发一个用于解码 QR 码的 Java 项目。但是,某些包含二维码的图像无法通过运行我的项目进行解码,但这些图像与在线 ZXing 解码器可以正常工作。我只是好奇 ZXing 发布的版本与他们用于在线解码器的版本相同吗?或者他们调整了在线版本。由于这种困惑,我正在拉扯我的头发。

public class Validator implements IValidator {
    private static Logger logger = Logger.getLogger(Validator.class);
    private BufferedImage currentImage;
    private String resultText;
    private float moduleSize;
    private ResultPoint[] patternCenters;
    private int blockSizePower;

    public Validator(BufferedImage imageFile) {
        this.currentImage = imageFile;
        setLuminanceThreshold(3); //default value used by validator
    }

    public Validator(File imageFile) {
        // take input image file and store in a BufferedImage variable
        try {
            currentImage = ImageIO.read(imageFile);
        } catch (IOException e) {
            logger.error("Image cannot be opened. There is no such image file. ", e);
        }
    }


    /**
     * <p>Validating the QR code</p>
     *
     * @return true if the QR code can be decoded
     */
    @Override
    public boolean validateQRCode() {
        return validateQRCode(null);
    }

    public boolean validateQRCode(Hashtable outValues) {
        return validateQRCode(outValues, true);
    }

    // if localLuminanceCheck == true then call HybridBinarizer, otherwise call GlobalHistogramBinarizer  
    public boolean validateQRCode(Hashtable outValues, boolean localLuminanceCheck)
    {
        return validateQRCode(outValues, true, false);
    }

    public boolean validateQRCode(Hashtable outValues, boolean localLuminanceCheck, boolean scale) {
        if (scale)
        {
            try {
                this.currentImage = Thumbnails.of(currentImage).size(275, 275).asBufferedImage();

            } catch (IOException e) {
                logger.error("Image cannot be scaled. ", e);
            }
        }

        // finding luminance of the image
        LuminanceSource lumSource = new BufferedImageLuminanceSource(currentImage);

        Binarizer qrHB;
        if (!localLuminanceCheck) {
            qrHB = new GlobalHistogramBinarizer(lumSource);
        } else {
            // creating binary bitmap from Black-White image
            qrHB = new HybridBinarizer(lumSource);
            ((HybridBinarizer) qrHB).setBLOCK_SIZE_POWER(blockSizePower);
        }
        BinaryBitmap bitmap = new BinaryBitmap(qrHB);

        try {
            currentImage = MatrixToImageWriter.toBufferedImage(bitmap.getBlackMatrix());
        } catch (NotFoundException e) {
            logger.error("cannot find any bit matrix.", e);
        }

        Hashtable<DecodeHintType, Object> hint = new Hashtable<DecodeHintType, Object>();
        hint.put(DecodeHintType.TRY_HARDER, BarcodeFormat.QR_CODE);

        QRCodeReader QRreader = new QRCodeReader();

        try {
            // decodes the QR code
            Result result = QRreader.decode(bitmap, hint);

            resultText = result.getText();
            return true;
        } catch (NotFoundException e) {
            logger.info("cannot detect any QR code (no enough finder patterns).");
            return false;
        } catch (ChecksumException e) {
            logger.info("cannot recover the QR code. Too much data errors.");
            return false;
        } catch (FormatException e) {
            logger.info("QR code cannot be decoded.");
            return false;
        } catch (FinderPatternNotFoundException e) {
            // if no Finder Pattern has been found, it may be the color of
            // QR is inverted. So we invert the QR and try one more time

            Binarizer invertHB;
            if (!localLuminanceCheck) {
                invertHB = new GlobalHistogramBinarizer(lumSource);
            } else {
                invertHB = new HybridBinarizer(lumSource);
                ((HybridBinarizer) invertHB).setBLOCK_SIZE_POWER(blockSizePower);
            }

            // get the inverted Black-White matrix
            BitMatrix invertBlackMatrix = null;
            try {
                invertBlackMatrix = invertHB.getBlackMatrix();
            } catch (NotFoundException e1) {
                logger.error(e1);
            }

            int invertWidth = currentImage.getWidth();
            int invertHeight = currentImage.getHeight();

            // flip each bit in the inverted BitMatrix
            for (int x = 0; x < invertWidth; x++) {
                for (int y = 0; y < invertHeight; y++) {
                    invertBlackMatrix.flip(x, y);
                }
            }

            currentImage = MatrixToImageWriter.toBufferedImage(invertBlackMatrix);

            // get luminance source from inverted image
            lumSource = new BufferedImageLuminanceSource(currentImage);

            Binarizer afterInvertHB;
            if (!localLuminanceCheck) {
                afterInvertHB = new GlobalHistogramBinarizer(lumSource);

            } else {
                // creating binary bitmap from Black-White image
                afterInvertHB = new HybridBinarizer(lumSource);
                ((HybridBinarizer) afterInvertHB).setBLOCK_SIZE_POWER(blockSizePower);
            }
            BinaryBitmap invertBitMap = new BinaryBitmap(afterInvertHB);

            // decoding inverted QR
            QRCodeReader invertQRreader = new QRCodeReader();

            try {
                Result invertResult = invertQRreader.decode(invertBitMap, hint);

                resultText = invertResult.getText();

                System.out.println("Out put data is: " + resultText);

                return true;
            } catch (NotFoundException e1) {
                logger.info("cannot detect any QR code (no enough finder patterns).");
                return false;
            } catch (ChecksumException e1) {
                logger.info("cannot recover the QR code. Too much data errors.");
                return false;
            } catch (FormatException e1) {
                logger.info("QR code cannot be decoded.");
                return false;
            } catch (FinderPatternNotFoundException e1) {
                logger.info("Cannot confirm where all three Finder Patterns are! ");
                return false;
            } catch (Exception e1) {
                logger.error(e1);
                return false;
            }
        } catch (Exception e) {
            logger.error(e);
            return false;
        }
    }

}

Im trying to use ZXing library to develop a Java project for decoding a QR code. However, some of the image containing QR code can not be decoded by running my project, but these are working fine with Online ZXing decoder. I am just curious does the ZXing released version is the same as they are using for Online decoder? or they have tweaked the online version. I'm pulling my hair because of this confusion.

public class Validator implements IValidator {
    private static Logger logger = Logger.getLogger(Validator.class);
    private BufferedImage currentImage;
    private String resultText;
    private float moduleSize;
    private ResultPoint[] patternCenters;
    private int blockSizePower;

    public Validator(BufferedImage imageFile) {
        this.currentImage = imageFile;
        setLuminanceThreshold(3); //default value used by validator
    }

    public Validator(File imageFile) {
        // take input image file and store in a BufferedImage variable
        try {
            currentImage = ImageIO.read(imageFile);
        } catch (IOException e) {
            logger.error("Image cannot be opened. There is no such image file. ", e);
        }
    }


    /**
     * <p>Validating the QR code</p>
     *
     * @return true if the QR code can be decoded
     */
    @Override
    public boolean validateQRCode() {
        return validateQRCode(null);
    }

    public boolean validateQRCode(Hashtable outValues) {
        return validateQRCode(outValues, true);
    }

    // if localLuminanceCheck == true then call HybridBinarizer, otherwise call GlobalHistogramBinarizer  
    public boolean validateQRCode(Hashtable outValues, boolean localLuminanceCheck)
    {
        return validateQRCode(outValues, true, false);
    }

    public boolean validateQRCode(Hashtable outValues, boolean localLuminanceCheck, boolean scale) {
        if (scale)
        {
            try {
                this.currentImage = Thumbnails.of(currentImage).size(275, 275).asBufferedImage();

            } catch (IOException e) {
                logger.error("Image cannot be scaled. ", e);
            }
        }

        // finding luminance of the image
        LuminanceSource lumSource = new BufferedImageLuminanceSource(currentImage);

        Binarizer qrHB;
        if (!localLuminanceCheck) {
            qrHB = new GlobalHistogramBinarizer(lumSource);
        } else {
            // creating binary bitmap from Black-White image
            qrHB = new HybridBinarizer(lumSource);
            ((HybridBinarizer) qrHB).setBLOCK_SIZE_POWER(blockSizePower);
        }
        BinaryBitmap bitmap = new BinaryBitmap(qrHB);

        try {
            currentImage = MatrixToImageWriter.toBufferedImage(bitmap.getBlackMatrix());
        } catch (NotFoundException e) {
            logger.error("cannot find any bit matrix.", e);
        }

        Hashtable<DecodeHintType, Object> hint = new Hashtable<DecodeHintType, Object>();
        hint.put(DecodeHintType.TRY_HARDER, BarcodeFormat.QR_CODE);

        QRCodeReader QRreader = new QRCodeReader();

        try {
            // decodes the QR code
            Result result = QRreader.decode(bitmap, hint);

            resultText = result.getText();
            return true;
        } catch (NotFoundException e) {
            logger.info("cannot detect any QR code (no enough finder patterns).");
            return false;
        } catch (ChecksumException e) {
            logger.info("cannot recover the QR code. Too much data errors.");
            return false;
        } catch (FormatException e) {
            logger.info("QR code cannot be decoded.");
            return false;
        } catch (FinderPatternNotFoundException e) {
            // if no Finder Pattern has been found, it may be the color of
            // QR is inverted. So we invert the QR and try one more time

            Binarizer invertHB;
            if (!localLuminanceCheck) {
                invertHB = new GlobalHistogramBinarizer(lumSource);
            } else {
                invertHB = new HybridBinarizer(lumSource);
                ((HybridBinarizer) invertHB).setBLOCK_SIZE_POWER(blockSizePower);
            }

            // get the inverted Black-White matrix
            BitMatrix invertBlackMatrix = null;
            try {
                invertBlackMatrix = invertHB.getBlackMatrix();
            } catch (NotFoundException e1) {
                logger.error(e1);
            }

            int invertWidth = currentImage.getWidth();
            int invertHeight = currentImage.getHeight();

            // flip each bit in the inverted BitMatrix
            for (int x = 0; x < invertWidth; x++) {
                for (int y = 0; y < invertHeight; y++) {
                    invertBlackMatrix.flip(x, y);
                }
            }

            currentImage = MatrixToImageWriter.toBufferedImage(invertBlackMatrix);

            // get luminance source from inverted image
            lumSource = new BufferedImageLuminanceSource(currentImage);

            Binarizer afterInvertHB;
            if (!localLuminanceCheck) {
                afterInvertHB = new GlobalHistogramBinarizer(lumSource);

            } else {
                // creating binary bitmap from Black-White image
                afterInvertHB = new HybridBinarizer(lumSource);
                ((HybridBinarizer) afterInvertHB).setBLOCK_SIZE_POWER(blockSizePower);
            }
            BinaryBitmap invertBitMap = new BinaryBitmap(afterInvertHB);

            // decoding inverted QR
            QRCodeReader invertQRreader = new QRCodeReader();

            try {
                Result invertResult = invertQRreader.decode(invertBitMap, hint);

                resultText = invertResult.getText();

                System.out.println("Out put data is: " + resultText);

                return true;
            } catch (NotFoundException e1) {
                logger.info("cannot detect any QR code (no enough finder patterns).");
                return false;
            } catch (ChecksumException e1) {
                logger.info("cannot recover the QR code. Too much data errors.");
                return false;
            } catch (FormatException e1) {
                logger.info("QR code cannot be decoded.");
                return false;
            } catch (FinderPatternNotFoundException e1) {
                logger.info("Cannot confirm where all three Finder Patterns are! ");
                return false;
            } catch (Exception e1) {
                logger.error(e1);
                return false;
            }
        } catch (Exception e) {
            logger.error(e);
            return false;
        }
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱冒险 2025-01-11 07:24:02

这没有什么不同,可能是您没有使用 TRY_HARDER 模式,或者没有尝试两个二值化器。在线版本将做这些事情。

It's not different, it's probably that you are not using TRY_HARDER mode, or are not trying both binarizers. The online version will do those things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文