title: Android AutoFocus and Image preview size
body: <p>I am using these two sources to make a QR code scanner</p>
  
  <p><a href="https://code.tutsplus.com/tutorials/reading-qr-codes-using-the-mobile-vision-api--cms-24680" rel="nofollow noreferrer">QR scanner android app</a></p>
  
  <p><a href="https://gist.github.com/Gericop/7de0b9fdd7a444e53b5a" rel="nofollow noreferrer">Fix the autofocus issue</a></p>
  
  <p>I tried to fix the auto focus problem, but I am getting an error that says <em>Cannot resolve symbol 'Field'</em>
  Also, I tried to work around the dimensions of the camera preview using the setRequestedPreviewSize method, but cannot find a solution. </p>
  
  <blockquote>
    <p>MainActivity.java</p>
  </blockquote>
  
  <pre><code>import android.app.Activity;
  import android.content.pm.PackageManager;
  import android.graphics.ImageFormat;
  import android.hardware.Camera;
  import android.os.Bundle;
  import android.util.Log;
  import android.util.SparseArray;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  import android.widget.TextView;
  
  import com.google.android.gms.common.api.GoogleApiClient;
  import com.google.android.gms.vision.CameraSource;
  import com.google.android.gms.vision.Detector;
  import com.google.android.gms.vision.barcode.Barcode;
  import com.google.android.gms.vision.barcode.BarcodeDetector;
  
  import java.io.IOException;
  import java.util.List;
  
  public class MainActivity extends Activity {
  
  private SurfaceView cameraView;
  private TextView barcodeInfo;
  private CameraSource cameraSource;
  private SurfaceHolder surfaceHolder;
  private GoogleApiClient client;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      cameraView = (SurfaceView) findViewById(R.id.cameraView);
      barcodeInfo = (TextView) findViewById(R.id.infoTextView);
  
      BarcodeDetector detector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
      cameraSource = new CameraSource.Builder(this, detector).setRequestedPreviewSize(640, 480).build();
  
      cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
          @Override
          public void surfaceCreated(SurfaceHolder holder) {
              try {
                  cameraSource.start(cameraView.getHolder());
              } catch (IOException e) {
                  Log.e("CAMERA SOURCE", e.getMessage());
              }
          }
  
          @Override
          public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  
          }
  
          @Override
          public void surfaceDestroyed(SurfaceHolder holder) {
              cameraSource.stop();
          }
      });
  
      detector.setProcessor(new Detector.Processor&lt;Barcode&gt;() {
          @Override
          public void release() {
  
          }
  
          @Override
          public void receiveDetections(Detector.Detections&lt;Barcode&gt; detections) {
              final SparseArray&lt;Barcode&gt; barcodes = detections.getDetectedItems();
              if (barcodes.size() != 0) {
                  barcodeInfo.post(new Runnable() {
                      @Override
                      public void run() {
                          barcodeInfo.setText(barcodes.valueAt(0).displayValue);
                      }
                  });
              }
          }
      });
  
  public void previewCamera() {
  
      try {
          cameraSource.setPreviewDisplay(surfaceHolder);
  
      } catch (IOException e) {
          e.printStackTrace();
      }
  
  
      CameraSource.Parameters param = cameraSource.getParameters();
      List&lt;CameraSource.Size&gt; sizeList = cameraSource.getParameters().getSupportedPreviewSizes();
      CameraSource.Size bestSize = sizeList.get(0);
      for (int i = 1; i &lt; sizeList.size(); i++) {
          if ((sizeList.get(i).width * sizeList.get(i).height) &gt; (bestSize.width * bestSize.height)) {
              bestSize = sizeList.get(i);
          }
      }
  
      List&lt;Integer&gt; supportedPreviewFormats = param.getSupportedPreviewFormats();
      for (Integer previewFormat : supportedPreviewFormats) {
          if (previewFormat == ImageFormat.YV12) {
              param.setPreviewFormat(previewFormat);
          }
      }
  
      PackageManager packageManager = getPackageManager();
  
      if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA) &amp;&amp; packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS)) {
  
          param.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
      }
  
      param.setPictureSize(bestSize.width, bestSize.height);
      param.setPreviewSize(bestSize.width, bestSize.height);
  
      cameraSource.setParameters(param);
      cameraSource.startPreview();
      previewing = true;
  
  }
  }
  }
  </code></pre>
  
  <blockquote>
    <p>activity_main.xml</p>
  </blockquote>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.android.diverseycare.MainActivity"
  android:background="#ffffff"
  android:padding="16dp"&gt;
  
  &lt;SurfaceView
      android:id="@+id/cameraView"
      android:layout_width="640px"
      android:layout_height="480px"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="67dp" /&gt;
  
  &lt;TextView
      android:id="@+id/infoTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="16dp"
      android:text="Nothing to read"
      android:textSize="20sp"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true" /&gt;
  
   &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>I tried to combine the code to using the code pointed out in the comments, but am I missing anything?Any pointers to a good resource to these issues are highly appreciated. (This is my first attempt at anbdroid, so please ingore if this is a silly mistake)</p>