Android Sdk: Get Raw Preview Camera Image Without Displaying It

Android SDK: Get raw preview camera image without displaying it

In Android 4, the simplest option to receiving raw image data without displaying it on screen is to use the Camera.setPreviewTexture() call to route the preview frames to the GPU.

You can use this in two ways:

  1. Do your actual processing on the GPU: Set up an OpenGL context (OpenGL ES 2 tutorial), and create a SurfaceTexture object in that context. Then pass that object to setPreviewTexture, and start preview. Then, in your OpenGL code, you can call SurfaceTexture.updateTexImage, and the texture ID associated with the SurfaceTexture will be updated to the latest preview frame from the camera. You can also read back the RGB texture data to the CPU for further processing using glReadPixels, if desired.
  2. Do your processing on the CPU: You can simply create a dummy SurfaceTexture object without any OpenGL context set up. Pass any integer you want as the texture ID, and connect the SurfaceTexture to the camera using setPreviewTexture. As long as you don't call updateTexImage, the SurfaceTexture will simply discard all data passed into it by the camera. Then set up preview callbacks using setPreviewCallback, and use that data (typically in a YUV format) for CPU processing. This is probably less efficient than #1, but does not require knowing OpenGL.

Taking picture from camera without preview

it is really weird that camera on android platform can't stream video until it given valid preview surface. it seems that the architects of the platform was not thinking about 3rd party video streaming applications at all. even for augmented reality case the picture can be presented as some kind of visual substitution, not real time camera stream.

anyway, you can simply resize preview surface to 1x1 pixels and put it somewhere in the corner of the widget (visual element). please pay attention - resize preview surface, not camera frame size.

of course such trick does not eliminate unwanted data streaming (for preview) which consumes some system resources and battery.

Can we use takePicture() in android without camera preview? I need to take a picture secretly for security purposes

According to android.developer.com, no.

https://developer.android.com/reference/android/hardware/Camera.html

"Important: Call startPreview() to start updating the preview surface. Preview must be started before you can take a picture."

Can I use Android Camera in service without preview?

You can refer this Question, which discuss how to do video record in the service. The steps to capture image is same as it.

To achieve your requirement, you may need:

  1. Get camera instance in your service. Check this Official Guideline.
  2. Setup your camera parameters. Use the APIs like Camera.getParameters(), Camera.setParameters(), Camera.Parameters.setPictureSize(int with, int height) and Camera.Parameters.setPictureFormat(int format) to do so.
  3. Prepare a file used to store the image. You need to implement Camera.PictureCallback to do so.
  4. Call Camera.startPreview() before you takeing picutre. If you don't call this function before taking picture, you will get exception. (Note: You don't need to do Camera.setPreviewdisplay(SurfaceHolder display) first.)
  5. Call camera.takePicture() in your service. Then you can get the captured image stored on the file you specified.

After it work, don forget to maintain the resource durning lifecycle to acquire/release camera correctly.

Here is my sample code on Github, it is also mentioned in the comment.



Related Topics



Leave a reply



Submit