Android and android Official Website

Source: Internet
Author: User

Android and android Official Website
Camera. java

  • AutoFocus () Focus callback function
@ Override public void autoFocus () {// records the current focus start time. mFocusStartTime = System. currentTimeMillis (); // sets the callback focus of Camera to mCameraDevice. autoFocus (mAutoFocusCallback); // set the status of Camera to Focusing setCameraState (FOCUSING );}
  • Set camera status
private void setCameraState(int state) {        mCameraState = state;        switch (state) {            case SNAPSHOT_IN_PROGRESS:            case FOCUSING:                enableCameraControls(false);                break;            case IDLE:            case PREVIEW_STOPPED:                enableCameraControls(true);                break;        }    }
  • EnableCameraControls, set enable, whether you can click
/*** Do not click to set several buttons or views * @ param enable */private void enableCameraControls (boolean enable) {if (mIndicatorControlContainer! = Null) {mIndicatorControlContainer. setEnabled (enable);} if (mModePicker! = Null) mModePicker. setEnabled (enable); if (mZoomControl! = Null) mZoomControl. setEnabled (enable); if (mThumbnailView! = Null) mThumbnailView. setEnabled (enable );}
  • Enable settings of mIndicatorControlContainer
Override    public void setEnabled(boolean enabled) {        super.setEnabled(enabled);        final int count = getChildCount();        for (int i = 0; i < count; i++) {            View v = getChildAt(i);            // Zoom buttons and shutter button are controlled by the activity.            if (v instanceof AbstractIndicatorButton) {                v.setEnabled(enabled);                // Show or hide the indicator buttons during recording.                if (mCurrentMode == MODE_VIDEO) {                    v.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);                }            }        }        if (mCameraPicker != null) {            mCameraPicker.setEnabled(enabled);            if (mCurrentMode == MODE_VIDEO) {                mCameraPicker.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);            }        }    }
  • Processing in mAutoFocusCallback
Private final class AutoFocusCallback implements android. hardware. camera. autoFocusCallback {public void onAutoFocus (boolean focused, android. hardware. camera camera) {// if it is paused, do not focus on if (mPausing) return; // calculate the time difference between the current focus and start mAutoFocusTime = System. currentTimeMillis ()-mFocusStartTime; Log. v (TAG, "mAutoFocusTime =" + mAutoFocusTime + "ms"); // set the Camera status to setCameraState (IDLE); // call AutoFocus mFocusManager in FocusManager. onAutoFocus (focused );}}
FocusManager. java
/*** Focus * @ param focused */public void onAutoFocus (boolean focused) {Log. I (TAG, "focus used:" + (System. currentTimeMillis ()-focusStart); // if (mState = STATE_FOCUSING_SNAP_ON_FINISH) after the focus action is completed, the image is taken regardless of whether the focus succeeds or fails. If you want to take a photo and voice it out, you do not need to use AF to voice it out. if (focused) {// focused successfully, mState = STATE_SUCCESS;} else {// failed to focus mState = STATE_FAIL ;} // update the focus box UI & set the face recognition UI. You have set updateFocusUI () in various States; // take a photo and change the mState to STATE_IDLE capture ();} else if (mState = STATE_FOCUSING) {// if it is a state in focus, the occurrence of this State is divided into two types. half-pressing is triggered by pressing focus or touch focus, in this case, do not take a photo if (focused) {// focus success mState = STATE_SUCCESS; // do not make a sound in the continuous focus state. The focus callback will be completed before the photo is taken, all statuses are STATE_FOCUSING if (! Parameters. FOCUS_MODE_CONTINUOUS_PICTURE. equals (mFocusMode) {mListener. playSound (CameraSound. FOCUS_COMPLETE) ;}} else {// failed to focus mState = STATE_FAIL;} // update focus & Facial UI updateFocusUI (); // If this is triggered by touch focus, cancel focus after a // while. // if it is a touch focus, you need to wait a moment to cancel the focus if (mFocusArea! = Null) {mHandler. sendEmptyMessageDelayed (RESET_TOUCH_FOCUS, RESET_TOUCH_FOCUS_DELAY) ;}} else if (mState = STATE_IDLE) {// idle // User has released the focus key before focus completes. // Do nothing .}}
  • Process messages
Private class MainHandler extends Handler {@ Override public void handleMessage (Message msg) {switch (msg. what) {case RESET_TOUCH_FOCUS: {// cancel the focus cancelAutoFocus (); // start face recognition mListener. startFaceDetection (); break ;}}}}
/*** Call mListener before resetting the tap area. cancelAutofocus; otherwise, the focus mode will always be auto & tap focus, and the driver will not reset */private void cancelAutoFocus () {// place the focus box in the middle of the screen resetTouchFocus (); mListener. cancelAutoFocus (); if (mFaceView! = Null) mFaceView. resume (); mState = STATE_IDLE; updateFocusUI (); mHandler. removeMessages (RESET_TOUCH_FOCUS );}
  • Touch focus
/*** Touch, where the touch focus * @ param e * @ return */public boolean onTouch (MotionEvent e) occurs) {// if (! MInitialized | mState = STATE_FOCUSING_SNAP_ON_FINISH) return false; // enable the user to cancel the previously uncleared touch focus if (mFocusArea! = Null) & (mState = STATE_FOCUSING | mState = STATE_SUCCESS | mState = STATE_FAIL) {cancelAutoFocus () ;}// Initialize variables. int x = Math. round (e. getX (); int y = Math. round (e. getY (); int focusWidth = mFocusIndicatorRotateLayout. getWidth (); int focusHeight = mFocusIndicatorRotateLayout. getHeight (); int previewWidth = mPreviewFrame. getWidth (); int previewHeight = mPreviewFrame. getHeigh T (); if (mFocusArea = null) {mFocusArea = new ArrayList <Area> (); mFocusArea. add (new Area (new Rect (), 1); mMeteringArea = new ArrayList <Area> (); mMeteringArea. add (new Area (new Rect (), 1);} // convert the coordinates to the driving format. The AE area is larger, because the exposure is sensitive and easy, or the exposure is insufficient, if the area is too small. CalculateTapArea (focusWidth, focusHeight, 1f, x, y, previewWidth, previewHeight, mFocusArea. get (0 ). rect); calculateTapArea (focusWidth, focusHeight, 1.5f, x, y, previewWidth, previewHeight, mMeteringArea. get (0 ). rect); // Use margin to set the focus indicator to the touched area. relativeLayout. layoutParams p = (RelativeLayout. layoutParams) mFocusIndicatorRotateLayout. getLayoutParams (); int left = Util. clamp (x-focusWidth/2, 0, previewWidth-focusWidth); int top = Util. clamp (y-focusHeight/2, 0, previewHeight-focusHeight); p. setMargins (left, top, 0, 0); // Disable "center" rule because we no longer want to put it in the center. int [] rules = p. getRules (); rules [RelativeLayout. CENTER_IN_PARENT] = 0; mFocusIndicatorRotateLayout. requestLayout (); // stop face recognition because it is necessary to focus on recognition and measure area mListener. stopFaceDetection (); // sets the mListener in the focus area and measurement area. setFocusParameters (); // if you support touch-based focus & finger-lifting if (mFocusAreaSupported & (e. getAction () = MotionEvent. ACTION_UP) {autoFocus ();} else {// Just show the indicator in all other cases. updateFocusUI (); // Reset the metering area in 3 seconds. mHandler. removeMessages (RESET_TOUCH_FOCUS); mHandler. sendEmptyMessageDelayed (RESET_TOUCH_FOCUS, RESET_TOUCH_FOCUS_DELAY);} return true ;}
Focus on what the status can do and what cannot be done & Switch
  • STATE_IDLE
DoSnap (): If you are idle, you can take a picture of onAutoFocus (boolean focused): the focus button is removed before the user completes the focus. OnPreviewStarted (), onPreviewStopped (): status changes to idle cancelAutoFocus (): Cancel focus, status changes to idle updateFocusUI (): If idle & has a focus area, the focus box is displayed
  • STATE_FOCUSING
OnShutterUp (): If the focusmode is set to auto-focus and the status is in active-focus, remove the focus doSnap (): If it is in active-focus status, change the status to the onAutoFocus (boolean focused) state after focusing on the camera: Determine the focused state. If it is set to true, it changes to the focus success state. If it is set to false, it changes to the focus failure state onTouch (MotionEvent e ): if you are focusing & you have previously manually focused, remove the previous focus autoFocus (): The status changes to focus status updateFocusUI (): displays the focus box.
  • STATE_FOCUSING_SNAP_ON_FINISH
OnShutterUp (): If the status is not after the focus is taken, you can set FocusParametersonAutoFocus (boolean focused): to judge the focused state. If it is true, it becomes the focus success state. If it is false, it becomes the focus failure state, update the focus box UIonTouch (MotionEvent e): directly do not continue updateFocusUI (): display the focus box
  • STATE_SUCCESS
OnShutterDown (): If it is an automatic focus mode and is not in the focus success state, execute the focus onShutterUp (): If it is an automatic focus mode and the focus is successful, execute the cancel focus doSnap (): Take A Picture onTouch (MotionEvent e): allows the user to cancel the previously invisible touch focus updateFocusUI (): the focus box is successfully displayed
  • STATE_FAIL
OnShutterDown (): If it is an automatic focus mode and it is not a focus failure state, execute the focus onShutterUp (): If it is an automatic focus mode and the focus failure state, execute the cancel focus doSnap (): Take A Picture onTouch (MotionEvent e): allows the user to cancel the previously invisible touch focus updateFocusUI (): failed to display the focus box
I am the dividing line of tiantiao

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.