您的位置:首页 > 其它

Camera 施工中

2015-11-16 11:39 169 查看
Camera APP层的一般流程 :

# Step 1

camera = Camera.open();  //1.调用Camera的open()方法打开相机。

# Step 2

Camera.Parameters parameters = camera.getParameters();

parameters.setPreviewSize  //设置预览尺寸大小

 .setPreviewFrameRate //设置预览帧率

 .setPictureFormat //设置图片格式

 .set("jpeg-quality",85) //设置JPG图片质量

 .setPictureSize //设置照片尺寸

camera.setParameters(parameters);//传入参数

 

# Step 3

camera.setPreviewDisplay(surfaceHolder); //设置那个SurfaceView来显示该view

 

# Step 4

camera.startPreview();  //camera预览

 

# Step 5

camera.autoFocus(afcb); //自动聚焦功能

 

# Step 6

camera.takePicture(null, null , myjpegCallback); //拍照

public static Camera open() {
if (!isPermissionGranted()) {
return null;
}
int numberOfCameras = getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
return new Camera(i);
}
}
return null;
}


new Camera(i) --- >
mEventHandler = new EventHandler(this, looper);
native_setup(new WeakReference<Camera>(this), cameraId, halVersion, packageName);


而native_setup函数最终会通过JNI调用(android_hardware_Camera.cpp  android_hardware_Camera_native_setup)

camera = Camera::connect(cameraId, clientName,Camera::USE_CALLING_UID)方法请求CamerService获取一个本地camera对象。

这其中涉及到一些Binder通信等方面的知识。

status_t CameraService::connect(
const sp<ICameraClient>& cameraClient,
int cameraId,
const String16& clientPackageName,
int clientUid,
/*out*/
sp<ICamera>& device) {

String8 clientName8(clientPackageName);
int callingPid = getCallingPid();

LOG1("CameraService::connect E (pid %d \"%s\", id %d)", callingPid,
clientName8.string(), cameraId);

status_t status = validateConnect(cameraId, /*inout*/clientUid); //一些参数方面的检查
if (status != OK) {
return status;
}

sp<Client> client;
{
Mutex::Autolock lock(mServiceLock);
sp<BasicClient> clientTmp;
if (!canConnectUnsafe(cameraId, clientPackageName,//安全性方面的检查
cameraClient->asBinder(),
/*out*/clientTmp)) {
return -EBUSY;
} else if (client.get() != NULL) {
device = static_cast<Client*>(clientTmp.get());
return OK;
}

status = connectHelperLocked(/*out*/client, // clinet 为输出的camera对象,及connection函数中的device参数
cameraClient,
cameraId,
clientPackageName,
clientUid,
callingPid);
if (status != OK) {
return status;
}

}
// important: release the mutex here so the client can call back
//    into the service from its destructor (can be at the end of the call)

device = client;
return OK;
}
在connectHelperLocked函数中,主要的作用是创建一个CameraClient对象来管理本地的Camera

status_t CameraService::connectHelperLocked(
/*out*/
sp<Client>& client,
/*in*/
const sp<ICameraClient>& cameraClient,
int cameraId,
const String16& clientPackageName,
int clientUid,
int callingPid,
int halVersion,
bool legacyMode) {

int facing = -1;
int deviceVersion = getDeviceVersion(cameraId, &facing);//读取deviceVersion

if (halVersion < 0 || halVersion == deviceVersion) {
// Default path: HAL version is unspecified by caller, create CameraClient
// based on device version reported by the HAL.
switch(deviceVersion) {
case CAMERA_DEVICE_API_VERSION_1_0: //本机器走的是这个流程

client = new CameraClient(this, cameraClient,
clientPackageName, cameraId,
facing, callingPid, clientUid, getpid(), legacyMode);
break;
case CAMERA_DEVICE_API_VERSION_2_0:
case CAMERA_DEVICE_API_VERSION_2_1:
case CAMERA_DEVICE_API_VERSION_3_0:
case CAMERA_DEVICE_API_VERSION_3_1:
case CAMERA_DEVICE_API_VERSION_3_2:
client = new Camera2Client(this, cameraClient,
clientPackageName, cameraId,
facing, callingPid, clientUid, getpid(), legacyMode);
break;
case -1:
ALOGE("Invalid camera id %d", cameraId);
return BAD_VALUE;
default:
ALOGE("Unknown camera device HAL version: %d", deviceVersion);
return INVALID_OPERATION;
}
} else {
// A particular HAL version is requested by caller. Create CameraClient
// based on the requested HAL version.
if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
// Only support higher HAL version device opened as HAL1.0 device.
client = new CameraClient(this, cameraClient,
clientPackageName, cameraId,
facing, callingPid, clientUid, getpid(), legacyMode);
} else {
// Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
" opened as HAL %x device", halVersion, deviceVersion,
CAMERA_DEVICE_API_VERSION_1_0);
return INVALID_OPERATION;
}
}<pre name="code" class="objc">    status_t status = connectFinishUnsafe(client, client->getRemote());//初始化Camera module
if (status != OK) {
// this is probably not recoverable.. maybe the client can try again
return status;
}

