您的位置:首页 > 移动开发 > Android开发

Android Camera 调用流程及调试

2013-08-29 09:14 686 查看
1、应用.java中   camera=Camera.open(0);java框架base/core/java/android/hardware/Camera.javapublicstaticCameraopen(intcameraId){ returnnewCamera(cameraId); }
Camera(intcameraId){
//定义回调函数
Looperlooper;//定义处理函数
native_setup(newWeakReference<Camera>(this),cameraId);
}
CameraJNI:base/core/jni/android_hardware_Camera.cpp
//connecttocameraservice
staticvoidandroid_hardware_Camera_native_setup(JNIEnv*env,jobjectthiz,
jobjectweak_this,jintcameraId)
{
sp<Camera>camera=Camera::connect(cameraId);

if(camera==NULL){
jniThrowRuntimeException(env,"Failtoconnecttocameraservice");
return;
}

//makesurecamerahardwareisalive
if(camera->getStatus()!=NO_ERROR){
jniThrowRuntimeException(env,"Camerainitializationfailed");
return;
}

jclassclazz=env->GetObjectClass(thiz);
if(clazz==NULL){
jniThrowRuntimeException(env,"Can'tfindandroid/hardware/Camera");
return;
}

//WeuseaweakreferencesotheCameraobjectcanbegarbagecollected.
//Thereferenceisonlyusedasaproxyforcallbacks.
sp<JNICameraContext>context=newJNICameraContext(env,weak_this,clazz,camera);
context->incStrong(thiz);
camera->setListener(context);

//savecontextinopaquefield
env->SetIntField(thiz,fields.context,(int)context.get());
}
Camera::connect:

