Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

java - Decoding qr code from image stored on the phone with Zxing (on Android phone)

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

         }
        }

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...