zxing
找不到结果。用我的手机扫描是没有问题的。
我正在使用最新的 Java zwing 3.5.2。
这里是使用的算法(辛苦的工作是由
zxing
完成的):
class Results {
public final double rotation;
public final List<Result> results;
Results(double rotation, List<Result> results) {
this.rotation = rotation;
this.results = results;
}
}
protected Results getResults(BufferedImage image) {
LuminanceSource source = new BufferedImageLuminanceSource(image);
Reader reader = new MultiFormatReader();
for (int i=0; i<2; ++i) {
BinaryBitmap bitmap;
if (i == 0) {
bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
} else {
bitmap = new BinaryBitmap(new HybridBinarizer(source));
}
for (int r=0; r<4; ++r) {
if (r > 0) {
bitmap = bitmap.rotateCounterClockwise();
}
List<Result> results = tryDetect(reader, bitmap);
if (results != null) {
return new Results(r*90, results);
}
}
}
return null;
}
protected List<Result> tryDetect(Reader reader, BinaryBitmap bitmap) {
// We should just be catching ReaderException in here but we discovered
// an ArrayIndexOutOfBoundsException in the zxing library which prevents
// us from continuing. So we catch everything.
try {
// Look for multiple barcodes
MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
Result[] theResults = multiReader.decodeMultiple(bitmap, HINTS);
if (theResults != null) {
return Arrays.asList(theResults);
}
} catch (Exception re) {
}
try {
// Look for multiple barcodes by quadrant
MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(new ByQuadrantReader(reader));
Result[] theResults = multiReader.decodeMultiple(bitmap, HINTS);
if (theResults != null) {
return Arrays.asList(theResults);
}
} catch (Exception re) {
}
try {
// Look for pure barcode
Result theResult = reader.decode(bitmap, HINTS_PURE);
if (theResult != null) {
return Arrays.asList(theResult);
}
} catch (Exception re) {
}
try {
// Look for normal barcode in photo
Result theResult = reader.decode(bitmap, HINTS);
if (theResult != null) {
return Arrays.asList(theResult);
}
} catch (Exception re) {
}
return null;
}
}
我见过的几乎所有
zxing
的用法都非常相似。
我可能是
zwing
或我的代码中的错误。如何克服这个限制?
我尝试通过插值来提高图像的分辨率,但没有成功。
有同样的问题,可以通过在解码之前向 qrCode 图像添加白色边框来解决它
int borderSize = 50;
BufferedImage borderedImage = new BufferedImage(
resizedQrCodeImage.getWidth() + 2 * borderSize,
resizedQrCodeImage.getHeight() + 2 * borderSize,
resizedQrCodeImage.getType());
Graphics2D g2d = borderedImage.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, borderedImage.getWidth(), borderedImage.getHeight());
g2d.drawImage(resizedQrCodeImage, borderSize, borderSize, null);
g2d.dispose();
然后简单地说:
try {
LuminanceSource source = new BufferedImageLuminanceSource(borderedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = new MultiFormatReader().decode(bitmap);
return Ezvcard.parse(result.getText()).first(); //do not pay attention, simply parsing to the VCard object for my needs
} catch (NotFoundException e) {
throw new RuntimeException("Error - qr code can not be decoded.", e);
}