mClient[cameraId] = client;
LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId,
getpid());

return OK;
</pre><pre name="code" class="objc">status_t CameraService::connectFinishUnsafe(const sp<BasicClient>& client,
const sp<IBinder>& remoteCallback) {
status_t status = client->initialize(mModule);//mModule为Camera在开机初始化的时候创建的 CameraService::onFirstRef
if (status != OK) {
ALOGE("%s: Could not initialize client from HAL module.", __FUNCTION__);
return status;
}
if (remoteCallback != NULL) {
remoteCallback->linkToDeath(this);
}

return OK;
}


再接着看client->initialize(mModule)函数


<pre name="code" class="objc">status_t CameraClient::initialize(camera_module_t *module) {
int callingPid = getCallingPid();
status_t res;

LOG1("CameraClient::initialize E (pid %d, id %d)", callingPid, mCameraId);

// Verify ops permissions
res = startCameraOps();
if (res != OK) {
return res;
}

char camera_device_name[10];
snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId);
//创建CameraHardwareInterface对象,该对象只起到一个中间衔接作用
mHardware = new CameraHardwareInterface(camera_device_name);
res = mHardware->initialize(&module->common);
if (res != OK) {
ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
__FUNCTION__, mCameraId, strerror(-res), res);
mHardware.clear();
return res;
}

// mtk callback
mHardware->setMtkCallbacks(
mtkMetadataCallback,
(void *)(uintptr_t)mCameraId
);
//设置数据回调函数
mHardware->setCallbacks(
notifyCallback,
dataCallback,
dataCallbackTimestamp,
(void *)(uintptr_t)mCameraId);

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

//!++
#if 1
// Enable MTK-extended messages by default
enableMsgType(MTK_CAMERA_MSG_EXT_NOTIFY | MTK_CAMERA_MSG_EXT_DATA);
#endif
//!--

LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId);
return OK;
}
</pre><pre name="code" class="objc">---> mHardware->initialize(&module->common);
status_t initialize(hw_module_t *module)
{
ALOGI("Opening camera %s", mName.string());
camera_module_t *cameraModule = reinterpret_cast<camera_module_t *>(module);
camera_info info;
status_t res = cameraModule->get_camera_info(atoi(mName.string()), &info);//获取关于camera设备的一些信息
if (res != OK) return res;

int rc = OK;
if (module->module_api_version >= CAMERA_MODULE_API_VERSION_2_3 &&
info.device_version > CAMERA_DEVICE_API_VERSION_1_0) {
// Open higher version camera device as HAL1.0 device.
rc = cameraModule->open_legacy(module, mName.string(),
CAMERA_DEVICE_API_VERSION_1_0,
(hw_device_t **)&mDevice);
} else {
rc = CameraService::filterOpenErrorCode(module->methods->open(
module, mName.string(), (hw_device_t **)&mDevice));//调用Camera module的open方法打开camera
}
if (rc != OK) {
ALOGE("Could not open camera %s: %d", mName.string(), rc);
return rc;
}
initHalPreviewWindow();//初始化预览的view
return rc;
}

module->methods->open(...)函数最终调用gCameraHAL.open(mod, name, dev);//CameraHAL.cpp 

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