Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

java - How do I get the raw Android camera buffer in C using JNI?

I've been searching Google and StackOverflow exhaustively and cannot find this. Maybe I'm missing something obvious. Thanks!

(This is because the Java implementation of the preview callback [even with buffer] is too inefficient.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I made a little investigation on topic. This presentation (from p.277, Chinese) helped a lot.

Camera preview call chain

As others mentioned, you can get a buffer using a Camera.setPreviewCallback method.
Here's how it happens there (a verbose version):

  1. User calls Camera.startPreview() which is a native function.
  2. android_hardware_Camera_startPreview calls startPreview method of C++ Camera class.
  3. Camera calls a startPreview method of ICamera interface
  4. ICamera makes an IPC call to remote client.
  5. It calls a setCameraMode method of CameraService class.
  6. CameraService sets a window to display a preview and calls a startPreview method of CameraHardwareInterface class.
  7. The latter tries to call a start_preview method on particular camera_device_t device.
    I didn't looked up further but it should perform a call to the driver.
  8. When image arrives, dataCallback of CameraService is invoked.
  9. It passes data to handlePreviewData method of client.
  10. Client either copies the buffer or sends it directly to the ICameraClient.
  11. ICameraClient sends it over IPC to the Camera.
  12. Camera calls a registered listener and passes buffer to JNI.
  13. It invokes a callback in Java class. If user provided a buffer with Camera.addCallbackBuffer then it copies to the buffer first.
  14. Finally Java class Camera handles the message and invokes a onPreviewFrame method of Camera.PreviewCallback.

As you can see 2 IPC calls were invoked and buffer was copied at least twice on steps 10, 11. First instance of raw buffer which is returned by camera_device_t is hosted in another process and you cannot access it due to security checks in CameraService.

Preview surface

However, when you set a preview surface using either Camera.setPreviewTexture or Camera.setPreviewDisplay it is be passed directly to the camera device and refreshed in realtime without participation of all the chain above. As it's documentation says:

Handle onto a raw buffer that is being managed by the screen compositor.

Java class Surface has a method to retrieve it's contents:

public static native Bitmap screenshot(int width, int height, int minLayer, int maxLayer);

But this API is hidden. See i.e. this question for a way to use it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...