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);
  }
  void setDesiredCameraParameters(OpenCamera camera, boolean safeMode) {

    Camera theCamera = camera.getCamera();
    Camera.Parameters parameters = theCamera.getParameters();

    //    theCamera.setDisplayOrientation(90);//add 这句代码作用是旋转镜头90度,使相机预览方向正确显示

    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);

    CameraConfigurationUtils.setFocus(parameters, true, true, safeMode);

    if (!safeMode) {
      CameraConfigurationUtils.setBarcodeSceneMode(parameters);
      CameraConfigurationUtils.setVideoStabilization(parameters);
      CameraConfigurationUtils.setFocusArea(parameters);
      CameraConfigurationUtils.setMetering(parameters);
    }

    parameters.setPreviewSize(bestPreviewSize.x, bestPreviewSize.y);

    theCamera.setParameters(parameters);

    theCamera.setDisplayOrientation(cwRotationFromDisplayToCamera);

    Camera.Parameters afterParameters = theCamera.getParameters();
    Camera.Size afterSize = afterParameters.getPreviewSize();
    if (afterSize != null
        && (bestPreviewSize.x != afterSize.width || bestPreviewSize.y != afterSize.height)) {
      Log.w(
          TAG,
          "Camera said it supported preview size "
              + bestPreviewSize.x
              + 'x'
              + bestPreviewSize.y
              + ", but after setting it, preview size is "
              + afterSize.width
              + 'x'
              + afterSize.height);
      bestPreviewSize.x = afterSize.width;
      bestPreviewSize.y = afterSize.height;
    }
  }
 /**
  * Sets the camera up to take preview images which are used for both preview and decoding. We
  * detect the preview format here so that buildLuminanceSource() can build an appropriate
  * LuminanceSource subclass. In the future we may want to force YUV420SP as it's the smallest, and
  * the planar Y can be used for barcode scanning without a copy in some cases.
  */
 void setDesiredCameraParameters(Camera camera) {
   Camera.Parameters parameters = camera.getParameters();
   Log.d(TAG, "Setting preview size: " + cameraResolution);
   parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
   // setFlash(parameters);
   // setZoom(parameters);
   parameters.set("zoom", "0");
   // setSharpness(parameters);
   Log.d(TAG, "Camera parameters flat: " + parameters.flatten());
   camera.setParameters(parameters);
 }
Exemple #4
0
  /*
   * Code to run when the op mode is first enabled goes here
   * @see com.qualcomm.robotcore.eventloop.opmode.OpMode#start()
   */
  @Override
  public void init() {
    camera = ((FtcRobotControllerActivity) hardwareMap.appContext).camera;
    camera.setPreviewCallback(previewCallback);

    Camera.Parameters parameters = camera.getParameters();
    data = parameters.flatten();

    ((FtcRobotControllerActivity) hardwareMap.appContext)
        .initPreview(camera, this, previewCallback);
  }
  public void init() {
    camera = ((FtcRobotControllerActivity) context).camera;

    camera.setPreviewCallback(previewCallback);

    Camera.Parameters parameters = camera.getParameters();

    data = parameters.flatten();

    ((FtcRobotControllerActivity) context).initPreviewLinear(camera, this, previewCallback);
  }
  public void startCamera() {

    camera = openCamera(Camera.CameraInfo.CAMERA_FACING_FRONT);
    camera.setPreviewCallback(previewCallback);

    Camera.Parameters parameters = camera.getParameters();

    width = parameters.getPreviewSize().width / ds;
    height = parameters.getPreviewSize().height / ds;
    parameters.setPreviewSize(width, height);

    camera.setParameters(parameters);

    data = parameters.flatten();
  }
