zoukankan      html  css  js  c++  java
  • Android — Camera聚焦流程

    Camera.java                                                                          

    • autoFocus()聚焦回调函数
    @Override
      public void autoFocus() {
        //记录当前聚焦开始时间
        mFocusStartTime = System.currentTimeMillis();
        //设置Camera的回调聚焦
        mCameraDevice.autoFocus(mAutoFocusCallback);
        //设置Camera的状态为Focusing
        setCameraState(FOCUSING);
      }
    • 设置相机状态
    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,设置enable,是否可以点击
    /**
       * 设置几个button或者view不可点击
       * @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);
      }
    • mIndicatorControlContainer的enable的设置
    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);
          }
        }
      }
    • mAutoFocusCallback中的处理
    private final class AutoFocusCallback
          implements android.hardware.Camera.AutoFocusCallback {
      public void onAutoFocus(
        boolean focused, android.hardware.Camera camera) {
          //如果是暂停状态,不聚焦
          if (mPausing) return;
          //算出当前到聚焦开始的时间差
          mAutoFocusTime = System.currentTimeMillis() - mFocusStartTime;
          Log.v(TAG, "mAutoFocusTime = " + mAutoFocusTime + "ms");
          //设置Camera状态为
          setCameraState(IDLE);
          //调用FocusManager中的AutoFocus
          mFocusManager.onAutoFocus(focused);
      }
        }

    FocusManager.java                                                                  

    /**
       * 聚焦
       * @param focused
       */
      public void onAutoFocus(boolean focused) {
        Log.i(TAG, "focus used : " + (System.currentTimeMillis() - focusStart));
        //正在进行聚焦,拍照动作必须再聚焦完之后
        if (mState == STATE_FOCUSING_SNAP_ON_FINISH) {
          // 无论聚焦成功还是失败,都会拍照。如果要进行拍照发声,就无需AF发声了
          if (focused) {
            //聚焦成功
            mState = STATE_SUCCESS;
          } else {
            //聚焦失败
            mState = STATE_FAIL;
          }
          //更新聚焦框UI&&设置人脸识别UI已经各种状态的设置
          updateFocusUI();
          //拍照,mState的状态变为STATE_IDLE
          capture();
        } else if (mState == STATE_FOCUSING) {//如果是聚焦中的状态
          //此状态的发生分为两种,half-pressing按压聚焦或者触摸聚焦被触发,这个时候不要发生拍照动作
          if (focused) {
            //聚焦成功
            mState = STATE_SUCCESS;
            //在连续聚焦状态不要发声,聚焦回调会在拍照前完成,所有状态一直为STATE_FOCUSING
            if (!Parameters.FOCUS_MODE_CONTINUOUS_PICTURE.
                equals(mFocusMode)) {
              mListener.playSound(CameraSound.FOCUS_COMPLETE);
            }
          } else {
            //聚焦失败
            mState = STATE_FAIL;
          }
          //更新聚焦&&人脸UI
          updateFocusUI();
          // If this is triggered by touch focus, cancel focus after a
          // while.
          //如果是触摸聚焦,需要延迟一下取消掉聚焦
          if (mFocusArea != null) {
            mHandler.sendEmptyMessageDelayed(RESET_TOUCH_FOCUS, RESET_TOUCH_FOCUS_DELAY);
          }
        } else if (mState == STATE_IDLE) {//空闲
          // User has released the focus key before focus completes.
          // Do nothing.
        }
      }
    • 处理消息
    private class MainHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
          switch (msg.what) {
            case RESET_TOUCH_FOCUS: {
              //取消掉聚焦
              cancelAutoFocus();
              //开始人脸识别
              mListener.startFaceDetection();
              break;
            }
          }
        }
      }
    /**
         * 再重置tap area之前调用mListener.cancelAutofocus,否则,聚焦模式将一直是自动&tap聚焦,并且驱动也不会重置
         */
        private void cancelAutoFocus() {
      //放置聚焦框到屏幕中间
      resetTouchFocus();
      mListener.cancelAutoFocus();
      if (mFaceView != null) mFaceView.resume();
      mState = STATE_IDLE;
      updateFocusUI();
      mHandler.removeMessages(RESET_TOUCH_FOCUS);
        }
    • 触摸聚焦
    /**
         * 触摸,,这里会发生触摸聚焦
         * @param e
         * @return
         */
        public boolean onTouch(MotionEvent e) {
      //没有初始化或者拍照前的聚焦的状态,直接返回
      if (!mInitialized || mState == STATE_FOCUSING_SNAP_ON_FINISH) return false;
    
      //让用户可以取消掉之前未消失的触摸聚焦
      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.getHeight();
      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));
      }
    
      //将坐标转换为驱动的格式。AE面积更大,因为曝光会敏感和容易,或者说曝光不足,如果面积太小了。
      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();
    
      //停止人脸识别,因为要进行识别聚焦和测量area
      mListener.stopFaceDetection();
    
      //设置聚焦区域&测量区域
      mListener.setFocusParameters();
      //如果支持触摸聚焦&&手指抬起
      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;
        }

    聚焦各个状态能做什么事不能做什么事&切换                                       

    • STATE_IDLE
    doSnap():空闲状态可以执行拍照
    onAutoFocus(boolean focused):用户再聚焦完成前放掉了聚焦按钮,所以不做任何事。
    onPreviewStarted(),onPreviewStopped():状态都变为空闲
    cancelAutoFocus():取消聚焦,状态变为空闲
    updateFocusUI():如果空闲&有聚焦区域,显示聚焦框框
    • STATE_FOCUSING
    onShutterUp():如果是focusmode为自动聚焦,状态为正在聚焦,则取消掉聚焦
    doSnap():如果是正在聚焦状态,将状态改为聚焦完拍照状态
    onAutoFocus(boolean focused):判断focused,为true变为聚焦成功状态,为false变为聚焦失败状态
    onTouch(MotionEvent e):如果正在聚焦&之前有手动聚焦了,则取消掉之前的聚焦
    autoFocus():状态变为聚焦状态
    updateFocusUI():显示聚焦的框框
    • STATE_FOCUSING_SNAP_ON_FINISH
    onShutterUp():状态不是聚焦完拍照,可以设置FocusParameters
    onAutoFocus(boolean focused):判断focused,为true变为聚焦成功状态,为false变为聚焦失败状态,更新聚焦框UI
    onTouch(MotionEvent e):直接不继续操作下去
    updateFocusUI():显示聚焦的框框
    • STATE_SUCCESS
    onShutterDown():如果是自动聚焦mode,并且不为聚焦成功状态,执行聚焦
    onShutterUp(): 如果是自动聚焦mode,并且为聚焦成功状态,执行取消聚焦
    doSnap():拍照
    onTouch(MotionEvent e):让用户可以取消掉之前未消失的触摸聚焦
    updateFocusUI():聚焦框框显示成功
    • STATE_FAIL
    onShutterDown():如果是自动聚焦mode,并且不为聚焦失败状态,执行聚焦
    onShutterUp(): 如果是自动聚焦mode,并且为聚焦失败状态,执行取消聚焦
    doSnap():拍照
    onTouch(MotionEvent e):让用户可以取消掉之前未消失的触摸聚焦
    updateFocusUI():聚焦框框显示失败
  • 相关阅读:
    LevelDB安装配置
    Enumerable.Intersect方法来生成2个序列的交集
    .Net 分页功能实现
    Canvas统计图表(多边形,蜘蛛网,渐变色)
    存储格式与压缩算法
    Hive数仓构建及数据倾斜
    团队沟通
    git错误:OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443
    linux搭建gitlab服务器
    2018牛客网暑期ACM多校训练营第一场
  • 原文地址:https://www.cnblogs.com/zl1991/p/5337533.html
Copyright © 2011-2022 走看看