Exemple #1
1
 @Override
 public synchronized void start() {
   theCamera = Camera.open();
   Camera.Parameters myParameters = theCamera.getParameters();
   myParameters.setPreviewSize(theCamPreviewWidth, theCamPreviewHeight);
   myParameters.setPreviewFpsRange(MIN_FPS, MAX_FPS);
   myParameters.setSceneMode(SCENE_MODE_NIGHT);
   myParameters.setFocusMode(FOCUS_MODE_AUTO);
   myParameters.setWhiteBalance(WHITE_BALANCE_AUTO);
   theCamera.setParameters(myParameters);
   theCamera.setDisplayOrientation(DISPLAY_ORIENTATION);
 }
Exemple #2
0
  // Called when holder has changed
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

    Camera.Parameters parameters = camera.getParameters();
    List<String> focusModes = parameters.getSupportedFocusModes();
    if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
      Log.d("camera", "focus_mode_auto");
      parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    } else {
      Log.d("camera", "focus_mode_macro");
      parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
    }
    Log.d("camera", "orientation: " + getResources().getConfiguration().orientation);
    int orientation = getResources().getConfiguration().orientation;
    switch (orientation) {
      case Surface.ROTATION_0:
        Log.d("camera", "ROTATION_0");
        camera.setDisplayOrientation(180);
        break;
      case Surface.ROTATION_90:
        Log.d("camera", "ROTATION_90");
        camera.setDisplayOrientation(90);
        break;
      case Surface.ROTATION_180:
        Log.d("camera", "ROTATION_180");
        camera.setDisplayOrientation(0);
        break;
      case Surface.ROTATION_270:
        Log.d("camera", "ROTATION_270");
        camera.setDisplayOrientation(270);
        break;
    }

    camera.setParameters(parameters);
    camera.startPreview();
  }
 private void setCameraFocusMode(Camera.Parameters parameters) {
   // MODE_INFINITY is preferred mode.
   List<String> supportedFocusModes = parameters.getSupportedFocusModes();
   if (supportedFocusModes != null) {
     if (supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_INFINITY)) {
       parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
       Log.v(TAG, "Set focus mode INFINITY");
     } else if (supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_FIXED)) {
       parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED);
       Log.v(TAG, "Set focus mode FIXED");
     } else if (supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
       parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
       Log.v(TAG, "Set focus mode AUTO");
     }
   }
 }
  @Override
  public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
    Log.e("ok", "surfaceChanged => w=" + i2 + ", h=" + i3);
    // before changing the application orientation, you need to stop the preview, rotate and then
    // start it again
    if (surfaceHolder.getSurface() == null) // check if the surface is ready to receive camera data
    return;

    try {
      mCamera.stopPreview();
    } catch (Exception e) {
      // this will happen when you are trying the camera if it's not running
    }

    // now, recreate the camera preview
    try {

      Camera.Parameters parameters = mCamera.getParameters();
      List<String> focusModes = parameters.getSupportedFocusModes();
      if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
      }
      parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
      mCamera.setParameters(parameters);
      mCamera.setDisplayOrientation(90);
      mCamera.startPreview(mHolder);
    } catch (IOException e) {
      Log.d("ERROR", "Camera error on surfaceChanged " + e.getMessage());
    }
  }
  private void initPreview(int width, int height) {
    if (camera != null && previewHolder.getSurface() != null) {
      try {
        camera.setPreviewDisplay(previewHolder);
      } catch (Throwable t) {

      }

      if (!cameraConfigured) {
        Camera.Parameters parameters = camera.getParameters();
        Camera.Size size = getBestPreviewSize(width, height, parameters);
        Camera.Size pictureSize = getLargestPictureSize(parameters);

        if (size != null && pictureSize != null) {
          if (parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_AUTO)) {

            // set the focus mode
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
          }
          parameters.setPreviewSize(size.width, size.height);
          parameters.setPictureSize(pictureSize.width, pictureSize.height);
          parameters.setPictureFormat(ImageFormat.JPEG);
          camera.setParameters(parameters);
          cameraConfigured = true;
        }
      }
    }
  }
  @SuppressLint("InlinedApi")
  private static void setDesiredCameraParameters(
      final Camera camera, final Camera.Size cameraResolution, final boolean continuousAutoFocus) {
    final Camera.Parameters parameters = camera.getParameters();
    if (parameters == null) return;

    final List<String> supportedFocusModes = parameters.getSupportedFocusModes();
    final String focusMode =
        continuousAutoFocus
            ? findValue(
                supportedFocusModes,
                Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE,
                Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO,
                Camera.Parameters.FOCUS_MODE_AUTO,
                Camera.Parameters.FOCUS_MODE_MACRO)
            : findValue(
                supportedFocusModes,
                Camera.Parameters.FOCUS_MODE_AUTO,
                Camera.Parameters.FOCUS_MODE_MACRO);
    if (focusMode != null) parameters.setFocusMode(focusMode);

    parameters.setPreviewSize(cameraResolution.width, cameraResolution.height);

    camera.setParameters(parameters);
  }
