Dix*_*ine 6 java android decode qr-code
我有一个从服务器接收qr代码的应用程序.我想解码它(不是意图和相机)并在我的应用程序中显示它包含的文本.我已经在Java SE中使用此代码使用来自zxing的jar来完成此操作:
private class QRCodeDecoder {
public String decode(File imageFile) {
BufferedImage image;
try {
image = ImageIO.read(imageFile);
} catch (IOException e1) {
return "io outch";
}
// creating luminance source
LuminanceSource lumSource = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(lumSource));
// barcode decoding
QRCodeReader reader = new QRCodeReader();
Result result = null;
try {
result = reader.decode(bitmap);
} catch (ReaderException e) {
return "reader error";
}
return result.getText();
}
}
Run Code Online (Sandbox Code Playgroud)
但在Android上,找不到BufferedImage.有没有人从手机上存储的图像解码android上的qr代码?TNX.
小智 14
在android中,你可以这样做:
@Override
protected Result doInBackground(Void... params)
{
try
{
InputStream inputStream = activity.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
if (bitmap == null)
{
Log.e(TAG, "uri is not a bitmap," + uri.toString());
return null;
}
int width = bitmap.getWidth(), height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
bitmap.recycle();
bitmap = null;
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader reader = new MultiFormatReader();
try
{
Result result = reader.decode(bBitmap);
return result;
}
catch (NotFoundException e)
{
Log.e(TAG, "decode exception", e);
return null;
}
}
catch (FileNotFoundException e)
{
Log.e(TAG, "can not open file" + uri.toString(), e);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 -3
Quickmark 和 qr droid 实际上会读出代码的内容,并且您可以解码手机上保存的条形码。当您加载图像并选择共享时,点击菜单按钮,找到“解码 qr droid”或“解码快速标记”,然后它们就会发挥作用。我更喜欢使用快速标记来阅读代码,因为它可以告诉我代码中输入的内容。
| 归档时间: |
|
| 查看次数: |
13066 次 |
| 最近记录: |