CameraHal.cpp文件需要修改;一般CameraModule.cpp的调用函数实际都是CameraHal.cpp实现的;
CAMERA_HAL_RET CameraHal :: setCaptureDevice(sp capturedevice)
{
CAMERA_LOG_FUNC;
CAMERA_HAL_RET ret = CAMERA_HAL_ERR_NONE;
if (mCameraReady == false)
//capturedevice是从CameraModule.cpp传递过来的;
//CameraHal.cpp文件所有的操作都是针对mCaptureDevice;你要添加代码也对它操作;
mCaptureDevice = capturedevice;
else
ret = CAMERA_HAL_ERR_BAD_ALREADY_RUN;
return ret;
}
CAMERA_HAL_RET CameraHal::Init()
{
CAMERA_LOG_FUNC;
CAMERA_HAL_RET ret = CAMERA_HAL_ERR_NONE;
mCameraReady == true;
mCaptureDevice->GetDevType(&mSensorType);
if ((ret = AllocInterBuf())<0) //分配内存
return ret;
if ((ret = InitCameraHalParam()) < 0) //Camera参数初始化;如果需要这里可以改动;
return ret;
if ((ret = CameraMiscInit()) < 0) //不太清楚;我们不管;
return ret;
return ret;
}
如果我们需要接收apk传递下来的参数比如设置白平衡、帧率扫描的就修改 setParameters(const CameraParameters& params);模仿添加相应的功能就可以了;
status_t CameraHal:: setParameters(const CameraParameters& params)
{
CAMERA_LOG_FUNC;
int w, h;
int framerate;
int max_zoom,zoom, max_fps, min_fps;
char tmp[128];
Mutex::Autolock lock(mLock);
max_zoom = params.getInt(CameraParameters::KEY_MAX_ZOOM);
zoom = params.getInt(CameraParameters::KEY_ZOOM);
if(zoom > max_zoom){
CAMERA_LOG_ERR("Invalid zoom setting, zoom %d, max zoom %d",zoom,max_zoom);
return BAD_VALUE;
}
if (!((strcmp(params.getPreviewFormat(), "yuv420sp") == 0) ||
(strcmp(params.getPreviewFormat(), "yuv420p") == 0)// || (strcmp(params.getPreviewFormat(), "yuv422i-yuyv") == 0)
)) {
CAMERA_LOG_ERR("Only yuv420sp or yuv420pis supported, but input format is %s", params.getPreviewFormat());
return BAD_VALUE;
}
if (strcmp(params.getPictureFormat(), "jpeg") != 0) {
CAMERA_LOG_ERR("Only jpeg still pictures are supported");
return BAD_VALUE;
}
params.getPreviewSize(&w, &h);
sprintf(tmp, "%dx%d", w, h);
CAMERA_LOG_INFO("Set preview size: %s", tmp);
if (strstr(mSupportedPreviewSizes, tmp) == NULL){
CAMERA_LOG_ERR("The preview size w %d, h %d is not corrected", w, h);
return BAD_VALUE;
}
params.getPictureSize(&w, &h);
sprintf(tmp, "%dx%d", w, h);
CAMERA_LOG_INFO("Set picture size: %s", tmp);
if (strstr(mSupportedPictureSizes, tmp) == NULL){
CAMERA_LOG_ERR("The picture size w %d, h %d is not corrected", w, h);
return BAD_VALUE;
}
framerate = params.getPreviewFrameRate();
CAMERA_LOG_INFO("Set frame rate:%d FPS", framerate);
if ((framerate > 30) || (framerate < 0) ){
CAMERA_LOG_ERR("The framerate is not corrected");
return BAD_VALUE;
}
params.getPreviewFpsRange(&min_fps, &max_fps);
CAMERA_LOG_INFO("FPS range: %d - %d",min_fps, max_fps);
if (max_fps < 1000 || min_fps < 1000 || max_fps > 33000 || min_fps > 33000){
CAMERA_LOG_ERR("The fps range from %d to %d is error", min_fps, max_fps);
return BAD_VALUE;
}
const char *pFlashStr;
pFlashStr = params.get(CameraParameters::KEY_FLASH_MODE);
if (strcmp(pFlashStr, CameraParameters::FLASH_MODE_OFF) != 0 && strcmp(pFlashStr, CameraParameters::FLASH_MODE_AUTO) != 0
&& strcmp(pFlashStr, CameraParameters::FLASH_MODE_ON) != 0 && strcmp(pFlashStr, CameraParameters::FLASH_MODE_RED_EYE) != 0
&& strcmp(pFlashStr, CameraParameters::FLASH_MODE_TORCH) != 0) {
CAMERA_LOG_ERR("The flash mode is not corrected");
return BAD_VALUE;
}
const char *pFocusStr;
pFocusStr = params.get(CameraParameters::KEY_FOCUS_MODE);
if(strcmp(pFocusStr, CameraParameters::FOCUS_MODE_AUTO) != 0 && strcmp(pFocusStr, CameraParameters::FOCUS_MODE_INFINITY) != 0
&& strcmp(pFocusStr, CameraParameters::FOCUS_MODE_MACRO) != 0 && strcmp(pFocusStr, CameraParameters::FOCUS_MODE_FIXED) != 0
&& strcmp(pFocusStr, CameraParameters::FOCUS_MODE_EDOF) != 0 && strcmp(pFocusStr, CameraParameters::FOCUS_MODE_CONTINUOUS_VIDEO) != 0) {
CAMERA_LOG_ERR("The focus mode is not corrected");
return BAD_VALUE;
}
// auto white banlance
const char *pWhiteBalanceStr;
//mParameters.get()是获得上层的设置信息;不是从底层驱动获得的;
pWhiteBalanceStr = mParameters.get(CameraParameters::KEY_WHITE_BALANCE);
CAMERA_LOG_INFO("white balance: %s",pWhiteBalanceStr);
if (strcmp(pWhiteBalanceStr, CameraParameters::WHITE_BALANCE_AUTO) == 0){
//DevAWB()是自己添加的设置白平衡的功能;CaptureDeviceInterface.cpp添加虚函数;V4l2CapDeviceBase.cpp内实现;
if(mCaptureDevice->DevAWB(mCameraid) == 0)
{
CAMERA_LOG_INFO("%s@%d********auto white balance successed*********\n",__func__,__LINE__);
}else {
CAMERA_LOG_INFO("%s@%d********auto white balance failed*********\n",__func__,__LINE__);
return BAD_VALUE;
}
}else{
if(mCaptureDevice->DevAWB(mCameraid) == 0)
{
CAMERA_LOG_INFO("%s@%d********auto white balance successed*********\n",__func__,__LINE__);
}else {
CAMERA_LOG_INFO("%s@%d********auto white balance failed*********\n",__func__,__LINE__);
return BAD_VALUE;
}
}
mParameters = params;
return NO_ERROR;
}
下面是从CameraModul.cpp调用autoFocus流程;
CameraModul.cpp文件代码如下:
int camera_device_open(const hw_module_t* module, const char* name,
hw_device_t** device)
{
//自动对焦;
camera_ops->auto_focus = camera_auto_focus;
}
int camera_auto_focus(struct camera_device * device)
{
int rv = -EINVAL;
fsl_camera_device_t* fsl_dev = NULL;
LOGV("%s", __FUNCTION__);
if(!device)
return rv;
fsl_dev = (fsl_camera_device_t*) device;
//关键在这里;调用CameraHal.cpp层实现;
rv = gCameraHals[fsl_dev->cameraid]->autoFocus();
return rv;
}
status_t CameraHal::autoFocus()
{
CAMERA_LOG_FUNC;
Mutex::Autolock lock(mLock);
if (mAutoFocusThread != NULL)
mAutoFocusThread.clear();
//new 一自动对焦线程;
mAutoFocusThread = new AutoFocusThread(this);
if (mAutoFocusThread == NULL)
return UNKNOWN_ERROR;
return NO_ERROR;
}
//创建一个线程;这个线程运行的函数是:mHardware->autoFocusThread()
class AutoFocusThread : public Thread {
CameraHal* mHardware;
public:
AutoFocusThread(CameraHal* hw)
: Thread(false), mHardware(hw), mTID(0) { }
virtual void onFirstRef() {
run("AutoFocusThread", PRIORITY_URGENT_DISPLAY);
}
virtual bool threadLoop() {
mTID = gettid();
//具体实现在autoFocusThread();
if (mHardware->autoFocusThread()>=0) //返回值大于0对焦成功;
return true;
else
return false;
}
int mTID;
};
int CameraHal::autoFocusThread()
{
CAMERA_LOG_FUNC;
int FocusFlag = 0;
//DevAutoFocus()就是我们实现;
//CaptureDeviceInterface.h,V4l2CapDeviceBase.h添加定义;
//V4l2CapDeviceBase.cpp添加具体实现;
//mCameraid参数随便传;你驱动判断做相应动作;实际就是传递给ctrl.value的值;
if(mCaptureDevice->DevAutoFocus(mCameraid) == 0)
{
CAMERA_LOG_INFO("%s@%d********auto focus successed*********\n",__func__,__LINE__);
}else {
CAMERA_LOG_INFO("%s@%d********auto focus failed*********\n",__func__,__LINE__);
}
//系统原生的;回调函数告诉Camera服务自动对焦成功了;实际没自动对焦;
if (mMsgEnabled & CAMERA_MSG_FOCUS)
mNotifyCb(CAMERA_MSG_FOCUS, true, 0, mCallbackCookie);
return UNKNOWN_ERROR; //exit the thread
}
status_t CameraHal::cancelAutoFocus()
{
CAMERA_LOG_FUNC;
//空的;fsl原生没有实现;
return NO_ERROR;
}
status_t CameraHal::cancelAutoFocus()
{
CAMERA_LOG_FUNC;
//我驱动用3来判断释放自动对焦;
int focusModule = 3;
//取消自动对焦和自动对焦调用同一个函数DevAutoFocus;
if(mCaptureDevice->DevAutoFocus(focusModule) == 0)
{
CAMERA_LOG_INFO("%s@%d**************succeeded\n",__func__,__LINE__);
}else {
CAMERA_LOG_INFO("%s@%d*****************failed\n",__func__,__LINE__);
return UNKNOWN_ERROR;
}
return NO_ERROR;
}