title: How to adjust camera (auto?) focus ?
body: <p>I'm building a barcode scanner with new Google's API (<a href="https://developers.google.com/android/reference/com/google/android/gms/vision/barcode/BarcodeDetector" rel="nofollow">https://developers.google.com/android/reference/com/google/android/gms/vision/barcode/BarcodeDetector</a>). I know nothing about camera, it's my first time working on this stuff.. I already can open the app and scan big codes, but the smaller ones, i can't, because the camera doesn't adjust it's focus. Stays the same, ever.</p>
  
  <p><strong>TLDL</strong>: how to enable CameraSource's (com.google.android.gms.vision) instance autofocus ?</p>
  
  <p>My layout:</p>
  
  <pre><code>&lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/topLayout"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:keepScreenOn="true"&gt;
  
  &lt;com.example.victormilazzo.barcodedetector.camera.CameraSourcePreview
      android:id="@+id/preview"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;com.example.victormilazzo.barcodedetector.camera.GraphicOverlay
          android:id="@+id/faceOverlay"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  
  &lt;/com.example.victormilazzo.barcodedetector.camera.CameraSourcePreview&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Where GraphicOverlay and CameraSourcePreview are custom classes.</p>
  
  <p>Main activity:</p>
  
  <pre><code>...
  private CameraSource mCameraSource = null;
  private CameraSourcePreview mPreview;
  private GraphicOverlay mGraphicOverlay;
  
  /**
   * Initializes the UI and creates the detector pipeline.
   */
  @Override
  public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.activity_camera);
  
      mPreview = (CameraSourcePreview) findViewById(R.id.preview);
      mGraphicOverlay = (GraphicOverlay) findViewById(R.id.faceOverlay);
  
      // Check for the camera permission before accessing the camera.  If the
      // permission is not granted yet, request permission.
      int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
      if (rc == PackageManager.PERMISSION_GRANTED) {
          createCameraSource();
      } else {
          requestCameraPermission();
      }
  }
  
  ...
  
  /**
   * Creates and starts the camera.  Note that this uses a higher resolution in comparison
   * to other detection examples to enable the barcode detector to detect small barcodes
   * at long distances.
   */
  private void createCameraSource() {
  
  
      Context context = getApplicationContext();
  
      // A barcode detector is created to track barcodes.  An associated multi-processor instance
      // is set to receive the barcode detection results, track the barcodes, and maintain
      // graphics for each barcode on screen.  The factory is used by the multi-processor to
      // create a separate tracker instance for each barcode.
      BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
      BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay);
      barcodeDetector.setProcessor(
              new MultiProcessor.Builder&lt;&gt;(barcodeFactory).build());
  
      if (!barcodeDetector.isOperational()) {
          // Note: The first time that an app using the barcode or face API is installed on a
          // device, GMS will download a native libraries to the device in order to do detection.
          // Usually this completes before the app is run for the first time.  But if that
          // download has not yet completed, then the above call will not detect any barcodes
          // and/or faces.
          //
          // isOperational() can be used to check if the required native libraries are currently
          // available.  The detectors will automatically become operational once the library
          // downloads complete on device.
          Log.w(TAG, "Detector dependencies are not yet available.");
      }
  
      // Creates and starts the camera.  Note that this uses a higher resolution in comparison
      // to other detection examples to enable the barcode detector to detect small barcodes
      // at long distances.
      mCameraSource = new CameraSource.Builder(getApplicationContext(), barcodeDetector)
              .setFacing(CameraSource.CAMERA_FACING_BACK)
              .setRequestedPreviewSize(1600, 1024)
              .setRequestedFps(15.0f)
              .build();
  }
  
  ...
  
  /**
   * Starts or restarts the camera source, if it exists.  If the camera source doesn't exist yet
   * (e.g., because onResume was called before the camera source was created), this will be called
   * again when the camera source is created.
   */
  private void startCameraSource() {
  
      // check that the device has play services available.
      int code = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(
              getApplicationContext());
      if (code != ConnectionResult.SUCCESS) {
          Dialog dlg =
                  GoogleApiAvailability.getInstance().getErrorDialog(this, code, RC_HANDLE_GMS);
          dlg.show();
      }
  
      if (mCameraSource != null) {
          try {
              mPreview.start(mCameraSource, mGraphicOverlay);
          } catch (IOException e) {
              Log.e(TAG, "Unable to start camera source.", e);
              mCameraSource.release();
              mCameraSource = null;
          }
      }
  }
  </code></pre>
  
  <p>I'm posting only the relevants methods. There's others like pauseCamera, etc..</p>
  
  <p>Thanks !</p>