Exemple #7
0
  public CustomeCamera(
      Activity activity, FrameLayout cameraPreviewLayout, Integer size, Integer quality)
      throws Exception {
    this.activity = activity;
    this.size = size;
    this.quality = quality;

    // Create an instance of Camera
    try {
      mCamera = getCameraInstance();
    } catch (Exception e) {
      throw new Exception(e.getMessage());
    }
    try {
      mCamera.setDisplayOrientation(90);
    } catch (Exception e) {
      Log.d(TAG, e.getMessage());
    }

    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
      case Surface.ROTATION_0:
        degrees = 0;
        break; // Natural orientation
      case Surface.ROTATION_90:
        degrees = 90;
        break; // Landscape left
      case Surface.ROTATION_180:
        degrees = 180;
        break; // Upside down
      case Surface.ROTATION_270:
        degrees = 270;
        break; // Landscape right
    }
    int rotate = (info.orientation - degrees + 360) % 360;

    // STEP #2: Set the 'rotation' parameter
    Camera.Parameters params = mCamera.getParameters();
    params.setRotation(rotate);

    List<String> focusModes = params.getSupportedFocusModes();
    if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
      params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    }
    if (params.getFocusMode() == Camera.Parameters.FOCUS_MODE_AUTO) autoFocusEnabled = true;
    else autoFocusEnabled = false;

    // TODO for the test
    autoFocusEnabled = false;

    mCamera.setParameters(params);

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(activity, mCamera);
    cameraPreviewLayout.addView(mPreview);
  }
  void setDesiredCameraParameters(Camera camera, boolean safeMode) {
    Camera.Parameters parameters = camera.getParameters();

    if (parameters == null) {
      Log.w(
          TAG,
          "Device error: no camera parameters are available. Proceeding without configuration.");
      return;
    }

    Log.i(TAG, "Initial camera parameters: " + parameters.flatten());

    if (safeMode) {
      Log.w(TAG, "In camera config safe mode -- most settings will not be honored");
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

    initializeTorch(parameters, prefs, safeMode);

    String focusMode = null;
    if (prefs.getBoolean(PreferencesActivity.KEY_AUTO_FOCUS, true)) {
      if (safeMode || prefs.getBoolean(PreferencesActivity.KEY_DISABLE_CONTINUOUS_FOCUS, false)) {
        focusMode =
            findSettableValue(
                parameters.getSupportedFocusModes(), Camera.Parameters.FOCUS_MODE_AUTO);
      } else {
        focusMode =
            findSettableValue(
                parameters.getSupportedFocusModes(),
                "continuous-picture", // Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE in 4.0+
                "continuous-video", // Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO in 4.0+
                Camera.Parameters.FOCUS_MODE_AUTO);
      }
    }
    // Maybe selected auto-focus but not available, so fall through here:
    if (!safeMode && focusMode == null) {
      focusMode =
          findSettableValue(
              parameters.getSupportedFocusModes(),
              Camera.Parameters.FOCUS_MODE_MACRO,
              "edof"); // Camera.Parameters.FOCUS_MODE_EDOF in 2.2+
    }
    if (focusMode != null) {
      parameters.setFocusMode(focusMode);
    }

    if (prefs.getBoolean(PreferencesActivity.KEY_INVERT_SCAN, false)) {
      String colorMode =
          findSettableValue(
              parameters.getSupportedColorEffects(), Camera.Parameters.EFFECT_NEGATIVE);
      if (colorMode != null) {
        parameters.setColorEffect(colorMode);
      }
    }

    parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
    camera.setParameters(parameters);
  }
Exemple #9
0
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  private boolean prepareVideoRecorder() {

    // BEGIN_INCLUDE (configure_preview)
    mCamera = CameraHelper.getDefaultCameraInstance();

    Camera.Parameters parameters = mCamera.getParameters();
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
    mCamera.setDisplayOrientation(90);
    mCamera.setParameters(parameters);

    // BEGIN_INCLUDE (configure_media_recorder)
    mMediaRecorder = new MediaRecorder();

    // Step 1: Unlock and set camera to MediaRecorder
    mCamera.unlock();

    // Camera.Parameters parameters = mCamera.getParameters();

    mMediaRecorder.setCamera(mCamera);
    CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_QVGA);

    // Log.d(TAG, profile.videoBitRate+"<- videoBitRate r");
    profile.videoBitRate = 1024000;

    // Step 2: Set sources
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    // Step 3: Set all values contained in profile except audio settings
    mMediaRecorder.setOutputFormat(profile.fileFormat);
    mMediaRecorder.setVideoEncoder(profile.videoCodec);
    mMediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
    mMediaRecorder.setVideoFrameRate(profile.videoFrameRate);
    mMediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
    mMediaRecorder.setOrientationHint(90);
    mMediaRecorder.setVideoFrameRate(24);

    // Step 4: Set output file
    save_to = CameraHelper.getOutputMediaFile(CameraHelper.MEDIA_TYPE_VIDEO).toString();
    mMediaRecorder.setOutputFile(save_to);

    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

    // Step 5: Prepare configured MediaRecorder
    try {
      mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
      Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
      releaseMediaRecorder();
      return false;
    } catch (IOException e) {
      Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
      releaseMediaRecorder();
      return false;
    }
    return true;
  }
 private void setupCameraParams() {
   if (mCamera != null) {
     mCamera.setPreviewCallback(this);
     Camera.Parameters params = mCamera.getParameters();
     if (params != null) {
       List<String> supportedFocusModes = mCamera.getParameters().getSupportedFocusModes();
       if (supportedFocusModes != null
           && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
         params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
       } else if (supportedFocusModes != null
           && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)
           && !supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_FIXED)) {
         params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
         mHandler.post(cameraFocusRunner);
       }
       mCamera.setParameters(params);
     }
   }
 }
  private void setIfAutoFocusSupported() {
    // get Camera parameters
    Camera.Parameters params = mCamera.getParameters();

    List<String> focusModes = params.getSupportedFocusModes();
    if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
      params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
      mCamera.setParameters(params);
    }
  }