av/camera/Camera.cpp
sp<Camera>Camera::connect(intcameraId)
{
ALOGV("connect");
sp<Camera>c=newCamera();
constsp<ICameraService>&cs=getCameraService();
if(cs!=0){
c->mCamera=cs->connect(c,cameraId);
}
if(c->mCamera!=0){
c->mCamera->asBinder()->linkToDeath(c);
c->mStatus=NO_ERROR;
}else{
c.clear();
}
returnc;
}
cs->connect(c,cameraId)---------》》av/services/camera/libcameraservice/CameraService.cpp
sp<ICamera>CameraService::connect(
constsp<ICameraClient>&cameraClient,intcameraId){
intcallingPid=getCallingPid();
sp<CameraHardwareInterface>hardware=NULL;

...

    structcamera_infoinfo;
    if(mModule->get_camera_info(cameraId,&info)!=OK){//重要
    ALOGE("Invalidcameraid%d",cameraId);
    returnNULL;
    }
hardware=newCameraHardwareInterface(camera_device_name);//没什么,
if(hardware->initialize(&mModule->common)!=OK){//重要
hardware.clear();
returnNULL;
}
mModule->get_camera_info(cameraId,&info)----------》》
intcamera_get_camera_info(intcamera_id,structcamera_info*info)
{
//thisgoingtobethefirstcallfromcameraservice
//initializecamerapropertieshere...
if(gCameraProperties.initialize()!=android::NO_ERROR)
{}
//Getcamerapropertiesforcameraindex
if(gCameraProperties.getProperties(camera_id,&properties)<0)
{
}
gCameraProperties.initialize()--------》》CameraProperties.cpp
//InitializestheCameraPropertiesclass
status_tCameraProperties::initialize()
{
#ifdefAMLOGIC_USB_CAMERA_SUPPORT
mCamerasSupported=0;
ret=loadProperties();
mInitialized=0;
#else
ret=loadProperties();
mInitialized=1;
#endif
}
///LoadsalltheCamerarelatedproperties
status_tCameraProperties::loadProperties()
{
//adapterupdatescapabilitiesandweupdatecameracount
mCamerasSupported=CameraAdapter_Capabilities(mCameraProps,mCamerasSupported,CameraAdapter_CameraNum());
for(unsignedinti=0;i<mCamerasSupported;i++){
mCameraProps[i].set(CAMERA_SENSOR_INDEX,i);
mCameraProps[i].dump();
}
}
mCameraProps[i].dump();将所有的属性打印出来
extern"C"voidloadCaps(intcamera_id,CameraProperties::Properties*params){
constcharDEFAULT_BRIGHTNESS[]="50";
constcharDEFAULT_CONTRAST[]="100";
constcharDEFAULT_IPP[]="ldc-nsf";
constcharDEFAULT_GBCE[]="disable";
constcharDEFAULT_ISO_MODE[]="auto";
constcharDEFAULT_PICTURE_FORMAT[]="jpeg";
constcharDEFAULT_PICTURE_SIZE[]="640x480";
constcharPREVIEW_FORMAT_420SP[]="yuv420sp";
constcharPREVIEW_FORMAT_422I[]="yuv422i-yuyv";
constcharDEFAULT_PREVIEW_SIZE[]="640x480";
constcharDEFAULT_NUM_PREV_BUFS[]="6";
constcharDEFAULT_NUM_PIC_BUFS[]="1";
constcharDEFAULT_MAX_FOCUS_AREAS[]="1";
constcharDEFAULT_SATURATION[]="100";
constcharDEFAULT_SCENE_MODE[]="auto";
constcharDEFAULT_SHARPNESS[]="100";
constcharDEFAULT_VSTAB[]="false";
constcharDEFAULT_VSTAB_SUPPORTED[]="true";
constcharDEFAULT_MAX_FD_HW_FACES[]="0";
constcharDEFAULT_MAX_FD_SW_FACES[]="0";
constcharDEFAULT_FOCAL_LENGTH_PRIMARY[]="4.31";
constcharDEFAULT_FOCAL_LENGTH_SECONDARY[]="1.95";
constcharDEFAULT_HOR_ANGLE[]="54.8";
constcharDEFAULT_VER_ANGLE[]="42.5";
constcharDEFAULT_AE_LOCK[]="false";
constcharDEFAULT_AWB_LOCK[]="false";
constcharDEFAULT_MAX_NUM_METERING_AREAS[]="0";
constcharDEFAULT_LOCK_SUPPORTED[]="true";
constcharDEFAULT_LOCK_UNSUPPORTED[]="false";
constcharDEFAULT_VIDEO_SIZE[]="640x480";
constcharDEFAULT_PREFERRED_PREVIEW_SIZE_FOR_VIDEO[]="640x480";

boolbFrontCam=false;
intcamera_fd=-1;

if(camera_id==0){
#ifdefAMLOGIC_BACK_CAMERA_SUPPORT
bFrontCam=false;
#elifdefined(AMLOGIC_FRONT_CAMERA_SUPPORT)
bFrontCam=true;
#elifdefined(AMLOGIC_USB_CAMERA_SUPPORT)
bFrontCam=true;
#else//definednothing,wetrybyourself.weassume,the0isfrontcamera,1isbackcamera
bFrontCam=true;
#endif
}elseif(camera_id==1){
#ifdefined(AMLOGIC_BACK_CAMERA_SUPPORT)&&defined(AMLOGIC_FRONT_CAMERA_SUPPORT)
bFrontCam=true;
#else//definednothing,wetrytobyourself
bFrontCam=false;
#endif
}

//shouldchangedwhilethescreenorientationchanged.
intdegree=-1;
charproperty[64];
memset(property,0,sizeof(property));
if(bFrontCam==true){
params->set(CameraProperties::FACING_INDEX,ExCameraParameters::FACING_FRONT);
if(getCameraOrientation(bFrontCam,property)>=0){
params->set(CameraProperties::ORIENTATION_INDEX,property);
}else{
#ifdefAMLOGIC_USB_CAMERA_SUPPORT
params->set(CameraProperties::ORIENTATION_INDEX,"0");
#else
params->set(CameraProperties::ORIENTATION_INDEX,"270");
#endif
}
}else{
params->set(CameraProperties::FACING_INDEX,ExCameraParameters::FACING_BACK);
if(getCameraOrientation(bFrontCam,property)>=0){
params->set(CameraProperties::ORIENTATION_INDEX,property);
}else{
#ifdefAMLOGIC_USB_CAMERA_SUPPORT
params->set(CameraProperties::ORIENTATION_INDEX,"180");
#else
params->set(CameraProperties::ORIENTATION_INDEX,"90");
#endif
}
}

params->set(CameraProperties::SUPPORTED_PREVIEW_FORMATS,"yuv420sp,yuv420p,mjpeg");//yuv420pforcts//leonaddmjpeg
if(DEFAULT_PREVIEW_PIXEL_FORMAT==V4L2_PIX_FMT_YUYV){//422I
//params->set(CameraProperties::SUPPORTED_PREVIEW_FORMATS,PREVIEW_FORMAT_422I);
params->set(CameraProperties::PREVIEW_FORMAT,PREVIEW_FORMAT_422I);
}elseif(DEFAULT_PREVIEW_PIXEL_FORMAT==V4L2_PIX_FMT_NV21){//420sp
//params->set(CameraProperties::SUPPORTED_PREVIEW_FORMATS,PREVIEW_FORMAT_420SP);
params->set(CameraProperties::PREVIEW_FORMAT,PREVIEW_FORMAT_420SP);
}else{//defaultcase
//params->set(CameraProperties::SUPPORTED_PREVIEW_FORMATS,PREVIEW_FORMAT_420SP);
params->set(CameraProperties::PREVIEW_FORMAT,PREVIEW_FORMAT_420SP);
}

//getpreviewsize&set
char*sizes=(char*)calloc(1,1024);
if(!sizes){
CAMHAL_LOGEA("Allocstringbufferror!");
return;
}

#ifdefAMLOGIC_USB_CAMERA_SUPPORT
#ifdefAMLOGIC_TWO_CH_UVC
intmain_id=-1;
if(NO_ERROR==getVideodevId(camera_id,main_id)){
if((camera_fd=open(DEVICE_PATH(camera_id),O_RDWR))!=-1)
{
CAMHAL_LOGDB("open%ssuccesstoloadCaps\n",DEVICE_PATH(camera_id));
}
}
#else
while(camera_id<(int)ARRAY_SIZE(SENSOR_PATH)){
if((camera_fd=open(DEVICE_PATH(camera_id),O_RDWR))!=-1)
{
CAMHAL_LOGDB("open%ssuccesswhenloadCaps!\n",DEVICE_PATH(camera_id));
break;
}
camera_id++;
}
if(camera_id>=(int)ARRAY_SIZE(SENSOR_PATH)){
CAMHAL_LOGEB("failedtoopeningCamerawhenloadCaps:%s",strerror(errno));
}
#endif
#else
camera_fd=open(DEVICE_PATH(camera_id),O_RDWR);
#endif
if(camera_fd<0)
CAMHAL_LOGEB("opencamera%derrorwhenloadcaps",camera_id);

#ifdefAMLOGIC_CAMERA_NONBLOCK_SUPPORT
intfps=0,fps_num=0;
intret;
char*fpsrange=(char*)calloc(32,sizeof(char));

ret=enumFramerate(camera_fd,&fps,&fps_num);
if((fpsrange!=NULL)&&(NO_ERROR==ret)&&(0!=fps_num)){
sprintf(fpsrange,"%s%d","10,",fps/fps_num);
CAMHAL_LOGDA("O_NONBLOCKoperationtodopreviewThread\n");

params->set(CameraProperties::SUPPORTED_PREVIEW_FRAME_RATES,fpsrange);
params->set(CameraProperties::PREVIEW_FRAME_RATE,fps/fps_num);

memset(fpsrange,0,32*sizeof(char));
sprintf(fpsrange,"%s%d","10000,",fps*1000/fps_num);
params->set(CameraProperties::FRAMERATE_RANGE_IMAGE,fpsrange);
params->set(CameraProperties::FRAMERATE_RANGE_VIDEO,fpsrange);

memset(fpsrange,0,32*sizeof(char));
sprintf(fpsrange,"(%s%d)","5000,",fps*1000/fps_num);
params->set(CameraProperties::FRAMERATE_RANGE_SUPPORTED,fpsrange);
memset(fpsrange,0,32*sizeof(char));
sprintf(fpsrange,"%s%d","5000,",fps*1000/fps_num);
params->set(CameraProperties::FRAMERATE_RANGE,fpsrange);
}else{
if(NO_ERROR!=ret)
CAMHAL_LOGDA("sensordriverneedtoimplementVIDIOC_G_PARM!!!\n");
params->set(CameraProperties::SUPPORTED_PREVIEW_FRAME_RATES,"10,15");
params->set(CameraProperties::PREVIEW_FRAME_RATE,"15");

params->set(CameraProperties::FRAMERATE_RANGE_SUPPORTED,"(5000,26623)");
params->set(CameraProperties::FRAMERATE_RANGE,"5000,26623");
params->set(CameraProperties::FRAMERATE_RANGE_IMAGE,"10000,15000");
params->set(CameraProperties::FRAMERATE_RANGE_VIDEO,"10000,15000");
}
#else
params->set(CameraProperties::SUPPORTED_PREVIEW_FRAME_RATES,"10,15");
params->set(CameraProperties::PREVIEW_FRAME_RATE,"15");

params->set(CameraProperties::FRAMERATE_RANGE_SUPPORTED,"(5000,26623)");
params->set(CameraProperties::FRAMERATE_RANGE,"5000,26623");
params->set(CameraProperties::FRAMERATE_RANGE_IMAGE,"10000,15000");
params->set(CameraProperties::FRAMERATE_RANGE_VIDEO,"10000,15000");
#endif

memset(sizes,0,1024);
uint32_tpreview_format=DEFAULT_PREVIEW_PIXEL_FORMAT;
#ifdefAMLOGIC_USB_CAMERA_SUPPORT
preview_format=V4L2_PIX_FMT_YUYV;
#endif
if(!getValidFrameSize(camera_fd,preview_format,sizes)){
intlen=strlen(sizes);
unsignedintsupported_w=0,supported_h=0,w=0,h=0;
if(len>1){
if(sizes[len-1]==',')
sizes[len-1]='\0';
}

#ifndefAMLOGIC_USB_CAMERA_SUPPORT
charsmall_size[8]="176x144";//forcts
if(strstr(sizes,small_size)==NULL){
if((len+sizeof(small_size))<(1024-1)){
strcat(sizes,",");
strcat(sizes,small_size);
}
}
#endif
params->set(CameraProperties::SUPPORTED_PREVIEW_SIZES,sizes);

char*b=(char*)sizes;
while(b!=NULL){
if(sscanf(b,"%dx%d",&supported_w,&supported_h)!=2){
break;
}
if((supported_w*supported_h)>(w*h)){
w=supported_w;
h=supported_h;
}
b=strchr(b,',');
if(b)
b++;
}
if((w>0)&&(h>0)){
memset(sizes,0,1024);
sprintf(sizes,"%dx%d",w,h);
}
//char*b=strrchr(sizes,',');
//if(b)
//b++;
//else
//b=sizes;
params->set(CameraProperties::PREVIEW_SIZE,sizes);
}
else
{
#ifdefAMLOGIC_USB_CAMERA_SUPPORT
params->set(CameraProperties::SUPPORTED_PREVIEW_SIZES,"320x240,176x144,160x120");
params->set(CameraProperties::PREVIEW_SIZE,"320x240");
#else
params->set(CameraProperties::SUPPORTED_PREVIEW_SIZES,"640x480,352x288,176x144");
params->set(CameraProperties::PREVIEW_SIZE,"640x480");
#endif
}

params->set(CameraProperties::SUPPORTED_PICTURE_FORMATS,DEFAULT_PICTURE_FORMAT);
params->set(CameraProperties::PICTURE_FORMAT,DEFAULT_PICTURE_FORMAT);
params->set(CameraProperties::JPEG_QUALITY,90);

//musthave>2sizesandcontain"0x0"
params->set(CameraProperties::SUPPORTED_THUMBNAIL_SIZES,"180x160,0x0");
params->set(CameraProperties::JPEG_THUMBNAIL_SIZE,"180x160");
params->set(CameraProperties::JPEG_THUMBNAIL_QUALITY,90);

//get&setpicturesize
memset(sizes,0,1024);
uint32_tpicture_format=DEFAULT_IMAGE_CAPTURE_PIXEL_FORMAT;
#ifdefAMLOGIC_USB_CAMERA_SUPPORT
picture_format=V4L2_PIX_FMT_YUYV;
#endif
if(!getValidFrameSize(camera_fd,picture_format,sizes)){
intlen=strlen(sizes);
unsignedintsupported_w=0,supported_h=0,w=0,h=0;
if(len>1){
if(sizes[len-1]==',')
sizes[len-1]='\0';
}

params->set(CameraProperties::SUPPORTED_PICTURE_SIZES,sizes);

char*b=(char*)sizes;
while(b!=NULL){
if(sscanf(b,"%dx%d",&supported_w,&supported_h)!=2){
break;
}
if((supported_w*supported_h)>(w*h)){
w=supported_w;
h=supported_h;
}
b=strchr(b,',');
if(b)
b++;
}
if((w>0)&&(h>0)){
memset(sizes,0,1024);
sprintf(sizes,"%dx%d",w,h);
}
//char*b=strrchr(sizes,',');
//if(b)
//b++;
//else
//b=sizes;
params->set(CameraProperties::PICTURE_SIZE,sizes);
}
else
{
#ifdefAMLOGIC_USB_CAMERA_SUPPORT
params->set(CameraProperties::SUPPORTED_PICTURE_SIZES,"320x240");
params->set(CameraProperties::PICTURE_SIZE,"320x240");
#else
params->set(CameraProperties::SUPPORTED_PICTURE_SIZES,"640x480");
params->set(CameraProperties::PICTURE_SIZE,"640x480");
#endif
}
free(sizes);

char*focus_mode=(char*)calloc(1,256);
char*def_focus_mode=(char*)calloc(1,64);
if((focus_mode)&&(def_focus_mode)){
memset(focus_mode,0,256);
memset(def_focus_mode,0,64);
if(getCameraAutoFocus(camera_fd,focus_mode,def_focus_mode)){
params->set(CameraProperties::SUPPORTED_FOCUS_MODES,focus_mode);
params->set(CameraProperties::FOCUS_MODE,def_focus_mode);
}else{
params->set(CameraProperties::SUPPORTED_FOCUS_MODES,"fixed");
params->set(CameraProperties::FOCUS_MODE,"fixed");
}
}else{
params->set(CameraProperties::SUPPORTED_FOCUS_MODES,"fixed");
params->set(CameraProperties::FOCUS_MODE,"fixed");
}
if(focus_mode){
free(focus_mode);
focus_mode=NULL;
}
if(def_focus_mode){
free(def_focus_mode);
def_focus_mode=NULL;
}

char*banding_mode=(char*)calloc(1,256);
char*def_banding_mode=(char*)calloc(1,64);
if((banding_mode)&&(def_banding_mode)){
memset(banding_mode,0,256);
memset(def_banding_mode,0,64);

getCameraBanding(camera_fd,banding_mode,def_banding_mode);
params->set(CameraProperties::SUPPORTED_ANTIBANDING,banding_mode);
params->set(CameraProperties::ANTIBANDING,def_banding_mode);
}else{
params->set(CameraProperties::SUPPORTED_ANTIBANDING,"50hz,60hz");
params->set(CameraProperties::ANTIBANDING,"50hz");
}
if(banding_mode){
free(banding_mode);
banding_mode=NULL;
}
if(def_banding_mode){
free(def_banding_mode);
def_banding_mode=NULL;
}

params->set(CameraProperties::FOCAL_LENGTH,"4.31");

params->set(CameraProperties::HOR_ANGLE,"54.8");
params->set(CameraProperties::VER_ANGLE,"42.5");

#ifdefAMLOGIC_USB_CAMERA_SUPPORT
params->set(CameraProperties::SUPPORTED_WHITE_BALANCE,"auto");
params->set(CameraProperties::WHITEBALANCE,"auto");
#else
char*wb_mode=(char*)calloc(1,256);
char*def_wb_mode=(char*)calloc(1,64);

if(wb_mode&&def_wb_mode){
memset(wb_mode,0,256);
memset(def_wb_mode,0,64);
getCameraWhiteBalance(camera_fd,wb_mode,def_wb_mode);
params->set(CameraProperties::SUPPORTED_WHITE_BALANCE,wb_mode);
params->set(CameraProperties::WHITEBALANCE,def_wb_mode);
}else{
params->set(CameraProperties::SUPPORTED_WHITE_BALANCE,"auto,daylight,incandescent,fluorescent");
params->set(CameraProperties::WHITEBALANCE,"auto");
}

if(wb_mode){
free(wb_mode);
wb_mode=NULL;
}
if(def_wb_mode){
free(def_wb_mode);
def_wb_mode=NULL;
}
#endif

params->set(CameraProperties::AUTO_WHITEBALANCE_LOCK,DEFAULT_AWB_LOCK);

params->set(CameraProperties::SUPPORTED_EFFECTS,"none,negative,sepia");
params->set(CameraProperties::EFFECT,"none");

char*flash_mode=(char*)calloc(1,256);
char*def_flash_mode=(char*)calloc(1,64);
if((flash_mode)&&(def_flash_mode)){
memset(flash_mode,0,256);
memset(def_flash_mode,0,64);
if(get_flash_mode(camera_fd,flash_mode,def_flash_mode)){
params->set(CameraProperties::SUPPORTED_FLASH_MODES,flash_mode);
params->set(CameraProperties::FLASH_MODE,def_flash_mode);
CAMHAL_LOGDB("def_flash_mode=%s,flash_mode=%s\n",
def_flash_mode,flash_mode);
}
}
if(flash_mode){
free(flash_mode);
flash_mode=NULL;
}
if(def_flash_mode){
free(def_flash_mode);
def_flash_mode=NULL;
}

//params->set(CameraParameters::KEY_SUPPORTED_SCENE_MODES,"auto,night,snow");
//params->set(CameraParameters::KEY_SCENE_MODE,"auto");

params->set(CameraProperties::EXPOSURE_MODE,"auto");
params->set(CameraProperties::SUPPORTED_EXPOSURE_MODES,"auto");
params->set(CameraProperties::AUTO_EXPOSURE_LOCK,DEFAULT_AE_LOCK);

intmin=0,max=0,def=0,step=0;
getCameraExposureValue(camera_fd,min,max,step,def);
params->set(CameraProperties::SUPPORTED_EV_MAX,max);
params->set(CameraProperties::SUPPORTED_EV_MIN,min);
params->set(CameraProperties::EV_COMPENSATION,def);
params->set(CameraProperties::SUPPORTED_EV_STEP,step);

//don'tsupportdigitalzoomnow
#ifndefAMLOGIC_USB_CAMERA_SUPPORT
intzoom_level=-1;
char*zoom_str=(char*)calloc(1,256);
if(zoom_str)
memset(zoom_str,0,256);
zoom_level=get_supported_zoom(camera_fd,zoom_str);
if(zoom_level>0){//newinterfacebyv4lioctl
params->set(CameraProperties::ZOOM_SUPPORTED,"true");
params->set(CameraProperties::SMOOTH_ZOOM_SUPPORTED,"false");
params->set(CameraProperties::SUPPORTED_ZOOM_RATIOS,zoom_str);
params->set(CameraProperties::SUPPORTED_ZOOM_STAGES,zoom_level);//thinkthezoomratiosasaarray,themaxzoomisthemaxindex
params->set(CameraProperties::ZOOM,0);//defaultshouldbe0
}else{//bysetvideolayerzoomsys
params->set(CameraProperties::ZOOM_SUPPORTED,"true");
params->set(CameraProperties::SMOOTH_ZOOM_SUPPORTED,"false");
params->set(CameraProperties::SUPPORTED_ZOOM_RATIOS,"100,120,140,160,180,200,220,280,300");
params->set(CameraProperties::SUPPORTED_ZOOM_STAGES,8);//thinkthezoomratiosasaarray,themaxzoomisthemaxindex
params->set(CameraProperties::ZOOM,0);//defaultshouldbe0
}
if(zoom_str)
free(zoom_str);
#else
params->set(CameraProperties::ZOOM_SUPPORTED,"false");
params->set(CameraProperties::SMOOTH_ZOOM_SUPPORTED,"false");
params->set(CameraProperties::SUPPORTED_ZOOM_RATIOS,"100");
params->set(CameraProperties::SUPPORTED_ZOOM_STAGES,0);//thinkthezoomratiosasaarray,themaxzoomisthemaxindex
params->set(CameraProperties::ZOOM,0);//defaultshouldbe0
#endif

params->set(CameraProperties::SUPPORTED_ISO_VALUES,"auto");
params->set(CameraProperties::ISO_MODE,DEFAULT_ISO_MODE);

params->set(CameraProperties::SUPPORTED_IPP_MODES,DEFAULT_IPP);
params->set(CameraProperties::IPP,DEFAULT_IPP);

params->set(CameraProperties::SUPPORTED_SCENE_MODES,"auto");
params->set(CameraProperties::SCENE_MODE,DEFAULT_SCENE_MODE);

params->set(CameraProperties::BRIGHTNESS,DEFAULT_BRIGHTNESS);
params->set(CameraProperties::CONTRAST,DEFAULT_CONTRAST);
params->set(CameraProperties::GBCE,DEFAULT_GBCE);
params->set(CameraProperties::SATURATION,DEFAULT_SATURATION);
params->set(CameraProperties::SHARPNESS,DEFAULT_SHARPNESS);
params->set(CameraProperties::VSTAB,DEFAULT_VSTAB);
params->set(CameraProperties::VSTAB_SUPPORTED,DEFAULT_VSTAB_SUPPORTED);
params->set(CameraProperties::MAX_FD_HW_FACES,DEFAULT_MAX_FD_HW_FACES);
params->set(CameraProperties::MAX_FD_SW_FACES,DEFAULT_MAX_FD_SW_FACES);
params->set(CameraProperties::REQUIRED_PREVIEW_BUFS,DEFAULT_NUM_PREV_BUFS);
params->set(CameraProperties::REQUIRED_IMAGE_BUFS,DEFAULT_NUM_PIC_BUFS);
#ifdefAMLOGIC_ENABLE_VIDEO_SNAPSHOT
params->set(CameraProperties::VIDEO_SNAPSHOT_SUPPORTED,"true");
#else
params->set(CameraProperties::VIDEO_SNAPSHOT_SUPPORTED,"false");
#endif
#ifdefAMLOGIC_USB_CAMERA_SUPPORT
params->set(CameraProperties::VIDEO_SIZE,params->get(CameraProperties::PREVIEW_SIZE));
params->set(CameraProperties::PREFERRED_PREVIEW_SIZE_FOR_VIDEO,params->get(CameraProperties::PREVIEW_SIZE));
#else
params->set(CameraProperties::VIDEO_SIZE,DEFAULT_VIDEO_SIZE);
params->set(CameraProperties::PREFERRED_PREVIEW_SIZE_FOR_VIDEO,DEFAULT_PREFERRED_PREVIEW_SIZE_FOR_VIDEO);
#endif

if(camera_fd>=0)
close(camera_fd);
}
ViewCode
intcamera_get_camera_info(intcamera_id,structcamera_info*info)中的另一个函数gCameraProperties.getProperties(camera_id,&properties)此函数是获取到对应camera_id的属性,因为在上一函数中已经遍历了所有的Camera设备,所以此函数实现较为简单:
//ReturnsthepropertiesclassforaspecificCamera
//EachvalueisindexedbytheCameraProperties::CameraPropertyIndexenum
intCameraProperties::getProperties(intcameraIndex,CameraProperties::Properties**properties)
{*properties=mCameraProps+cameraIndex;
return0;
}
camera_get_camera_info分析分析完了,其实现的主要功能:获取所有Camera的属性参数并保存在参数列表的数组中,并使用当前的Camera_id获取当前Camera的属性。

回到sp<ICamera>CameraService::connect(中的第二个调用的函数
hardware=newCameraHardwareInterface(camera_device_name);
if(hardware->initialize(&mModule->common)!=OK){
hardware.clear();
returnNULL;
}
hardware->initialize(&mModule->common)
status_tinitialize(hw_module_t*module)
{
ALOGI("Openingcamera%s",mName.string());
intrc=module->methods->open(module,mName.string(),
(hw_device_t**)&mDevice);
if(rc!=OK){
ALOGE("Couldnotopencamera%s:%d",mName.string(),rc);
returnrc;
}
initHalPreviewWindow();
returnrc;
}

intcamera_device_open(consthw_module_t*module,constchar*name,hw_device_t**device)
/*******************************************************************
*implementationofcamera_modulefunctions
*******************************************************************/

/*opendevicehandletooneofthecameras
*
*assumecameraservicewillkeepsingletonofeachcamera
*sothisfunctionwillalwaysonlybecalledoncepercamerainstance
*/

intcamera_device_open(consthw_module_t*module,constchar*name,hw_device_t**device)
{
aml_camera_device_t*camera_device=NULL;
camera_device_ops_t*camera_ops=NULL;
android::CameraHal*camera=NULL;
android::CameraProperties::Properties*properties=NULL;

android::Mutex::Autolocklock(gCameraHalDeviceLock);

LOGI("camera_deviceopen");
/*
...状态检查,camera_ops初始化及函数赋值
*/
//--------vendorspecificstuff--------
if(gCameraProperties.getProperties(cameraid,&properties)<0)//再次通过cameraid得到属性
{
LOGE("Couldn'tgetcameraproperties");
}

camera=newandroid::CameraHal(cameraid);//没什么,主要是变量初始化及video系统的变更设置,关闭video播放
if(properties&&(camera->initialize(properties)!=android::NO_ERROR))
{
LOGE("Couldn'tinitializecamerainstance");
}

gCameraHals[cameraid]=camera;
gCamerasOpen++;
}
....
returnrv;
}

properties&&(camera->initialize(properties)
/**
@briefInitializetheCameraHAL

CreatesCameraAdapter,AppCallbackNotifier,DisplayAdapterandMemoryManager

@paramNone
@returnNO_ERROR-Onsuccess
NO_MEMORY-Onfailuretoallocatememoryforanyoftheobjects
@remarksCameraHalinternalfunction

*/

status_tCameraHal::initialize(CameraProperties::Properties*properties)
{
//Getmycameraproperties
mCameraProperties=properties;
//DumpthepropertiesofthisCamera
//willonlyprintifDEBUGmacroisdefined
mCameraProperties->dump();//再次打印所有属性

if(strcmp(CameraProperties::DEFAULT_VALUE,mCameraProperties->get(CameraProperties::CAMERA_SENSOR_INDEX))!=0)
{
sensor_index=atoi(mCameraProperties->get(CameraProperties::CAMERA_SENSOR_INDEX));
}

CAMHAL_LOGDB("Sensorindex%d",sensor_index);//得到cameraindex

mCameraAdapter=CameraAdapter_Factory(sensor_index);//创建CameraAdapter,重要,但简单
if((NULL==mCameraAdapter)||(mCameraAdapter->initialize(properties)!=NO_ERROR))
{
CAMHAL_LOGEA("UnabletocreateorinitializeCameraAdapter");
mCameraAdapter=NULL;
gotofail_loop;
}
mCameraAdapter->incStrong(mCameraAdapter);
mCameraAdapter->registerImageReleaseCallback(releaseImageBuffers,(void*)this);//注册回调函数,释放buffer
mCameraAdapter->registerEndCaptureCallback(endImageCapture,(void*)this);

if(!mAppCallbackNotifier.get())
{
///Createthecallbacknotifier
mAppCallbackNotifier=newAppCallbackNotifier();//创建AppCallbackNotifier
if((NULL==mAppCallbackNotifier.get())||(mAppCallbackNotifier->initialize()!=NO_ERROR))
{
CAMHAL_LOGEA("UnabletocreateorinitializeAppCallbackNotifier");
gotofail_loop;
}
}

if(!mMemoryManager.get())
{
///CreateMemoryManager
mMemoryManager=newMemoryManager();
if((NULL==mMemoryManager.get())||(mMemoryManager->initialize()!=NO_ERROR))
{
CAMHAL_LOGEA("UnabletocreateorinitializeMemoryManager");
gotofail_loop;
}
}

///Setuptheclassdependencies...

///AppCallbackNotifierhastoknowwheretogettheCameraframesandtheeventslikeautofocuslocketcfrom.
///CameraAdapteristheonewhichprovidesthoseevents
///SetitastheframeandeventprovidersforAppCallbackNotifier
///@remarkssetEventProviderAPItakesinabitmaskofeventsforregisteringaproviderforthedifferentevents
///Thatway,ifeventscancomefromDisplayAdapterinfuture,wewillbeabletoadditasprovider
///foranyevent
mAppCallbackNotifier->setEventProvider(eventMask,mCameraAdapter);//重要,看注释
mAppCallbackNotifier->setFrameProvider(mCameraAdapter);

///Anydynamicerrorsthathappenduringthecamerausecasehastobepropagatedbacktotheapplication
///viaCAMERA_MSG_ERROR.AppCallbackNotifieristheclassthatnotifiessucherrorstotheapplication
///SetitastheerrorhandlerforCameraAdapter
mCameraAdapter->setErrorHandler(mAppCallbackNotifier.get());//重要,看注释

///Startthecallbacknotifier
if(mAppCallbackNotifier->start()!=NO_ERROR)
{
CAMHAL_LOGEA("Couldn'tstartAppCallbackNotifier");
gotofail_loop;
}
CAMHAL_LOGDA("StartedAppCallbackNotifier..");
mAppCallbackNotifier->setMeasurements(mMeasurementEnabled);

///Initializedefaultparameters//leon
initDefaultParameters();//重要

if(setParameters(mParameters)!=NO_ERROR)//非常重要
{
CAMHAL_LOGEA("Failedtosetdefaultparameters?!");
}returnNO_ERROR;

fail_loop:
returnNO_MEMORY;

}
注视中已说明创建ameraAdapter,AppCallbackNotifier,DisplayAdapterandMemoryManager等,除了上述的创建,还有两个重要函数,初始化默认的参数,及设置初始化的参数

initDefaultParameters();分析参数是否可用,并将设置默认参数mParameters。

setParameters(mParameters)
/**
@briefSetthecameraparameters.

@param[in]paramsCameraparameterstoconfigurethecamera
@returnNO_ERROR
@todoDefineerrorcodes

*/
intCameraHal::setParameters(constCameraParameters¶ms)
{

...//依然是设置各种参数,与Prop中的允许的参数对比,
//Onlysendparameterstoadapterifpreviewisalready
//enabledordoesSetParameterNeedUpdatesaysso.InitialsetParameterstocameraadapter,
//willbecalledinstartPreview()
//TODO(XXX):Needtoidentifyotherparametersthatneedupdatefromcameraadapter
...//只有在startPreviewenabled或者doesSetParameterNeedUpdate为true时才将parameters发送到adapter中。在startPreview时设置。

}
最后sp<ICamera>CameraService::connect创建Client并将之返回。
av/camera/Camera.cpp中sp<Camera>Camera::connect(intcameraId)会将newCamera返回给JNI;
JNI返回给base/core/java/android/hardware/Camera.java的NewCamera;
这样,Camera就被创建成功

V4LCameraAdapter::setParameters函数是对一些zoom曝光之类的参数进行设置。而真正的关于分辨率fmt之类的参数是在startPreview时设置的,调用函数UseBuffersPreview进行设置。

hardware->initialize(&mModule->common)-------------------》》av/services/camera/libcameraservice/CameraHardwareInterface.h
status_tinitialize(hw_module_t*module)
{
ALOGI("Openingcamera%s",mName.string());
intrc=module->methods->open(module,mName.string(),
(hw_device_t**)&mDevice);
if(rc!=OK){
ALOGE("Couldnotopencamera%s:%d",mName.string(),rc);
returnrc;
}
initHalPreviewWindow();
returnrc;
}
不去追究细节,module->methods->open-----------》》CameraHal_Module.cpp
staticstructhw_module_methods_tcamera_module_methods={
open:camera_device_open
};

;//重要,看注释
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: