歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android Camera調用流程

Android中Camera的調用流程可分為以下幾個層次:
Package->Framework->JNI->Camera(cpp)--(binder)-->CameraService->Camera HAL->Camera Driver

以拍照流程為例:
1. 各個參數設置完成,對焦完成後,位於Package的Camera.java會調用Framework中Camera.java的takePicture函數,如下:
public final void takePicture(ShutterCallback shutter, PictureCallback raw,
            PictureCallback postview, PictureCallback jpeg) {
        mShutterCallback = shutter;
        mRawImageCallback = raw;
        mPostviewCallback = postview;
        mJpegCallback = jpeg;
        native_takePicture();
}
此函數保存Package層傳下的callback函數,同時調用JNI層的native_takePicture

2. JNI層的native_takePicture自己並沒有做太多事情,只是簡單地調用cpp的Camera中的takePicture函數。此前已經把JNI中的一個對象注冊成了Camera.cpp的listener

3. 位於frameworks/base/libs/camera是向CameraService請求服務的客戶端,但它本身也繼承了一個BnCameraClient類,用於CameraService回調自己。
class ICameraClient: public IInterface
{
public:
    DECLARE_META_INTERFACE(CameraClient);

    virtual void            notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2) = 0;
    virtual void            dataCallback(int32_t msgType, const sp<IMemory>& data) = 0;
    virtual void            dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& data) = 0;
};
從上面的接口定義可以看到,這個類就是用於回調。

Camera.cpp的takePicture函數是利用open Camera時得到的ICamera對象來繼續調用takePicture

4. 接下來通過binder轉到另一個進程CameraService中的處理。CameraService中之前已經實例化了一個HAL層的CameraHardware,並把自己的data callback傳遞給了CameraHardware,這些工作都是由CameraService的內部類Client來完成的,這個Client類繼承自BnCamera,是真正提供Camera操作API的類

5. 然後自然是調用HAL層CameraHardware的takePicture函數。從HAL層向下就不是Android的標准代碼了,各個廠商有自己不同的實現。但思路應該都是相同的:Camera遵循V4L2架構,利用ioctl發送VIDIOC_DQBUF命令得到有效的圖像數據,接著回調HAL層的data callback接口以通知CameraService,CameraService會通過binder通知Camera.cpp,如下:
void CameraService::Client::dataCallback(int32_t msgType,
        const sp<IMemory>& dataPtr, void* user) {
    LOG2("dataCallback(%d)", msgType);

    sp<Client> client = getClientFromCookie(user);
    if (client == 0) return;
    if (!client->lockIfMessageWanted(msgType)) return;

    if (dataPtr == 0) {
        LOGE("Null data returned in data callback");
        client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
        return;
    }

    switch (msgType) {
        case CAMERA_MSG_PREVIEW_FRAME:
            client->handlePreviewData(dataPtr);
            break;
        case CAMERA_MSG_POSTVIEW_FRAME:
            client->handlePostview(dataPtr);
            break;
        case CAMERA_MSG_RAW_IMAGE:
            client->handleRawPicture(dataPtr);
            break;
        case CAMERA_MSG_COMPRESSED_IMAGE:
            client->handleCompressedPicture(dataPtr);
            break;
        default:
            client->handleGenericData(msgType, dataPtr);
            break;
    }
}
// picture callback - compressed picture ready
void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) {
    int restPictures =  mHardware->getPictureRestCount();
    if (!restPictures)
    {
        disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
    }

    sp<ICameraClient> c = mCameraClient;
    mLock.unlock();
    if (c != 0) {
        c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
    }
}

Copyright © Linux教程網 All Rights Reserved