問題描述
我一直在徹底搜索 Google 和 StackOverflow,但找不到這個.也許我錯過了一些明顯的東西.謝謝!
I've been searching Google and StackOverflow exhaustively and cannot find this. Maybe I'm missing something obvious. Thanks!
(這是因為預覽回調的Java實現[即使有緩沖區]效率太低.)
(This is because the Java implementation of the preview callback [even with buffer] is too inefficient.)
推薦答案
我對主題做了一點調查.這個介紹(來自p.277,中文)幫助很大.
I made a little investigation on topic. This presentation (from p.277, Chinese) helped a lot.
正如其他人提到的,您可以使用 Camera.setPreviewCallback
方法獲取緩沖區.
以下是它的發生方式(詳細版本):
As others mentioned, you can get a buffer using a Camera.setPreviewCallback
method.
Here's how it happens there (a verbose version):
- 用戶調用
Camera.startPreview()
這是一個原生函數. android_hardware_Camera_startPreview
調用C++Camera
類的startPreview
方法.Camera
調用ICamera
接口的startPreview
方法ICamera
對遠程客戶端進行IPC
調用.- 調用
CameraService
類的setCameraMode
方法. CameraService
設置一個窗口顯示預覽并調用CameraHardwareInterface
類的startPreview
方法.- 后者嘗試在特定
camera_device_t
設備上調用start_preview
方法.
我沒有進一步查找,但它應該會調用驅動程序. - 當圖片到達時,調用
CameraService
的dataCallback
. - 將數據傳遞給客戶端的
handlePreviewData
方法. - 客戶端要么復制緩沖區,要么直接將其發送到
ICameraClient
. ICameraClient
通過IPC
將其發送到Camera
.Camera
調用已注冊的監聽器并將緩沖區傳遞給JNI
.- 它調用 Java 類中的回調.如果用戶使用
Camera.addCallbackBuffer
提供了一個緩沖區,則它首先復制到緩沖區. - 最后,Java 類
Camera
處理消息并調用Camera.PreviewCallback
的onPreviewFrame
方法.
- User calls
Camera.startPreview()
which is a native function. android_hardware_Camera_startPreview
callsstartPreview
method of C++Camera
class.Camera
calls astartPreview
method ofICamera
interfaceICamera
makes anIPC
call to remote client.- It calls a
setCameraMode
method ofCameraService
class. CameraService
sets a window to display a preview and calls astartPreview
method ofCameraHardwareInterface
class.- The latter tries to call a
start_preview
method on particularcamera_device_t
device.
I didn't looked up further but it should perform a call to the driver. - When image arrives,
dataCallback
ofCameraService
is invoked. - It passes data to
handlePreviewData
method of client. - Client either copies the buffer or sends it directly to the
ICameraClient
. ICameraClient
sends it overIPC
to theCamera
.Camera
calls a registered listener and passes buffer toJNI
.- It invokes a callback in Java class. If user provided a buffer with
Camera.addCallbackBuffer
then it copies to the buffer first. - Finally Java class
Camera
handles the message and invokes aonPreviewFrame
method ofCamera.PreviewCallback
.
如您所見,調用了 2 個 IPC
調用,并且在步驟 10、11 中至少復制了兩次緩沖區.camera_device_t
返回的原始緩沖區的第一個實例是托管在另一個進程中,由于 CameraService
中的安全檢查,您無法訪問它.
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
.
但是,當您使用 Camera.setPreviewTexture
或 Camera.setPreviewDisplay
設置預覽表面時,它會直接傳遞給相機設備并實時刷新,而無需上面的所有鏈條.正如它的文檔所說:
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 類 Surface
有一個方法來檢索它的內容:
Java class Surface
has a method to retrieve it's contents:
public static native Bitmap screenshot(int width, int height, int minLayer, int maxLayer);
但是這個 API 是隱藏的.參見 這個問題 了解使用方法.
But this API is hidden. See i.e. this question for a way to use it.
這篇關于如何使用 JNI 在 C 中獲取原始 Android 相機緩沖區?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!