我正在 Android 上做一个简单的应用程序,如下所示:
将二维码图像放入应用程序的 Drawable 文件中。通过 ButtonClick,应该对其进行解码并显示结果(使用 Zxing 库)。
我在 Java 上制作了相同的应用程序(然后使用 BufferedImageLuminanceSource
类进行解码)。
在我的 Android 应用程序中,我使用了 RGBLuminanceSource
类,如下所示:
LuminanceSource source = new RGBLuminanceSource(width, height, pixels)BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
我在这里面临的问题是:图像必须太小而无法被Android应用程序解码(并且我必须尝试多种尺寸才能最终得到一个可以解码QR码图像的图像)。同时,在 Java 应用程序中使用 BufferedImageLuminanceSource 可以轻松解码相同的图像,无需调整大小。
如何避免此调整大小问题?
最佳答案
为时已晚,但可以对其他人有所帮助,
因此我们可以使用Zxing库从Bitmap中获取二维码信息。
Bitmap generatedQRCode;
int width = generatedQRCode.getWidth();
int height = generatedQRCode.getHeight();
int[] pixels = new int[width * height];
generatedQRCode.getPixels(pixels, 0, width, 0, 0, width, height);
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
Result result = null;
try {
result = reader.decode(binaryBitmap);
} catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
}
String text = result.getText();
textViewQRCode.setText(" CONTENT: " + text);
关于android - 使用zxing android进行二维码解码图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32120988/