从存储在手机上使用Zxing图像解码QR code(在Android手机) [英] Decoding qr code from image stored on the phone with Zxing (on Android phone)

查看:848
本文介绍了从存储在手机上使用Zxing图像解码QR code(在Android手机)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序从服务器接收QR code。我想脱$ C C是$(不是故意和摄像机),并显示其包含的文字在我的应用程序。我已经alredy从这个code zxing做到了这一点在Java SE与罐子:

I have an app that receives qr code from the server. I want to decode it (not with intent and camera) and display the text it contains in my app. I have alredy done this in Java SE with jars from zxing with this code:

 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();

         }
        }

不过,在Android上的BufferedImage没有找到。 有没有人去codeD QR $ C $从图像的android下贮存在手机上? TNX。

But on Android, BufferedImage is not found. Has anyone decoded qr code on android from image stored on the phone? Tnx.

推荐答案

在Android中,你可以这样来做:

In android,you can do it this way:

    @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;
        }
    }

这篇关于从存储在手机上使用Zxing图像解码QR code(在Android手机)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