8

我有一个从服务器接收二维码的应用程序。我想解码它(不是用意图和相机)并在我的应用程序中显示它包含的文本。我已经在 J​​ava SE 中使用来自 zxing 的 jars 使用以下代码完成了此操作:

 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。有没有人从手机上存储的图像中解码出安卓上的二维码?肿瘤坏死因子。

4

3 回答 3

18

在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;
        }
    }
于 2013-01-06T03:27:45.683 回答
2

从谷歌代码下载ZXing,这个类文件:ZXing-1.6/zxing-1.6/androidtest/src/com/google/zxing/client/androidtest/RGBLuminanceSource.java可以帮助你。

于 2011-07-12T03:14:47.800 回答
-3

Quickmark 和 qr droid 实际上会读出代码的内容,您可以对保存在手机上的条形码进行解码。当您加载图像并选择共享时,点击菜单按钮,找到 decode qr droid 或 decode quickmark,然后就会变魔术。我更喜欢用 quickmark 来阅读代码,因为它告诉我在代码中输入了什么。

于 2011-02-17T00:08:35.233 回答