Exemple #7
0
  /**
   * Opens the camera driver and initializes the hardware parameters.
   *
   * @param holder The surface object which the camera will draw preview frames into.
   * @throws IOException Indicates the camera driver failed to open.
   */
  public synchronized void openDriver(SurfaceHolder holder) throws IOException {
    Camera theCamera = camera;
    if (theCamera == null) {
      // 获取手机背面的摄像头
      theCamera = OpenCameraInterface.open();
      if (theCamera == null) {
        throw new IOException();
      }
      camera = theCamera;
    }

    // 设置摄像头预览view
    theCamera.setPreviewDisplay(holder);

    if (!initialized) {
      initialized = true;
      configManager.initFromCameraParameters(theCamera);
      if (requestedFramingRectWidth > 0 && requestedFramingRectHeight > 0) {
        setManualFramingRect(requestedFramingRectWidth, requestedFramingRectHeight);
        requestedFramingRectWidth = 0;
        requestedFramingRectHeight = 0;
      }
    }

    Camera.Parameters parameters = theCamera.getParameters();
    String parametersFlattened = parameters == null ? null : parameters.flatten(); // Save
    // these,
    // temporarily
    try {
      configManager.setDesiredCameraParameters(theCamera, false);
    } catch (RuntimeException re) {
      // Driver failed
      Log.w(TAG, "Camera rejected parameters. Setting only minimal safe-mode parameters");
      Log.i(TAG, "Resetting to saved camera params: " + parametersFlattened);
      // Reset:
      if (parametersFlattened != null) {
        parameters = theCamera.getParameters();
        parameters.unflatten(parametersFlattened);
        try {
          theCamera.setParameters(parameters);
          configManager.setDesiredCameraParameters(theCamera, true);
        } catch (RuntimeException re2) {
          // Well, darn. Give up
          Log.w(TAG, "Camera rejected even safe-mode parameters! No configuration");
        }
      }
    }
  }
  public void get_all_camera_param() {
    String all = parameters.flatten();
    String[] all_s = all.split(";");
    String all_line = "";

    for (String i : all_s) {
      all_line = all_line + "\n" + i;
    }
    try {
      BufferedWriter out =
          new BufferedWriter(new FileWriter("/sdcard/ez_Lua_Script_Camera/camera_parameters.txt"));
      out.write(res.getString(R.string.creator) + "\n");
      out.write(all_line);
      out.close();
    } catch (IOException e) {
    }
  }
  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;
    }

    initializeTorch(parameters, safeMode);

    CameraConfigurationUtils.setFocus(parameters, true, true, safeMode);

    if (!safeMode) {
      CameraConfigurationUtils.setBarcodeSceneMode(parameters);
      CameraConfigurationUtils.setVideoStabilization(parameters);
      CameraConfigurationUtils.setFocusArea(parameters);
      CameraConfigurationUtils.setMetering(parameters);
    }

    parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
    Log.i(TAG, "Final camera parameters: " + parameters.flatten());

    camera.setParameters(parameters);
    Camera.Parameters afterParameters = camera.getParameters();
    Camera.Size afterSize = afterParameters.getPreviewSize();
    if (afterSize != null
        && (cameraResolution.x != afterSize.width || cameraResolution.y != afterSize.height)) {
      Log.w(
          TAG,
          "Camera said it supported preview size "
              + cameraResolution.x
              + 'x'
              + cameraResolution.y
              + ", but after setting it, preview size is "
              + afterSize.width
              + 'x'
              + afterSize.height);
      cameraResolution.x = afterSize.width;
      cameraResolution.y = afterSize.height;
    }

    camera.setDisplayOrientation(90);
  }
  /**
   * Opens the camera driver and initializes the hardware parameters.
   *
   * @param holder The surface object which the camera will draw preview frames into.
   * @throws IOException Indicates the camera driver failed to open.
   */
  public synchronized void openDriver() throws IOException {
    Camera theCamera = camera;
    if (theCamera == null) {
      theCamera = Camera.open();
      if (theCamera == null) {
        throw new IOException();
      }
      camera = theCamera;
    }

    if (!initialized) {
      initialized = true;
      configManager.initFromCameraParameters(theCamera);
    }

    Camera.Parameters parameters = theCamera.getParameters();
    String parametersFlattened = parameters == null ? null : parameters.flatten(); // Save
    // these,
    // temporarily
    try {
      configManager.setDesiredCameraParameters(theCamera, false);
    } catch (RuntimeException re) {
      // Driver failed
      Log.w(TAG, "Camera rejected parameters. Setting only minimal safe-mode parameters");
      Log.i(TAG, "Resetting to saved camera params: " + parametersFlattened);
      // Reset:
      if (parametersFlattened != null) {
        parameters = theCamera.getParameters();
        parameters.unflatten(parametersFlattened);
        try {
          theCamera.setParameters(parameters);
          configManager.setDesiredCameraParameters(theCamera, true);
        } catch (RuntimeException re2) {
          // Well, darn. Give up
          Log.w(TAG, "Camera rejected even safe-mode parameters! No configuration");
        }
      }
    }
  }
 public static String collectStats(Camera.Parameters parameters) {
   return collectStats(parameters.flatten());
 }
  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(),
                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(
              parameters.getSupportedFocusModes(),
              Camera.Parameters.FOCUS_MODE_MACRO,
              Camera.Parameters.FOCUS_MODE_EDOF);
    }
    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);

    Camera.Parameters afterParameters = camera.getParameters();
    Camera.Size afterSize = afterParameters.getPreviewSize();
    if (afterSize != null
        && (cameraResolution.x != afterSize.width || cameraResolution.y != afterSize.height)) {
      Log.w(
          TAG,
          "Camera said it supported preview size "
              + cameraResolution.x
              + 'x'
              + cameraResolution.y
              + ", but after setting it, preview size is "
              + afterSize.width
              + 'x'
              + afterSize.height);
      cameraResolution.x = afterSize.width;
      cameraResolution.y = afterSize.height;
    }
  }
  public Camera open(final SurfaceHolder holder, final boolean continuousAutoFocus)
      throws IOException {
    // try back-facing camera
    camera = Camera.open();

    // fall back to using front-facing camera
    if (camera == null) {
      final int cameraCount = Camera.getNumberOfCameras();
      final CameraInfo cameraInfo = new CameraInfo();

      // search for front-facing camera
      for (int i = 0; i < cameraCount; i++) {
        Camera.getCameraInfo(i, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
          camera = Camera.open(i);
          break;
        }
      }
    }

    camera.setPreviewDisplay(holder);

    final Camera.Parameters parameters = camera.getParameters();

    final Rect surfaceFrame = holder.getSurfaceFrame();
    cameraResolution = findBestPreviewSizeValue(parameters, surfaceFrame);

    final int surfaceWidth = surfaceFrame.width();
    final int surfaceHeight = surfaceFrame.height();

    final int rawSize = Math.min(surfaceWidth * 2 / 3, surfaceHeight * 2 / 3);
    final int frameSize = Math.max(MIN_FRAME_SIZE, Math.min(MAX_FRAME_SIZE, rawSize));

    final int leftOffset = (surfaceWidth - frameSize) / 2;
    final int topOffset = (surfaceHeight - frameSize) / 2;
    frame = new Rect(leftOffset, topOffset, leftOffset + frameSize, topOffset + frameSize);
    framePreview =
        new Rect(
            frame.left * cameraResolution.width / surfaceWidth,
            frame.top * cameraResolution.height / surfaceHeight,
            frame.right * cameraResolution.width / surfaceWidth,
            frame.bottom * cameraResolution.height / surfaceHeight);

    final String savedParameters = parameters == null ? null : parameters.flatten();

    try {
      setDesiredCameraParameters(camera, cameraResolution, continuousAutoFocus);
    } catch (final RuntimeException x) {
      if (savedParameters != null) {
        final Camera.Parameters parameters2 = camera.getParameters();
        parameters2.unflatten(savedParameters);
        try {
          camera.setParameters(parameters2);
          setDesiredCameraParameters(camera, cameraResolution, continuousAutoFocus);
        } catch (final RuntimeException x2) {
          log.info("problem setting camera parameters", x2);
        }
      }
    }

    camera.startPreview();

    return camera;
  }
  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);

    CameraConfigurationUtils.setFocus(
        parameters,
        prefs.getBoolean(PreferencesActivity.KEY_AUTO_FOCUS, true),
        prefs.getBoolean(PreferencesActivity.KEY_DISABLE_CONTINUOUS_FOCUS, true),
        safeMode);

    if (!safeMode) {
      if (prefs.getBoolean(PreferencesActivity.KEY_INVERT_SCAN, false)) {
        CameraConfigurationUtils.setInvertColor(parameters);
      }

      if (!prefs.getBoolean(PreferencesActivity.KEY_DISABLE_BARCODE_SCENE_MODE, true)) {
        CameraConfigurationUtils.setBarcodeSceneMode(parameters);
      }

      if (!prefs.getBoolean(PreferencesActivity.KEY_DISABLE_METERING, true)) {
        CameraConfigurationUtils.setVideoStabilization(parameters);
        CameraConfigurationUtils.setFocusArea(parameters);
        CameraConfigurationUtils.setMetering(parameters);
      }
    }

    parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);

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

    camera.setParameters(parameters);

    Camera.Parameters afterParameters = camera.getParameters();
    Camera.Size afterSize = afterParameters.getPreviewSize();
    if (afterSize != null
        && (cameraResolution.x != afterSize.width || cameraResolution.y != afterSize.height)) {
      Log.w(
          TAG,
          "Camera said it supported preview size "
              + cameraResolution.x
              + 'x'
              + cameraResolution.y
              + ", but after setting it, preview size is "
              + afterSize.width
              + 'x'
              + afterSize.height);
      cameraResolution.x = afterSize.width;
      cameraResolution.y = afterSize.height;
    }
  }