Exemple #12
0
  /** Initialize camera with all good paramters. */
  private void initializeCamera() {
    // - Open camera.
    mEasyCamera = DefaultEasyCamera.open();

    // - Make sure preview is not started.
    mEasyCamera.stopPreview();

    // - Set good parameters.
    Camera.Parameters params = mEasyCamera.getParameters();
    params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    mEasyCamera.setParameters(params);
    mEasyCamera.setDisplayOrientation(90);
  }
 // ---------------------------- CAMERA FUNCTIONS ----------------------------------
 private static Camera setCameraInstance() {
   Camera c = null;
   try {
     c = Camera.open(); // attempt to get a Camera instance
     Camera.Parameters params = c.getParameters();
     params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
     c.setParameters(params);
   } catch (Exception e) {
     Log.e(LOG_TAG, "Camera is not available or does not exist.");
     e.printStackTrace();
   }
   return c;
 }
 /** A safe way to get an instance of the Camera object. */
 private Camera getCameraInstance() {
   Camera c = null;
   try {
     cameraId = 0;
     c = Camera.open(); // attempt to get a Camera instance
     Camera.Parameters p = c.getParameters();
     p.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
     c.setParameters(p);
   } catch (Exception e) {
     // Camera is not available (in use or does not exist)
   }
   return c; // returns null if camera is unavailable
 }
