android source code analysis -- camera camera

1, camera start

An activity starts the calling process:


   1. You can add the required layout file and draw the interface.

  2. Start the thread, start the hardware camera, and call CameraHolder.instance().open() to get an android.hardwareCamera
Example: mCameraDevice
        ensureCameraDevice()-->CameraHolder.instance().open()-->mCameraDevice.getParameters(); Make sure the camera is present and turn it on.
        startPreview(); Start viewfinder Preview

    3. Create the SurfaceHolder of VideoPreview and register the callback function. When the length and width of VideoPreview change, call the SurfaceChanged() function

        mSurfaceView = (SurfaceView) findViewById(;

        SurfaceHolder holder = mSurfaceView.getHolder();



  4. Is msimagecapture intent sent from SMS or contact editing interface to camera
mIsImageCaptureIntent = isImageCaptureIntent(); When the function returns true, it means to jump from the SMS or contact interface to the photographing interface;

   When it is false, it means that camera is started normally from the desktop program; Note that the function button area of the right interface required for two different situations changes.


  5. Mswitch.setswitch (switch_camera) sets the camera video switching button to switch to the camera mode.


    startPreview() restart Preview

    1. When pressing the home key to exit the program, stopPreview() will be disabled, so when entering again, onResume() function will be executed. In this function, the preview function should be restarted.

    2. Keepscreen onawhile() lights up the screen to prevent the screen from turning black.

2, Camera and video switching function

    1. The switcher inherits from ImageView and can be laid out directly in the layout file

    2. When mSwitch is true, the slider is below to display the photographing interface; When mSwitch is false, the slider is above to display the recording interface.

    3. When clicking or touching the photo recording switch button:

 MotionEvent.ACTION_UP-->tryToSetSwitch()-->mListener.onSwitchChanged(this, onOff)

   Implement the interface in, call switchtovideomode() -- > menuhelper. Gotovideomode (this) - > startcameraactivity(), and end the current program.

3, updateThumbnailButton() update function button to album button

    1. Call event:

        Called when initializeFirstTime is initialized

        Called when initializeSecondTime()

        Intent.ACTION_MEDIA_SCANNER_FINISHED, called at the end of SD card scanning

    2.mThumbController.isUriValid(), when there are pictures in the gallery, the value is TRUE; When there are no pictures in the gallery, the value is FALSE.

    3.updateLastImage() updates the display picture on the album button.

    4.mThumbController.updateDisplayIfNeeded()-->if (mUri == null) mButton.setImageDrawable(null); When there are no photos in the gallery, no photos are displayed here.

4, shutterButton Photo button

1. Focus calling process
Press the ShutterButton button on the screen. Listener thread call in callShutterButtonFocus(),This function calls the interface ShutterButton.OnShutterButtonListener Function of onShutterButtonFocus(). The interface is implemented ShutterButton.OnShutterButtonListener. so Functions in onShutterButtonFocus()Called.

The next execution process is ----- > dofocus() ---- > autofocus() ---- > mcameradevice. Autofocus()

   2. Photo calling process

    after call performClick(),Called in this function Medium onShutterButtonClick()

    The next execution process is ----->dosnap()----->ImagePicture.onSnap()----->ImageCapture.initiate()---->ImageCapture.capture() After this function is executed, takepicture Process completion

    takePicture When finished, surfaceChanged()Will be called to display the picture just taken on the screen. Callback interface in JpegPictureCallback Functions in onPictureTaken()Then execute. This function is called first ImageCapture.storeImage()take jpeg Image data is stored in memory.

   3. Display process of thumbnail in picture:

    ImageCapture.storeImage()--->ImageCapture.setLastPictureThumb()---> mThumbController.setData(uri, lastPictureThumb);

5, Layout file

1.attach_camera_control.xml. When entering camera from the SMS or contact interface, click "OK", "remake" and "Cancel" in the function button area on the right

2.camera_control.xml, the layout of the function button area on the right side of the main interface of camera and videocamera, preview the layout of the scenic spot on the left side of the camera main interface

4.video_camera.xml, the layout of the scenic spot is previewed on the left side of the videocamera main interface

5.on_screen_hint.xml, the output of SD card related information, such as the prompt "please insert SD card before using the camera" at the bottom of the screen

6, Camera SD card

1.BroadcastReceiver mReceiver,accept SD Status change message from the card.


  Intent.ACTION_MEDIA_UNMOUNTED Not hanging on

  Intent.ACTION_MEDIA_CHECKING SD This state is called when the card is never inserted into the insertion process

  Intent.ACTION_MEDIA_SCANNER_FINISHED scanning SD Card end, i.e SD The card is ready for use

3.checkStorage()->calculatePicturesRemaining()->MenuHelper.calculatePicturesRemaining()calculation SD Card remaining space

4.updateStorageHint()Update interface prompt display text

Where MenuHelper.NO_STORAGE_ERROR contains SD card status: MEDIA_CHECKING,MEDIA_SHARED, wait until the SD card is removed

state = Environment.getExternalStorageState() get SD card status

For example, state.equals(Environment.MEDIA_CHECKING) indicates that the SD card is being prepared

mStorageHint = OnScreenHint.makeText(this, noStorageText); Set the content to be displayed in the current interface; Display SD card status text

7, The camera SD card is a little more troublesome than the camera

1.onResume()Register in function receive event

       IntentFilter intentFilter =

                new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);






        mReceiver = new MyBroadcastReceiver();

        registerReceiver(mReceiver, intentFilter);

        mStorageStatus = getStorageStatus(true);

2.class MyBroadcastReceiver Handle the above registration event received

For example

          if (action.equals(Intent.ACTION_MEDIA_EJECT)) {



         }When pulled out SD Card is triggered


Here when remaining == NO_STORAGE_ERROR,Can be added such as SHARE,CHECKING Equal state processing

Different values are returned according to different states

For example:

             if (state.equals(Environment.MEDIA_CHECKING)) {              

                return STORAGE_STATUS_PREPARE;

            }else if(state.equals(Environment.MEDIA_SHARED)){              

                return STORAGE_STATUS_SHARE;

            }else {                     

                return STORAGE_STATUS_NONE;


Of course, the events filled in need to be in 1.2 Step to register and call this function


stay switch (mStorageStatus)Process required display information

For example, add the following case Statement to handle the information added above SHARE state


                errorMessage = getString(R.string.sdcard_busy_message);


5.mStorageHint = OnScreenHint.makeText(this, errorMessage);Set the text content to be displayed;Displayed in the current camera interface

8, Viewfinder preview to the right setting bar

1.stay oncreat()perhaps xml In the file, we can't find the layout corresponding to the setting item, so where is the setting item added?

changeHeadUpDisplayState()This function adds the entry of this layout.


The corresponding picture and text values are located in the xml Under file

Pay attention to me and share knowledge every day!

Tags: Android Back-end

Posted on Tue, 30 Nov 2021 22:17:19 -0500 by firecat318