Android and androidadt
Com. android. camera. Camera. java mainly implements Activity, inherited from ActivityBase.
ActivityBase
Execute the process in ActivityBase:
Camera. java
Next, analyze Camera. java and execute the process:
1. onCreate
// Obtain the number of cameras, front and rear getPreferredCameraId (); // obtain the focus settings eg: continuous focus or other String [] defaultFocusModes = getResources (). getStringArray (R. array. pref_camera_focusmode_default_array); // instantiate the Focus management object mFocusManager = new FocusManager (mPreferences, defaultFocusModes); // enable the thread to start the camera mCameraOpenThread. start (); // whether the photo taking function is enabled by a third-party application mIsImageCaptureIntent = isImageCaptureIntent (); // sets the UI layout file setContentView (R. layout. camera); if (mIsImag ECaptureIntent) {// when a third party sends a photo, different UIS need to be displayed, such as canceling the keyboard mReviewDoneButton = (Rotatable) findViewById (R. id. btn_done); mReviewCancelButton = (Rotatable) findViewById (R. id. btn_cancel); findViewById (R. id. btn_cancel ). setVisibility (View. VISIBLE);} else {// reverse display of the thumbnail: mThumbnailView = (RotateImageView) findViewById (R. id. thumbnail); mThumbnailView. enableFilter (false); mThumbnailView. setVisibility (View. VISIBLE);} // 1 Dialog that can be rotated. for example, for the dialog set by the camera, this class implements the rotating parent class mRotateDialog = new RotateDialogController (this, R. layout. rotate_dialog); // sets the camera ID and writes mPreferences in SharedPreference. setLocalId (this, mCameraId); // update preferenceCameraSettings. upgradeLocalPreferences (mPreferences. getLocal (); // get the number of cameras mNumberOfCameras = CameraHolder. instance (). getNumberOfCameras (); // it seems that you want to get a quick photo. mQuickCapture = getIntent (). getBooleanExtra (EXTRA _ QUICK_CAPTURE, false); // reset the exposure value resetExposureCompensation () for the current preview; // hide Util such as the System navigation bar. enterLightsOutMode (getWindow (); // SurfaceView preview = (SurfaceView) findViewById (R. id. camera_preview); SurfaceHolder holder = preview. getHolder (); holder. addCallback (this); holder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS); try {// This join statement is used to ensure that the current thread starts running after the openCamera thread is executed. The main purpose is to ensure that the camera device has mCameraOpenThread enabled. join (); // After the thread is executed, the backend is null to allow the system to recycle resources mCameraOpenThread = null; if (mOpenCameraFail) {// failed to enable camera, and "unable to connect to camera" Util is displayed. showErrorAndFinish (this, R. string. cannot_connect_camera); return;} else if (mCameraDisabled) {// the camera has been disabled due to security policy restrictions. showErrorAndFinish (this, R. string. camera_disabled); return ;}} catch (InterruptedException ex) {// ignore} // enable the displayed sub-thread mCameraPreviewThread. start (); if (mIsImageCaptureIntent) {// if enabled by a third party, setupCaptureParams sets the parameter setupCaptureParams ();} else {// sets ModePicker mModePicker = (mopickdeer) 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); // camera IDmBackCameraId = CameraHolder. instance (). getBackCameraId (); mFrontCameraId = CameraHolder. instance (). getFrontCameraId (); // In startPreview, the notify method synchronized (mCameraPreviewThread) {try {mCameraPreviewThread. wait ();} catch (InterruptedException ex) {// ignore} // initialize various control buttons initializeIndicatorControl (); // initialize the photo sound mCameraSound = new CameraSound (); try {// make sure mCameraPreviewThread is displayed. join ();} catch (InterruptedException ex) {// ignore} mCameraPreviewThread = null;
2. surfaceCreated
Nothing done
3. surfaceChanged
// Make sure there is 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 cocould happen if onResume () is invoked after this // function. mSurfaceHolder = holder; if (mCameraDevice = null) return; if (mPausing | isFinishing () return; // Set preview display if the surface is being c Reated. preview was // already started. also restart the preview if display rotation has // changed. sometimes this happens when the device is held in portrait // and camera app is opened. rotation animation takes some time and // display rotation in onCreate may not be what we want. if (mCameraState = PREVIEW_STOPPED) {startPreview (); startFaceDetection ();} else {if (Util. getDisplayRotation (this )! = MDisplayRotation) {setDisplayOrientation ();} if (holder. isCreating () {// Set preview display if the surface is being created and preview // was already started. that means preview display was set to null // and we need to set it now. setPreviewDisplay (holder) ;}// If first time initialization is not finished, send a message to do // it later. we want to finish surfaceChanged as soon as possibl E to let // user see preview first. if (! MFirstTimeInitialized) {mHandler. sendEmptyMessage (FIRST_TIME_INIT);} else {initializeSecondTime ();}
If this is the first load, execute mHandler. sendEmptyMessage (FIRST_TIME_INIT); the corresponding processing is initializeFirstTime ();
/*** Initialization, first initialization * // Snapshots can only be taken after this is called. it shoshould be called * // once only. we cocould have done these things in onCreate () but we want to * // make preview screen appear as soon as possible. */private void initializeFirstTime () {if (mFirstTimeInitialized) return; // Create orientation listenter. this shoshould be done first because it // takes some time to get first or Ientation. mOrientationListener = new MyOrientationEventListener (Camera. this); mOrientationListener. enable (); // Initialize location sevice. boolean recordLocation = RecordLocationPreference. get (mPreferences, getContentResolver (); // initialize the sign at the top of the screen, such as the initOnScreenIndicator () when the exposure value is enabled. // The location service mLocationManager. recordLocation (recordLocation); keepMediaProviderInstance (); // check the bucket and initialize the storage directory checkStor Age (); // Initialize last picture button. mContentResolver = getContentResolver (); if (! MIsImageCaptureIntent) {// no thumbnail in image capture intent // Initialize the thumbnail initThumbnailButton ();} // Initialize shutter button. // initialize the photo button and set the listening event mShutterButton = (ShutterButton) findViewById (R. id. shutter_button); mShutterButton. setOnShutterButtonListener (this); mShutterButton. setVisibility (View. VISIBLE); // Initialize focus UI. mPreviewFrame = findViewById (R. id. camera_preview); mPreviewFrame. setOnTouchListener (this); // The mFocusAreaIndicator = (RotateLayout) findViewById (R. id. focus_indicator_rotate_layout); CameraInfo info = CameraHolder. instance (). getCameraInfo () [mCameraId]; boolean mirror = (info. facing = CameraInfo. CAMERA_FACING_FRONT); mFocusManager. initialize (mFocusAreaIndicator, mPreviewFrame, mFaceView, this, mirror, mDisplayOrientation); // initialize the image storage thread mImageSaver = new ImageSaver (); // sets the screen brightness Util. initializeScreenBrightness (getWindow (), getContentResolver (); // register the broadcasting related to the SD card, such as pulling out the installIntentFilter () of the memory card or something; // initialize the scaling UI initializeZoom (); // updateOnScreenIndicators (); // start face detection startFaceDetection (); // Show the tap to focus toast if this is the first start. // if this is the first startup, the system prompts the user to "Touch Focus" if (mFocusAreaSupported & mPreferences. getBoolean (CameraSettings. KEY_CAMERA_FIRST_USE_HINT_SHOWN, true) {// Delay the toast for one second to wait for orientation. mHandler. sendEmptyMessageDelayed (SHOW_TAP_TO_FOCUS_TOAST, 1000);} mFirstTimeInitialized = true; addIdleHandler ();}
If not, execute initializeSecondTime ();
/*** // If the activity is paused and resumed, this method will be called in * // onResume. */private void initializeSecondTime () {// Start orientation listener as soon as possible because it takes/some time to get first orientation. // enable the orientation flip setting, which includes the animated mOrientationListener during flip. enable (); // Start location update if needed. boolean recordLocation = RecordLocationPreference. get (mPreference S, getContentResolver (); mLocationManager. recordLocation (recordLocation); // sets the SD card broadcast installIntentFilter (); mImageSaver = new ImageSaver (); // initializes the Zoom initializeZoom (); // token = media Provider object keepMediaProviderInstance (); // check hard disk checkStorage (); // fade out retake and done's Button hidePostCaptureAlert (); if (! MIsImageCaptureIntent) {// if not enabled by a third party, update the thumbnail updateThumbnailButton (); mModePicker. setCurrentMode (ModePicker. MODE_CAMERA );}}
4. surfaceDestroyed
stopPreview();mSurfaceHolder = null;
I am the dividing line of tiantiao