前言:做了7年的camera app开发,给自己一个总结,算是对camera的一次告白吧。Camera被大家誉为手机的眼睛,是现在各大手机厂商的卖点,也是各大厂商重点发力的地方。Camera的重要性我就不在这里赘述了,让我们进入正题。
如果只要拍照功能,不需要录像的功能,只申请Camera的权限就可以了。
参考代码:参考Camera2 原码
如果camera app中需要有录像的功能
参考代码:参考Camera2 原码
如果需要拍照生成的照片带gps的信息,需要申请这两个权限。
参考代码:参考Camera2 原码
在Camera app启动的时候,onCreate的时候创建surface,可以选择SurfaceView或者SurfaceTexture,这里是根据业务选择的,各有优缺点。
Surface准备好了,收到onSurfaceTextureAvailable后,app就可以打开camera和预览了。
参考代码:reopenCamera
首先要确定app要打开的是哪个Camera id,把这个id确定好,app就可以通过openCamera来打开正确的camera sensor了。
参见camera2代码:获取cameraid
CameraManager调用openCamera方法打开camera,参见api文档:openCamera
对应到Camera2 的原码位置:open —> 真正调用openCamera的位置 -->framework中CameraManager调用openCamera的位置–>openCameraForUid -->openCameraDeviceUserAsync–>connectDevice–>connectHelper–>CameraService.cpp中的方法connectHelper–>[makeClient ]
(http://aospxref.com/android-13.0.0_r3/xref/frameworks/av/services/camera/libcameraservice/CameraService.cpp#940)
1060 if (effectiveApiLevel == API_1) { // Camera1 API route
1061 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
1062 *client = new Camera2Client(cameraService, tmp, cameraService->mCameraServiceProxyWrapper,
1063 packageName, featureId, cameraId, api1CameraId, facing, sensorOrientation,
1064 clientPid, clientUid, servicePid, overrideForPerfClass, overrideToPortrait,
1065 forceSlowJpegMode);
1066 ALOGI("%s: Camera1 API (legacy), override to portrait %d, forceSlowJpegMode %d",
1067 __FUNCTION__, overrideToPortrait, forceSlowJpegMode);
1068 } else { // Camera2 API route
1069 sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
1070 static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
1071 *client = new CameraDeviceClient(cameraService, tmp,
1072 cameraService->mCameraServiceProxyWrapper, packageName, systemNativeClient,
1073 featureId, cameraId, facing, sensorOrientation, clientPid, clientUid, servicePid,
1074 overrideForPerfClass, overrideToPortrait);
1075 ALOGI("%s: Camera2 API, override to portrait %d", __FUNCTION__, overrideToPortrait);
1076 }
–>调用camera2的CameraDeviceClient -->Camera2ClientBase.
switch (providerTransport) {
116 case IPCTransport::HIDL:
117 mDevice =
118 new HidlCamera3Device(TClientBase::mCameraIdStr, mOverrideForPerfClass,
119 mLegacyClient);
120 break;
121 case IPCTransport::AIDL:
122 mDevice =
123 new AidlCamera3Device(TClientBase::mCameraIdStr, mOverrideForPerfClass,
124 mLegacyClient);
125 break;
126 default:
127 ALOGE("%s Invalid transport for camera id %s", __FUNCTION__,
128 TClientBase::mCameraIdStr.string());
129 return NO_INIT;
130 }
–>Camera2ClientBase.cpp 中的方法 initialize --> HidlCamera3Device.cpp 中的方法initialize -->
openHidlSession -->