Exemple #15
0
  public synchronized void setupCamera(int width, int height) {
    if (mCamera != null) {
      Log.i(TAG, "Setup Camera - " + width + "x" + height);
      Camera.Parameters params = mCamera.getParameters();
      List<Camera.Size> sizes = params.getSupportedPreviewSizes();
      mFrameWidth = width;
      mFrameHeight = height;

      // selecting optimal camera preview size
      {
        int minDiff = Integer.MAX_VALUE;
        for (Camera.Size size : sizes) {
          if (Math.abs(size.height - height) < minDiff) {
            mFrameWidth = size.width;
            mFrameHeight = size.height;
            minDiff = Math.abs(size.height - height);
          }
        }
      }

      params.setPreviewSize(getFrameWidth(), getFrameHeight());

      List<String> FocusModes = params.getSupportedFocusModes();
      if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
      }

      mCamera.setParameters(params);

      /* Now allocate the buffer */
      params = mCamera.getParameters();
      int size = params.getPreviewSize().width * params.getPreviewSize().height;
      size = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8;
      mBuffer = new byte[size];
      /* The buffer where the current frame will be copied */
      mFrame = new byte[size];
      mCamera.addCallbackBuffer(mBuffer);

      /* Notify that the preview is about to be started and deliver preview size */
      onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height);

      try {
        setPreview();
      } catch (IOException e) {
        Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e);
      }

      /* Now we can start a preview */
      mCamera.startPreview();
    }
  }
  /**
   * 设置焦点和测光区域
   *
   * @param event
   */
  public void focusOnTouch(MotionEvent event) {
    try {
      if (mCamera == null) {
        return;
      }
      Camera.Parameters parameters = mCamera.getParameters();
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        int[] location = new int[2];
        mSurfaceView.getLocationOnScreen(location);
        Rect focusRect =
            calculateTapArea(
                view_focus.getWidth(),
                view_focus.getHeight(),
                1f,
                event.getRawX(),
                event.getRawY(),
                location[0],
                location[0] + mSurfaceView.getWidth(),
                location[1],
                location[1] + mSurfaceView.getHeight());
        Rect meteringRect =
            calculateTapArea(
                view_focus.getWidth(),
                view_focus.getHeight(),
                1.5f,
                event.getRawX(),
                event.getRawY(),
                location[0],
                location[0] + mSurfaceView.getWidth(),
                location[1],
                location[1] + mSurfaceView.getHeight());
        if (parameters.getMaxNumFocusAreas() > 0) {
          List<Camera.Area> focusAreas = new ArrayList<Camera.Area>();
          focusAreas.add(new Camera.Area(focusRect, 1000));
          parameters.setFocusAreas(focusAreas);
        }

        if (parameters.getMaxNumMeteringAreas() > 0) {
          List<Camera.Area> meteringAreas = new ArrayList<Camera.Area>();
          meteringAreas.add(new Camera.Area(meteringRect, 1000));
          parameters.setMeteringAreas(meteringAreas);
        }
      }

      parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
      mCamera.setParameters(parameters);
      mCamera.autoFocus(this);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemple #17
0
  /** Handles camera opening */
  private void openCamera(int which) {
    if (mCamera != null) {
      mCamera.stopPreview();
      mCamera.release();
      mCamera = null;
    }

    if (mCameraId >= 0) {
      Camera.getCameraInfo(mCameraId, mCameraInfo);
      if (which == FRONT) mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
      else mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);

      params = mCamera.getParameters();
      params.setRotation(0);

      /** set focus mode */
      List<String> FocusModes = params.getSupportedFocusModes();
      if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
      }

      mCamera.setParameters(params);
      try {
        if (mSurfaceTexture != null) {
          mCamera.setPreviewTexture(mSurfaceTexture);
          mCamera.startPreview();
        }
      } catch (Exception ex) {
      }
    }

    if (mCamera == null || mSharedData == null) {
      return;
    }

    int orientation = mCameraInfo.orientation + rot_angle;

    Matrix.setRotateM(mSharedData.mOrientationM, 0, orientation, 0f, 0f, 1f);

    Camera.Size size = mCamera.getParameters().getPreviewSize();
    if (orientation % 90 == 0) {
      int w = size.width;
      size.width = size.height;
      size.height = w;
    }

    mSharedData.mAspectRatioPreview[0] = (float) Math.min(size.width, size.height) / size.width;
    mSharedData.mAspectRatioPreview[1] = (float) Math.min(size.width, size.height) / size.height;
  }
