Android开发之Camera(三)

本文深入探讨了Android中Camera开发的细节,重点讲解了CameraService的Client类,包括数据回调函数的处理,如何将数据从底层传递到Java应用层进行显示。同时介绍了CameraHardwareInterface的作用,它是应用层与底层驱动之间的桥梁,详细阐述了如何通过HAL层打开Camera驱动。
摘要由CSDN通过智能技术生成

通过前面一章我们知道了在jni层我们通过sp<Camera> camera = Camera::connect(cameraId);获取到了一个Camera示例,Camera包含两个变量:

      sp<ICameraService> mCameraService (实际类型为sp<BpCameraService>,最终实现类为CameraService)

      sp<ICamera> mCamera  (实际类型为sp<BpCamera>,最终实现类为Client)

于是我们通过类Camera::xx的函数就可以直接操作Client类的接口。

 

在CameraService::connect()函数中我们首先根据cameraId创建了一个CameraHardwareInterface的对象,然后将这个对象作为参数创建了一个Client对象,并返回sp<ICamera>给客户端。

 

下面我们开始分析CameraService::Client这个类:下面是Client类的构造函数和析构函数。

CameraService::Client::Client(const sp<CameraService>& cameraService,
        const sp<ICameraClient>& cameraClient,
        const sp<CameraHardwareInterface>& hardware,
        int cameraId, int cameraFacing, int clientPid) {
    int callingPid = getCallingPid();
    LOG1("Client::Client E (pid %d)", callingPid);

    mCameraService = cameraService;
    mCameraClient = cameraClient;
    mHardware = hardware;
    mCameraId = cameraId;
    mCameraFacing = cameraFacing;
    mClientPid = clientPid;
    mMsgEnabled = 0;
    mSurface = 0;
    mPreviewWindow = 0;
    mHardware->setCallbacks(notifyCallback,
                            dataCallback,
                            dataCallbackTimestamp,
                            (void *)cameraId);

    // Enable zoom, error, focus, and metadata messages by default
    enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
                  CAMERA_MSG_PREVIEW_METADATA);

    // Callback is disabled by default
    mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
    mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
    mPlayShutterSound = true;
    cameraService->setCameraBusy(cameraId);
    cameraService->loadSound();
    LOG1("Client::Client X (pid %d)", callingPid);
}

// tear down the client
CameraService::Client::~Client() {
    int callingPid = getCallingPid();
    LOG1("Client::~Client E (pid %d, this %p)", callingPid, this);

    // set mClientPid to let disconnet() tear down the hardware
    mClientPid = callingPid;
    disconnect();
    mCameraService->releaseSound();
    LOG1("Client::~Client X (pid %d, this %p)", callingPid, this);
}

 

在构造函数中:

      1)首先调用CameraHardwareInterface::setCallbacks(notifyCallback,  dataCallback, dataCallbackTimestamp, (void*)cameraId);

其中dataCallback函数主要用于从底层采集数据的缓冲区拷贝数据到显示的preview缓冲区。

void CameraService::Client::dataCallback(int32_t msgType,
        const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
    LOG2("dataCallback(%d)", msgType);

    sp<Client> client = getClientFromCookie(user);
    if (clien
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值