我正在Android上做一个简单的应用程序,如下所示:
将QR码图像放入应用程序的Drawable文件中。通过ButtonClick,应将其解码并显示结果(使用Zxing库)。
我在Java上做了相同的应用程序(然后使用BufferedImageLuminanceSource类进行了解码)。
在我的android应用程序中,我使用了以下RGBLuminanceSource类:
LuminanceSource source = new RGBLuminanceSource(width, height, pixels)BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
Run Code Online (Sandbox Code Playgroud)
我在这里面临的问题是:图像必须太小而不能被android应用程序解码(而且我不得不尝试许多尺寸才能最终获得解码QR码图像的尺寸)。同时,使用BufferedImageLuminanceSourceJava应用程序可以轻松解码相同的图像,而无需调整大小。
如何避免此大小调整问题?
为时已晚,但对其他人有帮助,
因此,我们可以使用Zxing库从Bitmap获取Qr代码信息。
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3690 次 |
| 最近记录: |