1

I was looking at this class : https://developer.android.com/reference/android/media/ImageReader.html

What I want is to get an InputStream from the ImageReader class and get the image data which is being rendering into the current surface. Current Surface means the currently displaying android screen. I looked at an example inside the AOSP code but it was really tough to understand. Please it would be really helpful if someone provides me with a nice example of ImageReader class.

omerjerk
  • 4,090
  • 5
  • 40
  • 57
  • Take look on this thread http://stackoverflow.com/questions/5688104/why-does-imagereader-return-incorrect-bufferedimage – mohammed momn Feb 08 '14 at 15:29
  • The documentation says use `acquireLatestImage()` method to grab the Image. – DevZer0 Feb 08 '14 at 15:29
  • @mohammedmomn Thanks for your response but I need to use android.media.ImageReader – omerjerk Feb 08 '14 at 16:00
  • @DevZer0 But I'm not getting how to set the Surface. I want the image that is being shown in the current screen. – omerjerk Feb 08 '14 at 16:02
  • Can you show some of your code what your doing, do you have a instance of the ImageReader you want to use? – DevZer0 Feb 08 '14 at 16:08
  • I wrote some random code using this : https://android.googlesource.com/platform/cts/+/kitkat-release/tests/tests/hardware/src/android/hardware/camera2/cts/ImageReaderTest.java – omerjerk Feb 08 '14 at 18:01
  • I wrote some random code using this : https://android.googlesource.com/platform/cts/+/kitkat-release/tests/tests/hardware/src/android/hardware/camera2/cts/ImageReaderTest.java . But I'm not able to get how to get Images from the currently displaying screen in android using ImageReader. – omerjerk Feb 08 '14 at 18:03
  • This is my random code : http://pastebin.com/dQVj74XR – omerjerk Feb 08 '14 at 18:05
  • I found another test regarding ImageReader : https://android.googlesource.com/platform/cts/+/33286bc/tests/tests/display/src/android/display/cts/VirtualDisplayTest.java If anyone could help. – omerjerk Feb 10 '14 at 11:24
  • You can refer to this question http://stackoverflow.com/questions/27219799/system-error-capturing-the-output-of-a-mediaprojection-virtual-display-to-an-ima/27638215?noredirect=1#comment43774763_27638215 – Charlesjean Dec 31 '14 at 01:27

1 Answers1

7

It sounds like you're trying to use ImageReader to pull data out of an arbitrary Surface. It doesn't work that way.

ImageReader is a buffer queue "CPU consumer", which is a fancy way of saying that when you send data to its Surface, code running on the CPU can examine the data directly. Not all surfaces can be handled this way -- the usage flags are set when the surface is created -- so you need to create the Surface with ImageReader#getSurface() and then pass that to something that wants to send data to a Surface.

As the documentation notes, "Several Android media API classes accept Surface objects as targets to render to, including MediaPlayer, MediaCodec, and RenderScript Allocations."

The primary customer for ImageReader is the camera, demonstrated in the CTS test you linked to. It doesn't really work with MediaCodec decoders yet because the output formats are different from camera (and some of them are proprietary).

The virtual display test is closer to what you want, because you can direct the output of a virtual display to a Surface. For an app with normal privileges you can only send your own app's output there. The Android 4.4 "screenrecord" command does something similar.

(If you want to record what you're drawing on a SurfaceView, and you're rendering with OpenGL, there are some alternatives demonstrated here and here.)

fadden
  • 51,356
  • 5
  • 116
  • 166