Exemple #18
0
  /**
   * Sets the focus mode.
   *
   * @param mode the focus mode
   * @return {@code true} if the focus mode is set, {@code false} otherwise
   * @see #getFocusMode()
   */
  public boolean setFocusMode(@FocusMode String mode) {
    synchronized (mCameraLock) {
      if (mCamera != null && mode != null) {
        Camera.Parameters parameters = mCamera.getParameters();
        if (parameters.getSupportedFocusModes().contains(mode)) {
          parameters.setFocusMode(mode);
          mCamera.setParameters(parameters);
          mFocusMode = mode;
          return true;
        }
      }

      return false;
    }
  }
  // zoom in and enable flash
  protected boolean initializeCamera(int width, int height) {
    boolean ret = super.initializeCamera(width, height);

    Camera.Parameters params = mCamera.getParameters();

    List<String> FocusModes = params.getSupportedFocusModes();
    if (FocusModes != null && FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
      params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
    } else if (FocusModes != null
        && FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
      params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    }

    List<String> FlashModes = params.getSupportedFlashModes();
    if (FlashModes != null && FlashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)) {
      params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    }

    if (params.isZoomSupported()) enableZoomControls(params);

    mCamera.setParameters(params);

    return ret;
  }
  private void initCameraParameters() {
    Camera.Parameters parameters = mCamera.getParameters();
    String mod = Build.MODEL;
    if (Build.VERSION.SDK_INT >= 8) {
      // MZ 180, other 90...
      if ("M9".equalsIgnoreCase(mod) || "MX".equalsIgnoreCase(mod)) {
        setDisplayOrientation(mCamera, 180);
      } else {
        setDisplayOrientation(mCamera, 90);
      }
    } else {
      parameters.set("orientation", "portrait");
      parameters.set("rotation", 90);
    }

    if (cameraCurrentlyLocked == CameraInfo.CAMERA_FACING_FRONT) {
      ismCameraCanFlash = false;
    } else {
      ismCameraCanFlash = true;
    }
    if (!ismCameraCanFlash) {
      mBtnFlash1.setVisibility(View.INVISIBLE);
      mBtnFlash2.setVisibility(View.INVISIBLE);
      mBtnFlash3.setVisibility(View.INVISIBLE);
    } else {
      mBtnFlash1.setVisibility(View.VISIBLE);
    }

    List<String> focusModes = parameters.getSupportedFocusModes();
    if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
      // Autofocus mode is supported
      parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    }

    if (parameters.isZoomSupported()) {
      parameters.setZoom(parameters.getZoom()); // 测试通过
    }
    Camera.Size previewSize = getFitParametersSize(parameters.getSupportedPreviewSizes());
    parameters.setPreviewSize(previewSize.width, previewSize.height);
    Camera.Size pictureSize = getFitParametersSize(parameters.getSupportedPictureSizes());
    parameters.setPictureSize(pictureSize.width, pictureSize.height);

    try {
      mCamera.setParameters(parameters);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public int setCameraProperty(String key, String value) {
    if (mCamera == null) {
      return MAAPI_consts.MA_CAMERA_RES_FAILED;
    }
    try {
      Camera.Parameters param = getCurrentParameters();
      if (key.equals(MAAPI_consts.MA_CAMERA_IMAGE_FORMAT)) {
        if (value.equals(MAAPI_consts.MA_CAMERA_IMAGE_RAW)) {
          rawMode = true;
        } else {
          // default mode is jpeg
          rawMode = false;
        }
      } else if (key.equals(MAAPI_consts.MA_CAMERA_FOCUS_MODE)) {
        if (value.equals(MAAPI_consts.MA_CAMERA_FOCUS_AUTO)) {
          if (false == param.getSupportedFocusModes().contains(value)) {

            return MAAPI_consts.MA_CAMERA_RES_VALUE_NOTSUPPORTED;
          }

          mCamera.autoFocus(autoFocusCallback);
        } else if (value.equals(MAAPI_consts.MA_CAMERA_FOCUS_MACRO)) {

          if (false == param.getSupportedFocusModes().contains(value)) {
            return MAAPI_consts.MA_CAMERA_RES_VALUE_NOTSUPPORTED;
          }
          mCamera.autoFocus(autoFocusCallback);
        } else if (false == param.getSupportedFocusModes().contains(value)) {
          mCamera.cancelAutoFocus();
          return MAAPI_consts.MA_CAMERA_RES_VALUE_NOTSUPPORTED;
        } else mCamera.cancelAutoFocus();

        param.setFocusMode(value);
      } else if (key.equals(MAAPI_consts.MA_CAMERA_FLASH_MODE)) {
        if (true == param.getSupportedFlashModes().contains(value)) {
          param.setFlashMode(value);
        } else {
          return MAAPI_consts.MA_CAMERA_RES_VALUE_NOTSUPPORTED;
        }
      } else {
        param.set(key, value);
      }
      mCamera.setParameters(param);
    } catch (Exception e) {
      return MAAPI_consts.MA_CAMERA_RES_FAILED;
    }
    return MAAPI_consts.MA_CAMERA_RES_OK;
  }
  // 设置相机的属性 抓取图像
  private void setCameraParameters() {
    Camera.Parameters caParameters = camera.getParameters();
    caParameters.setPictureFormat(ImageFormat.JPEG);
    caParameters.setPreviewSize(400, 500);
    caParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    camera.autoFocus(
        new Camera.AutoFocusCallback() {
          @Override
          public void onAutoFocus(boolean success, Camera camera) {

            if (success) {
              camera.takePicture(null, null, pictureCallback);
            }
          }
        });
  }
Exemple #23
0
  public void setCamera(Camera camera) {
    mCamera = camera;
    if (mCamera != null) {
      mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
      requestLayout();

      // get Camera parameters
      Camera.Parameters params = mCamera.getParameters();

      List<String> focusModes = params.getSupportedFocusModes();
      if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
        // set the focus mode
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        // set Camera parameters
        mCamera.setParameters(params);
      }
    }
  }
  public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();
    parameters = camera.getParameters();

    parameters.setPreviewSize(640, 480);
    parameters.setPreviewFrameRate(30);
    parameters.setJpegQuality(100);
    parameters.setFocusMode("continuous-video");
    parameters.setPictureSize(2048, 1536);
    camera.setParameters(parameters); // /!!!!!
    try {
      camera.setPreviewDisplay(holder);

    } catch (IOException exception) {
      camera.release();
      camera = null;
    }
  }
  void setDesiredCameraParameters(Camera camera) {
    Camera.Parameters parameters = camera.getParameters();

    if (parameters == null) {
      Log.w(
          TAG,
          "Device error: no camera parameters are available. Proceeding without configuration.");
      return;
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

    initializeTorch(parameters, prefs);
    String focusMode = null;
    if (prefs.getBoolean(PreferencesActivity.KEY_AUTO_FOCUS, true)) {
      if (prefs.getBoolean(PreferencesActivity.KEY_DISABLE_CONTINUOUS_FOCUS, false)) {
        focusMode =
            findSettableValue(
                parameters.getSupportedFocusModes(), Camera.Parameters.FOCUS_MODE_AUTO);
      } else {
        focusMode =
            findSettableValue(
                parameters.getSupportedFocusModes(),
                "continuous-picture", // Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE in 4.0+
                "continuous-video", // Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO in 4.0+
                Camera.Parameters.FOCUS_MODE_AUTO);
      }
    }
    // Maybe selected auto-focus but not available, so fall through here:
    if (focusMode == null) {
      focusMode =
          findSettableValue(
              parameters.getSupportedFocusModes(),
              Camera.Parameters.FOCUS_MODE_MACRO,
              "edof"); // Camera.Parameters.FOCUS_MODE_EDOF in 2.2+
    }
    if (focusMode != null) {
      parameters.setFocusMode(focusMode);
    }

    parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
    camera.setParameters(parameters);
  }
  public void takePhoto(View v) {
    Camera.Parameters p = mCamera.getParameters();
    p.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    mCamera.setParameters(p);

    mCamera.autoFocus(
        new Camera.AutoFocusCallback() {
          @Override
          public void onAutoFocus(boolean success, Camera camera) {
            if (success) {
              // captureButton.setEnabled(false);
              mCamera.takePicture(null, null, mPicture);
            } else {
              // captureButton.setEnabled(false);
              mCamera.takePicture(null, null, mPicture);
            }
          }
        });
  }
 public CameraPreview(ImageView CameraPreview) {
   if (mCamera == null) {
     mCamera = Camera.open();
   }
   Camera.Parameters parameters;
   parameters = mCamera.getParameters();
   List<String> af = parameters.getSupportedFocusModes();
   System.out.println(af);
   if (af.contains("continuous-video")) {
     parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
     System.out.println("FocusModeTrue-Auto");
     // TODO log this
   }
   List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
   Camera.Size cs = sizes.get(0);
   PreviewSizeWidth = cs.width;
   PreviewSizeHeight = cs.height;
   MyCameraPreview = CameraPreview;
   bitmap = Bitmap.createBitmap(PreviewSizeWidth, PreviewSizeHeight, Bitmap.Config.ARGB_8888);
   pixels = new int[PreviewSizeWidth * PreviewSizeHeight];
 }
Exemple #28
0
  @Override
  public void surfaceCreated(SurfaceHolder arg0) {
    camera = Camera.open();
    Camera.Parameters params = camera.getParameters();
    camera.setDisplayOrientation(90);
    params.setRotation(90);
    params.set("orientation", "landscape");
    params.set("rotation", 90);
    params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    Camera.Size previewSize = params.getPreviewSize();
    List<Camera.Size> previewSizes = params.getSupportedPreviewSizes();
    int i = 0;
    for (Camera.Size cs : previewSizes) {
      Log.d(MarvinActivity.TAG, "Camera - supports:(" + (i++) + ") " + cs.width + "x" + cs.height);
    }

    previewSize.width = 720;
    previewSize.height = 480;

    params.setSceneMode("portrait");
    Log.d(MarvinActivity.TAG, "w: " + previewSize.width + " h: " + previewSize.height);
    List<Integer> formats = params.getSupportedPreviewFormats();
    for (Integer format : formats) {
      Log.d(MarvinActivity.TAG, "Camera - supports preview format: " + format);
    }
    params.setPreviewSize(previewSize.width, previewSize.height);
    camera.setParameters(params);

    detectThread = new FaceDetectThread(context, user, params);
    detectThread.start();
    camera.setPreviewCallback(detectThread);
    try {
      camera.setPreviewDisplay(cameraView.getHolder());
      camera.startPreview();
      Log.d(MarvinActivity.TAG, "preview started");
    } catch (Exception e) {
      Log.e(MarvinActivity.TAG, e.getMessage(), e);
    }
    user.onEyeStarted();
  }
 public static void setFocus(
     Camera.Parameters parameters,
     boolean autoFocus,
     boolean disableContinuous,
     boolean safeMode) {
   List<String> supportedFocusModes = parameters.getSupportedFocusModes();
   String focusMode = null;
   if (autoFocus) {
     if (safeMode || disableContinuous) {
       focusMode =
           findSettableValue("focus mode", supportedFocusModes, Camera.Parameters.FOCUS_MODE_AUTO);
     } else {
       focusMode =
           findSettableValue(
               "focus mode",
               supportedFocusModes,
               Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE,
               Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO,
               Camera.Parameters.FOCUS_MODE_AUTO);
     }
   }
   // Maybe selected auto-focus but not available, so fall through here:
   if (!safeMode && focusMode == null) {
     focusMode =
         findSettableValue(
             "focus mode",
             supportedFocusModes,
             Camera.Parameters.FOCUS_MODE_MACRO,
             Camera.Parameters.FOCUS_MODE_EDOF);
   }
   if (focusMode != null) {
     if (focusMode.equals(parameters.getFocusMode())) {
       Log.i(TAG, "Focus mode already set to " + focusMode);
     } else {
       parameters.setFocusMode(focusMode);
     }
   }
 }
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Log.i("TAG", "surfaceChanged");
    // TODO Auto-generated method stub
    Camera.Parameters parameters = mCamera.getParameters();
    parameters.setFocusMode(Parameters.FOCUS_MODE_MACRO);
    parameters.setPictureFormat(PixelFormat.JPEG); // 设置图片格式
    //		Camera.Size picSize = parameters.getSupportedPictureSizes().get(0);
    //		parameters.setPictureSize(picSize.width, picSize.height);

    mCamera.setParameters(parameters);

    List<Camera.Size> sizes = parameters.getSupportedPictureSizes();
    mFrameWidth = width;
    mFrameHeight = height;

    // selecting optimal camera preview size
    {
      double minDiff = Double.MAX_VALUE;
      for (Camera.Size size : sizes) {
        if (Math.abs(size.height - height) < minDiff) {
          mFrameWidth = size.width;
          mFrameHeight = size.height;
          Log.d(
              "Size",
              "width = "
                  + Integer.toString(mFrameWidth)
                  + "height = "
                  + Integer.toString(mFrameHeight));
          minDiff = Math.abs(size.height - height);
        }
      }
    }

    parameters.setPreviewSize(getFrameWidth(), getFrameHeight());
    mCamera.setParameters(parameters);

    mCamera.startPreview(); // 开始预览
  }