title: How to set size of camera preview in android
body: <p>I am trying to create a custom camera in android. I want to set the exact size of the picture to be taken. I want that exact size to be what the user sees in the preview. How do I set that exact size? (By size I mean width and height: dimension)</p>
  
  <p>I find the following verbiage in the <a href="http://developer.android.com/guide/topics/media/camera.html" rel="nofollow">android literatures</a>:</p>
  
  <blockquote>
    <p>If you want to set a specific size for your camera preview, set this
    in the surfaceChanged() method as noted in the comments above. When
    setting preview size, you must use values from
    getSupportedPreviewSizes(). Do not set arbitrary values in the
    setPreviewSize() method.</p>
  </blockquote>
  
  <p>Does this paragraph specify what I need to do or does it pertain to something else? In any case, does anyone have any code snippet for how I would go about setting the size? say I want a square image, how would I specify it so that both the preview and the final image are both squares?</p>
  
  <p>Right now my <code>surfaceChanged</code> method is as follows</p>
  
  <pre><code>@Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
      if (mCamera != null) {
          try {
              mCamera.setPreviewDisplay(holder);
              if (mIsCapturing) {
                  mCamera.startPreview();
              }
          } catch (IOException e) {
              Toast.makeText(CameraActivity.this, "Unable to start camera preview.", Toast.LENGTH_LONG).show();
          }
      }
  }
  </code></pre>
  
  <p>Thanks for helping.</p>
  
  <p>Also, in case it's important, my layout is</p>
  
  <pre><code>&lt;FrameLayout
      android:id="@+id/camera_frame"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1" &gt;
  
      &lt;ImageView
          android:id="@+id/camera_image_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  
      &lt;SurfaceView
          android:id="@+id/preview_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  &lt;/FrameLayout&gt;
  </code></pre>