publicstatic Camera mCameraDevice;
try {
Log.d(TAG,"open camera 0");
//writeFile(APK_FILE_PATH,1);
Log.d("qyh","opencamera"+System.currentTimeMillis());
mCameraDevice = Camera.open(0);/*打开camera,获取camera实例给mCameraDevice */
Log.d("qyh","sendMessage"+System.currentTimeMillis());
mHandler.sendEmptyMessageDelayed(DISMISS_ALL,500);
}catch (Exception e) {
Log.e(TAG,"open camera error");
//writeFile(APK_FILE_PATH,0);
e.printStackTrace();
return;
}
try{
mCameraDevice.setPreviewDisplay(holder);/*这个应该是数据的显示部分,待分析*/
}catch (Exception e) {
e.printStackTrace();
}
Camera.Parameters parameters = mCameraDevice.getParameters();/*获取驱动的相关参数*/
parameters.setPreviewSize(800, 600);/*重新设置预览的分辨率*/
mCameraDevice.setParameters(parameters);/*设置相关参数*/
mCameraDevice.startPreview();/*开始进入预览模式*/
以上是应用APP中打开camera的简要步骤,下面我们就对mCameraDevice = Camera.open(0);进行具体的跟进下:
STEP 1:
\frameworks\base\core\java\android\hardware\Camera.java
public static Camera open(int cameraId) {
if (!isPermissionGranted()) {
return null;
}
returnnewCamera(cameraId);
}
STEP 2:
\frameworks\base\core\java\android\hardware\Camera.java
/** used by Camera#open, Camera#open(int)*/
Camera(int cameraId) {
int err = cameraInitNormal(cameraId);
if (checkInitErrors(err)) {
switch(err) {
case EACCESS:
throw newRuntimeException("Fail to connect to camera service");
case ENODEV:
throw newRuntimeException("Camera initialization failed");
default:
// Should never hit this.
throw newRuntimeException("Unknown camera error");
}
}
}
STEP 3:
private int cameraInitNormal(int cameraId){
return cameraInitVersion(cameraId,CAMERA_HAL_API_VERSION_NORMAL_CONNECT);
}
/*
CAMERA_HAL_API_VERSION_NORMAL_CONNECT参数的定义
private static final int CAMERA_HAL_API_VERSION_NORMAL_CONNECT= -2;
*/
STEP 4:
private int cameraInitVersion(int cameraId,int halVersion) {
mShutterCallback = null;
mRawImageCallback = null;
mJpegCallback = null;
mPreviewCallback = null;
mPreviewRawDumpCallback = null;
mPostviewCallback = null;
mUsingPreviewAllocation = false;
mZoomListener = null;
Looper looper;
if ((looper = Looper.myLooper()) !=null) {
mEventHandler = new EventHandler(this,looper);
} else if ((looper =Looper.getMainLooper()) != null) {
mEventHandler = newEventHandler(this, looper);
} else {
mEventHandler = null;
}
return native_setup(newWeakReference<Camera>(this), cameraId, halVersion,
ActivityThread.currentOpPackageName());
}
/*
private native final int native_setup(Object camera_this, int cameraId,int halVersion,
String packageName);
native_setup是一个本地的方法,对应的JNI在下面步骤中。
*/
STEP 5:
\frameworks\base\core\jni\android_hardware_Camera.cpp
// connect tocamera service
static jint android_hardware_Camera_native_setup(JNIEnv *env,jobject thiz,
jobject weak_this, jint cameraId, jinthalVersion, jstring clientPackageName)
{
// Convert jstring to String16
const char16_t *rawClientName =reinterpret_cast<const char16_t*>(
env->GetStringChars(clientPackageName, NULL));
jsize rawClientNameLen =env->GetStringLength(clientPackageName);
String16 clientName(rawClientName,rawClientNameLen);
env->ReleaseStringChars(clientPackageName,
reinterpret_cast<const jchar*>(rawClientName));</