Beispiel #1
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);
  }
 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);
     }
   }
 }
Beispiel #3
0
  /**
   * Opens the camera and applies the user settings.
   *
   * @throws RuntimeException if the method fails
   */
  @SuppressLint("InlinedApi")
  private Camera createCamera() {
    int requestedCameraId = getIdForRequestedCamera(mFacing);
    if (requestedCameraId == -1) {
      throw new RuntimeException(mContext.getString(R.string.no_requested_camera));
    }
    Camera camera = Camera.open(requestedCameraId);

    SizePair sizePair =
        selectSizePair(camera, mRequestedPreviewWidth, mRequestedPreviewHeight, mContext);
    if (sizePair == null) {
      throw new RuntimeException(mContext.getString(R.string.no_suitable_preview_size));
    }
    Size pictureSize = sizePair.pictureSize();
    mPreviewSize = sizePair.previewSize();

    int[] previewFpsRange = selectPreviewFpsRange(camera, mRequestedFps);
    if (previewFpsRange == null) {
      throw new RuntimeException(mContext.getString(R.string.no_suitable_frames_per_second));
    }

    Camera.Parameters parameters = camera.getParameters();

    parameters.setPictureSize(pictureSize.getWidth(), pictureSize.getHeight());
    parameters.setPreviewSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
    parameters.setPreviewFpsRange(
        previewFpsRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX],
        previewFpsRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]);
    parameters.setPreviewFormat(ImageFormat.NV21);

    setRotation(camera, parameters, requestedCameraId);

    if (mFocusMode != null) {
      if (parameters.getSupportedFocusModes().contains(mFocusMode)) {
        parameters.setFocusMode(mFocusMode);
      } else {
        Log.i(
            TAG,
            mContext.getString(R.string.camera_focus_mode)
                + mFocusMode
                + mContext.getString(R.string.not_supported));
      }
    }

    // setting mFocusMode to the one set in the params
    mFocusMode = parameters.getFocusMode();

    if (mFlashMode != null) {
      if (parameters.getSupportedFlashModes().contains(mFlashMode)) {
        parameters.setFlashMode(mFlashMode);
      } else {
        Log.i(
            TAG,
            mContext.getString(R.string.flash_mode)
                + mFlashMode
                + mContext.getString(R.string.not_supported));
      }
    }

    // setting mFlashMode to the one set in the params
    mFlashMode = parameters.getFlashMode();

    camera.setParameters(parameters);

    // Four frame buffers are needed for working with the camera:
    //
    //   one for the frame that is currently being executed upon in doing detection
    //   one for the next pending frame to process immediately upon completing detection
    //   two for the frames that the camera uses to populate future preview images
    camera.setPreviewCallbackWithBuffer(new CameraPreviewCallback());
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));

    return camera;
  }