ZXing 2.0发布与在线ZXing解码器有什么不同吗?
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这没有什么不同,可能是您没有使用 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.