如何从图像扫描二维码(不形成相机) [英] How to scan qr code from image(not form camera)

查看:16

我的问题是:

  1. 如何将图像文件从 sd 卡获取到应用程序中
  2. 最后是如何解码条码图像文件并存储解码后的结果.

但是我从 zxing 库制作了 QR 扫描仪.我的意思是.如何从图像扫描二维码(不是相机)

解决方案

我想在发布之前对其进行测试,所以花了我一段时间,我现在也在使用 ZXing 所以这对我来说也很方便:

当然,首先从图库中读取图像(这可以在您的活动中):

 Intent pickIntent = new Intent(Intent.ACTION_PICK);pickIntent.setDataAndType( android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");startActivityForResult(pickIntent, 111);

之后,只需获取活动结果上的图像uri,然后ZXing就会发挥作用:

@覆盖protected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);开关(请求代码){//这种情况是因为您可能在这里处理多个请求代码案例111:if(data == null || data.getData()==null) {Log.e("TAG", "uri为空,可能是用户使用后退按钮取消了图片选择过程.");返回;}uri uri = data.getData();尝试{InputStream inputStream = getContentResolver().openInputStream(uri);位图 bitmap = BitmapFactory.decodeStream(inputStream);如果(位图 == 空){Log.e("TAG", "uri 不是位图" + uri.toString());返回;}int width = bitmap.getWidth(), height = bitmap.getHeight();int[] 像素 = 新的 int[宽 * 高];bitmap.getPixels(pixels, 0, width, 0, 0, width, height);bitmap.recycle();位图 = 空;RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixel);BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));MultiFormatReader reader = new MultiFormatReader();尝试{结果 result = reader.decode(bBitmap);Toast.makeText(this, "二维码图片内容为:" + result.getText(), Toast.LENGTH_SHORT).show();}catch (NotFoundException e){Log.e("TAG", "解码异常", e);}}catch (FileNotFoundException e){Log.e("TAG", "无法打开文件" + uri.toString(), e);}休息;}}

我对此进行了测试,效果很好,加油.

  • 原文

My Question is:

  1. How to get a image file from sd card onto the application
  2. Finally how to decode the barcode image file and store the decoded result.

But i have made QR Scanner from zxing library. I mean. How to scan qr code from image(not form camera)

解决方案

I wanted to test it out before posting it so it took me a while, I'm also using ZXing right now so this comes handy for me as well:

First of course, read the image from the gallery (this can be in your activity):

        Intent pickIntent = new Intent(Intent.ACTION_PICK);
        pickIntent.setDataAndType( android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");

        startActivityForResult(pickIntent, 111);

After that, just get the image uri on the activity result and then ZXing will do the magic:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            //the case is because you might be handling multiple request codes here
            case 111:
                if(data == null || data.getData()==null) {
                    Log.e("TAG", "The uri is null, probably the user cancelled the image selection process using the back button.");
                    return;
                }
                Uri uri = data.getData();
                try
                {
                    InputStream inputStream = getContentResolver().openInputStream(uri);
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    if (bitmap == null)
                    {
                        Log.e("TAG", "uri is not a bitmap," + uri.toString());
                        return;
                    }
                    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);
                        Toast.makeText(this, "The content of the QR image is: " + result.getText(), Toast.LENGTH_SHORT).show();
                    }
                    catch (NotFoundException e)
                    {
                        Log.e("TAG", "decode exception", e);
                    }
                }
                catch (FileNotFoundException e)
                {
                    Log.e("TAG", "can not open file" + uri.toString(), e);
                }
                break;
        }
    }

I tested this and it works, cheers.