Java 发布的ZXing 2.0是否与在线ZXing解码器不同?

Java 发布的ZXing 2.0是否与在线ZXing解码器不同?,java,zxing,Java,Zxing,我正在尝试使用ZXing库开发一个用于解码二维码的Java项目。然而,一些包含QR码的图像无法通过运行我的项目进行解码,但这些图像可以与在线ZXing解码器一起正常工作。我只是好奇ZXing发布的版本是否与他们用于在线解码器的版本相同?或者他们调整了在线版本。因为这种混乱,我在胡思乱想 public class Validator implements IValidator { private static Logger logger = Logger.getLogger(Validat

我正在尝试使用ZXing库开发一个用于解码二维码的Java项目。然而,一些包含QR码的图像无法通过运行我的项目进行解码,但这些图像可以与在线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;
        }
    }

}

这没什么不同,可能是您没有使用TRY_HARDER模式,或者没有同时尝试两种二进制代码。在线版本将完成这些操作。

我已经发布了我的代码,请查看一下。在代码中,我加入了TRY_HARDER并检查了HybridBinarizer和GlobalHistorogrambinarizer。请指出可能导致问题的位置。非常感谢。