Android ImageReader.acquireLatestImage 返回无效的 JPG [英] Android ImageReader.acquireLatestImage returns invalid JPG

查看:17
本文介绍了Android ImageReader.acquireLatestImage 返回无效的 JPG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Android ImageReader 类从 MediaProjection.createVirtualDisplay 方法接收位图.

I am using Android ImageReader class to receive Bitmaps from MediaProjection.createVirtualDisplay method.

到目前为止我的代码是这样的:

My code so far looks like this:

mProjection.createVirtualDisplay("test", width, height, density, flags, mImageReader.getSurface(), new VirtualDisplayCallback(), mHandler);
            mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
                @Override
                public void onImageAvailable(ImageReader reader) {
                    Image image = null;
                    try {
                        image = mImageReader.acquireLatestImage();
                        final Image.Plane[] planes = image.getPlanes();
                        final ByteBuffer buffer = planes[0].getBuffer();
                        final byte[] data = new byte[buffer.capacity()];
                        buffer.get(data);
                        final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                        if (bitmap==null)
                            Log.e(TAG, "bitmap is null");

                    } catch (Exception e) {
                        if (image!=null)
                            image.close();
                    }
                }

            }, mHandler);

问题是 BitmapFactory 无法将 data[] 解码回 Bitmap,即 BitmapFactory 总是返回 null.我从 logcat 看到的唯一消息来自 android_media_ImageReader.cpp 并且像这样:

The problem is that BitmapFactory cannot decode data[] back to Bitmap, i.e. BitmapFactory always returns null. The only messages I see from logcat come from android_media_ImageReader.cpp and go like this:

D/ImageReader_JNI(1432): ImageReader_imageSetup: Receiving JPEG in HAL_PIXEL_FORMAT_RGBA_8888 buffer.
W/ImageReader_JNI(1432): Image_getJpegSize: No JPEG header detected, defaulting to size=width=3891200

acquireLatestImage 返回的图像对象不为空但也不是有效的 JPEG,我尝试检查以下失败的测试:

Image object returned by acquireLatestImage is not null but not a valid JPEG either, I tried to check with the following test which fails:

if((buf [0] & 0xFF) == 0xFF && (buf[1] & 0xFF) == 0xD8 && (buf[2] & 0xFF) == 0xFF && (buf[3] & 0xFF) == 0xE0)
    Log.e(TAG, "is JPG");
else
    Log.e(TAG, "not a valid JPG");

目前我唯一怀疑的是我正在测试的 Android 5.0 模拟器无法处理 API 调用.

The only think I am suspecting at the moment is that Android 5.0 emulator I am testing against cannot hanlde the API calls.

有什么想法吗?

推荐答案

我测试了第一个答案的代码,但不幸的是它不适用于真机.我进行了一些调查,以下代码解决了我的问题:

I tested the code of the first answer, but unfortunately it does not work on real device. I make some investigation and the following code solved my problem:

 mImgReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 5);
    mSurface = mImgReader.getSurface();// mSurfaceView.getHolder().getSurface();
    mImgReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
        @Override
        public void onImageAvailable(ImageReader reader) {
            Log.i(TAG, "in OnImageAvailable");
            FileOutputStream fos = null;
            Bitmap bitmap = null;
            Image img = null;
            try {
                img = reader.acquireLatestImage();
                if (img != null) {
                    Image.Plane[] planes = img.getPlanes();
                    if (planes[0].getBuffer() == null) {
                        return;
                    }
                    int width = img.getWidth();
                    int height = img.getHeight();
                    int pixelStride = planes[0].getPixelStride();
                    int rowStride = planes[0].getRowStride();
                    int rowPadding = rowStride - pixelStride * width;
                    byte[] newData = new byte[width * height * 4];

                    int offset = 0;
                    bitmap = Bitmap.createBitmap(metrics,width, height, Bitmap.Config.ARGB_8888);
                    ByteBuffer buffer = planes[0].getBuffer();
                    for (int i = 0; i < height; ++i) {
                        for (int j = 0; j < width; ++j) {
                            int pixel = 0;
                            pixel |= (buffer.get(offset) & 0xff) << 16;     // R
                            pixel |= (buffer.get(offset + 1) & 0xff) << 8;  // G
                            pixel |= (buffer.get(offset + 2) & 0xff);       // B
                            pixel |= (buffer.get(offset + 3) & 0xff) << 24; // A
                            bitmap.setPixel(j, i, pixel);
                            offset += pixelStride;
                        }
                        offset += rowPadding;
                    }
                    String name = "/myscreen" + count + ".png";
                    count++;
                    File file = new File(Environment.getExternalStorageDirectory(), name);
                    fos = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    Log.i(TAG, "image saved in" + Environment.getExternalStorageDirectory() + name);
                    img.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != fos) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (null != bitmap) {
                    bitmap.recycle();
                }
                if (null != img) {
                    img.close();
                }

            }



        }
    }, mHandler);

这篇关于Android ImageReader.acquireLatestImage 返回无效的 JPG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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