/** Convenience method for {@link com.google.zxing.client.android.CaptureActivity} */
 public synchronized void setTorch(boolean newSetting) {
   if (newSetting != configManager.getTorchState(camera)) {
     if (camera != null) {
       if (autoFocusManager != null) {
         autoFocusManager.stop();
       }
       configManager.setTorch(camera, newSetting);
       if (autoFocusManager != null) {
         autoFocusManager.start();
       }
     }
   }
 }
 /**
  * Calculates the framing rect which the UI should draw to show the user where to place the
  * barcode. This target helps with alignment as well as forces the user to hold the device far
  * enough away to ensure the image will be in focus.
  *
  * @return The rectangle to draw on screen in window coordinates.
  */
 public synchronized Rect getFramingRect() {
   if (framingRect == null) {
     if (camera == null) {
       return null;
     }
     Point screenResolution = configManager.getScreenResolution();
     if (screenResolution == null) {
       // Called early, before init even finished
       return null;
     }
     int width = screenResolution.x * 3 / 4;
     if (width < MIN_FRAME_WIDTH) {
       width = MIN_FRAME_WIDTH;
     } else if (width > MAX_FRAME_WIDTH) {
       width = MAX_FRAME_WIDTH;
     }
     int height = screenResolution.y * 3 / 4;
     if (height < MIN_FRAME_HEIGHT) {
       height = MIN_FRAME_HEIGHT;
     } else if (height > MAX_FRAME_HEIGHT) {
       height = MAX_FRAME_HEIGHT;
     }
     int leftOffset = (screenResolution.x - width) / 2;
     int topOffset = (screenResolution.y - height) / 2;
     framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
     Log.d(TAG, "Calculated framing rect: " + framingRect);
   }
   return framingRect;
 }
  /**
   * Calculates the framing rect which the UI should draw to show the user where to place the
   * barcode. This target helps with alignment as well as forces the user to hold the device far
   * enough away to ensure the image will be in focus.
   *
   * @return The rectangle to draw on screen in window coordinates.
   */
  public synchronized Rect getFramingRect() {
    if (framingRect == null) {
      if (camera == null) {
        return null;
      }
      Point screenResolution = configManager.getScreenResolution();
      if (screenResolution == null) {
        // Called early, before init even finished
        return null;
      }

      int resolutionMin = Math.min(screenResolution.x, screenResolution.y);
      //      int width = findDesiredDimensionInRange(resolutionMin,MIN_FRAME_HEIGHT,
      // MAX_FRAME_HEIGHT);
      int width = PxUtil.dip2px(context, 240);
      int height = width;
      //      int width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH,
      // MAX_FRAME_WIDTH);
      //      int height = findDesiredDimensionInRange(screenResolution.y, MIN_FRAME_HEIGHT,
      // MAX_FRAME_HEIGHT);

      int leftOffset = (screenResolution.x - width) / 2;
      //      int topOffset = (screenResolution.y - height) / 4;
      int topOffset = PxUtil.dip2px(context, 80);
      framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
      Log.d(TAG, "Calculated framing rect: " + framingRect);
    }
    return framingRect;
  }
  /**
   * Opens the camera driver and initializes the hardware parameters.
   *
   * @param holder The surface object which the camera will draw preview frames into.
   * @throws java.io.IOException Indicates the camera driver failed to open.
   */
  public synchronized void openDriver(SurfaceHolder holder) throws IOException {
    Camera theCamera = camera;
    if (theCamera == null) {
      theCamera = new OpenCameraManager().build().open();
      if (theCamera == null) {
        throw new IOException();
      }
      camera = theCamera;
    }
    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");
        }
      }
    }
  }
 /**
  * Like {@link #getFramingRect} but coordinates are in terms of the preview frame, not UI /
  * screen.
  */
 public synchronized Rect getFramingRectInPreview() {
   if (framingRectInPreview == null) {
     Rect framingRect = getFramingRect();
     if (framingRect == null) {
       return null;
     }
     Rect rect = new Rect(framingRect);
     Point cameraResolution = configManager.getCameraResolution();
     Point screenResolution = configManager.getScreenResolution();
     if (cameraResolution == null || screenResolution == null) {
       // Called early, before init even finished
       return null;
     }
     rect.left = rect.left * cameraResolution.x / screenResolution.x;
     rect.right = rect.right * cameraResolution.x / screenResolution.x;
     rect.top = rect.top * cameraResolution.y / screenResolution.y;
     rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;
     framingRectInPreview = rect;
   }
   return framingRectInPreview;
 }
 /**
  * Allows third party apps to specify the scanning rectangle dimensions, rather than determine
  * them automatically based on screen resolution.
  *
  * @param width The width in pixels to scan.
  * @param height The height in pixels to scan.
  */
 public synchronized void setManualFramingRect(int width, int height) {
   if (initialized) {
     Point screenResolution = configManager.getScreenResolution();
     if (width > screenResolution.x) {
       width = screenResolution.x;
     }
     if (height > screenResolution.y) {
       height = screenResolution.y;
     }
     int leftOffset = (screenResolution.x - width) / 2;
     int topOffset = (screenResolution.y - height) / 2;
     framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
     Log.d(TAG, "Calculated manual framing rect: " + framingRect);
     framingRectInPreview = null;
   } else {
     requestedFramingRectWidth = width;
     requestedFramingRectHeight = height;
   }
 }