com.android.camera.Camera.java,首要的完成Activity,承继于ActivityBase。
ActivityBase
在ActivityBase中履行流程:
onCreate中进行判别是否是平板;
onResume中判别是否锁屏,锁屏camera不存在时分,mOnResumePending置为true,不然置为false并履行doOnResume;
onWindowFocusChanged中判别是否获取到焦点mOnResumePending,满意的话履行doOnResume;
onPause中将mOnResumePending置为false;
Camera.java
接下来剖析Camera.java,履行流程:
1、onCreate
仿制代码
// 取得摄像头的数量,前置和后置
getPreferredCameraId();
// 取得对焦设置eg:接连对焦或许其它
String[] defaultFocusModes = getResources().getStringArray(R.array.pref_camera_focusmode_default_array);
//实例化Focus办理目标
mFocusManager = new FocusManager(mPreferences, defaultFocusModes);
// 敞开线程来发动摄像头
mCameraOpenThread.start();
// 是否是第三方使用发动摄影功用
mIsImageCaptureIntent = isImageCaptureIntent();
// 设置UI布局文件
setContentView(R.layout.camera);
if (mIsImageCaptureIntent) {
// 当第三方其送摄影,需求显现不同的UI,比方撤销键盘
mReviewDoneButton = (Rotatable) findViewById(R.id.btn_done);
mReviewCancelButton = (Rotatable) findViewById(R.id.btn_cancel);
findViewById(R.id.btn_cancel).setVisibility(View.VISIBLE);
} else {
// 反之显现缩略图
mThumbnailView = (RotateImageView) findViewById(R.id.thumbnail);
mThumbnailView.enableFilter(false);
mThumbnailView.setVisibility(View.VISIBLE);
}
// 一个能旋转的dialog.比方相机设置的dialog,这个类完成了旋转的父类
mRotateDialog = new RotateDialogController(this, R.layout.rotate_dialog);
// 设置camera的ID,写道SharedPreference中
mPreferences.setLocalId(this, mCameraId);
// 更新preference
CameraSettings.upgradeLocalPreferences(mPreferences.getLocal());
// 取得相机数
mNumberOfCameras = CameraHolder.instance().getNumberOfCameras();
// 貌似是取得是否是快速摄影
mQuickCapture = getIntent().getBooleanExtra(EXTRA_QUICK_CAPTURE, false);
// 为当时的preview重置曝光值
resetExposureCompensation();
// 躲藏体系导航栏等
Util.enterLightsOutMode(getWindow());
//SurfaceView
SurfaceView preview = (SurfaceView) findViewById(R.id.camera_preview);
SurfaceHolder holder = preview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
try {
// 这个join句子便是为了保证openCamera的线程履行完后,当时的线程才开端运转。首要是为了保证camera设备被翻开了
mCameraOpenThread.join();
// 线程履行完后置为空来让体系收回资源
mCameraOpenThread = null;
if (mOpenCameraFail) {
// 翻开camera失利,显现“无法连接到相机”
Util.showErrorAndFinish(this, R.string.cannot_connect_camera);
return;
} else if (mCameraDisabled) {
// 因为安全方针约束,相机已被停用
Util.showErrorAndFinish(this, R.string.camera_disabled);
return;
}
} catch (InterruptedException ex) {
// ignore
}
//敞开显现的子线程
mCameraPreviewThread.start();
if (mIsImageCaptureIntent) {
//如果是第三方敞开的 ,setupCaptureParams 设置摄影的参数
setupCaptureParams();
} else {
//设置ModePicker
mModePicker = (ModePicker) findViewById(R.id.mode_picker);
mModePicker.setVisibility(View.VISIBLE);
mModePicker.setOnModeChangeListener(this);
mModePicker.setCurrentMode(ModePicker.MODE_CAMERA);
}
mZoomControl = (ZoomControl) findViewById(R.id.zoom_control);
mOnScreenIndicators = (Rotatable) findViewById(R.id.on_screen_indicators);
mLocationManager = new LocationManager(this, this);
//摄像头ID
mBackCameraId = CameraHolder.instance().getBackCameraId();
mFrontCameraId = CameraHolder.instance().getFrontCameraId();
// 在startPreview里边有notify办法
synchronized (mCameraPreviewThread) {
try {
mCameraPreviewThread.wait();
} catch (InterruptedException ex) {
// ignore
}
}
// 初始化各种操控按钮
initializeIndicatorControl();
//初始化摄影声响
mCameraSound = new CameraSound();
try {
//保证显现
mCameraPreviewThread.join();
} catch (InterruptedException ex) {
// ignore
}
mCameraPreviewThread = null;
仿制代码
2、surfaceCreated
啥都没做
3、surfaceChanged
仿制代码
// 保证在holder中有surface
if (holder.getSurface() == null) {
Log.d(TAG, holder.getSurface() == null);
return;
}
// We need to save the holder for later use, even when the mCameraDevice
// is null. This could happen if onResume() is invoked after this