Android ImageReader.acquireLatestImage返回无效的JPG

时间:2014-10-31 11:03:05

标签: android bitmap android-5.0-lollipop

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

到目前为止我的代码看起来像这样:

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 并且像这样:

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返回的图像对象不是null但也不是有效的JPEG,我试图检查以下测试失败:

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调用。

有什么想法吗?

6 个答案:

答案 0 :(得分:7)

我测试了第一个答案的代码,但遗憾的是它不能在真实设备上运行。我做了一些调查,下面的代码解决了我的问题:

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

答案 1 :(得分:7)

@charlesjean回答的代码有效,但我宁愿不自己生成每个像素。从ImageReader获取Image的更好方法是创建正确大小的位图并使用方法copyPixelsFromBuffer()。按如下方式创建ImageReader:

mImageReader = ImageReader.newInstance(mWidth, mHeight, ImageFormat.RGB_565, 2);

然后你可以使用下面的代码从mImageReader获取图像。

final Image.Plane[] planes = image.getPlanes();
final ByteBuffer buffer = planes[0].getBuffer();
int offset = 0;
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * mWidth;
// create bitmap
bitmap = Bitmap.createBitmap(mWidth+rowPadding/pixelStride, mHeight, Bitmap.Config.RGB_565);
bitmap.copyPixelsFromBuffer(buffer);
image.close();

我已经描述了使用MediaProjection API捕获屏幕的过程,以及大多数人在blog post中从ImageReader获取图像时所犯的错误,如果感兴趣,您可以阅读。

答案 2 :(得分:5)

我遇到了你的问题。我的ImageReader创建如下:

ImageReader.newInstance(mCaptureSize.getWidth(), mCaptureSize.getHeight(), ImageFormat.JPEG, 1);

上面的ImageReader应该只返回压缩图像,这些图像需要解压缩。我获取了LimalImage(),然后将其传递给以下内容:

ByteBuffer bBuffer = planes[0].getBuffer;
bBuffer.rewind();
byte[] buffer = new byte[bBuffer.remaining()];
planes[0].getBuffer().get(buffer);
Bitmap bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);

对我而言,关键是回放ByteBuffer。您的代码应该如下:

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();
                        buffer.rewind()
                        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);

我不喜欢通过中间字节[]复制ByteBuffer,但内部数组受到保护。

在HTC上测试了5.0.1的工作

答案 3 :(得分:4)

如果其他人偶然发现,工作代码如下:

            mImageReader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 5);
            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;
                    FileOutputStream fos = null;
                    Bitmap bitmap = null;

                    try {
                        image = mImageReader.acquireLatestImage();
                        fos = new FileOutputStream(getFilesDir() + "/myscreen.jpg");
                        final Image.Plane[] planes = image.getPlanes();
                        final Buffer buffer = planes[0].getBuffer().rewind();
                        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                        bitmap.copyPixelsFromBuffer(buffer);
                        bitmap.compress(CompressFormat.JPEG, 100, fos);

                    } catch (Exception e) {
                        e.printStackTrace();

                        if (image!=null)
                            image.close();
                    } finally {
                        if (fos!=null) {
                            try {
                                fos.close();
                            } catch (IOException ioe) { 
                                ioe.printStackTrace();
                            }
                        }

                        if (bitmap!=null)
                            bitmap.recycle();
                    }
                }

            }, mHandler);

如您所见,我将从ImageReader捕获的位图保存到fileoutput流,这会生成一个有效的jpeg文件。

我从 android_media_ImageReader.cpp 回来的消息并没有表明任何形式的不当行为。

希望将来帮助某人!

答案 4 :(得分:1)

我尝试使用许多示例代码。但是这些代码都不起作用。我自己混合了这些代码。我创建了工作示例代码。

    final DisplayMetrics dm = getResources().getDisplayMetrics();
    mImageReader = ImageReader.newInstance(dm.widthPixels, dm.heightPixels, PixelFormat.RGBA_8888, 1);
    mProjection.createVirtualDisplay("screen-mirror", dm.widthPixels, dm.heightPixels, dm.densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), vdCallback, mHandler);
    mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
        @Override
        public void onImageAvailable(ImageReader reader) {
            Image image = reader.acquireLatestImage();
            if (image == null){
                return;
            }
            final Image.Plane[] planes = image.getPlanes();
            final ByteBuffer buffer = planes[0].getBuffer();
            int offset = 0;
            int pixelStride = planes[0].getPixelStride();
            int rowStride = planes[0].getRowStride();
            int rowPadding = rowStride - pixelStride * dm.widthPixels;
            // create bitmap
            Bitmap bitmap = Bitmap.createBitmap(dm.widthPixels+rowPadding/pixelStride, dm.heightPixels, Bitmap.Config.ARGB_8888);
            bitmap.copyPixelsFromBuffer(buffer);
            imageView.setImageBitmap(bitmap);
            image.close();
        }
    }, mHandler);

答案 5 :(得分:0)

对于那些正在寻找从ImageReader捕获到JPG的最简单方法(无论是在磁盘上还是在内存中的字节)的人,这实际上很简单。

给出一个ImageReader,将其设置为ImageFormat.JPEG:

ImageReader imageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), imageFormat, 1);

然后在CameraCaptureSession.StateCallback中:

captureSession.capture(captureRequest, new CameraCaptureSession.CaptureCallback() {
    @Override
    public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
        super.onCaptureCompleted(session, request, result);             
        Image image = imageReader.acquireLatestImage();
        final Image.Plane[] planes = image.getPlanes();
        final ByteBuffer buffer = planes[0].getBuffer();
        buffer.rewind();
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);

    }
}

然后您可以使用bytes进行任何操作。通过套接字将它们发送到文件中,对其进行哈希处理,等等。