title: How to solve getting auto cropped image problem in Android with a custom camera?
body: <p>I'm working on custom camera in Android. I used android.hardware.camera, when I capture image like this:</p>
  
  <p><img src="https://i.stack.imgur.com/5CA6z.jpg" alt="enter image description here"></p>
  
  <p>But when I display on imageview getting auto cropped image like:</p>
  
  <p><a href="https://i.stack.imgur.com/VLhJh.jpg" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>And my code is:</p>
  
  <pre><code>import android.content.res.Resources;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Matrix;
  import android.graphics.Paint;
  import android.graphics.Rect;
  import android.graphics.drawable.BitmapDrawable;
  import android.hardware.Camera;
  import android.util.DisplayMetrics;
  import android.util.Log;
  import android.view.SurfaceHolder;
  import android.widget.Toast;
  import android.app.Activity;
  import android.content.Intent;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.hardware.Camera;
  import android.os.Environment;
  import android.provider.MediaStore;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  import android.view.View;
  import android.widget.Button;
  import android.widget.ImageView;
  import android.widget.Toast;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.math.BigInteger;
  
  class CameraActivity extends Activity implements Camera.PictureCallback, SurfaceHolder.Callback {
  
      public static final String EXTRA_CAMERA_DATA = "camera_data";
  
      private static final String KEY_IS_CAPTURING = "is_capturing";
  
      private Camera mCamera;
      private ImageView mCameraImage;
      private SurfaceView mCameraPreview;
      private Button mCaptureImageButton;
      private byte[] mCameraData;
      private boolean mIsCapturing;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_camera);
  
          DisplayMetrics displayMetrics = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
          int height = displayMetrics.heightPixels;
          int width = displayMetrics.widthPixels;
          Log.d("tag", "height oncreaate============= " + height + "width " + width);
  
  
          mCameraImage = (ImageView) findViewById(R.id.camera_image_view);
          mCameraImage.setVisibility(View.INVISIBLE);
  
          mCameraPreview = (SurfaceView) findViewById(R.id.preview_view);
          final SurfaceHolder surfaceHolder = mCameraPreview.getHolder();
          surfaceHolder.addCallback(this);
          surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  
          mCaptureImageButton = (Button) findViewById(R.id.capture_image_button);
          mCaptureImageButton.setOnClickListener(mCaptureImageButtonClickListener);
  
          final Button doneButton = (Button) findViewById(R.id.done_button);
          doneButton.setOnClickListener(mDoneButtonClickListener);
  
          mIsCapturing = true;
      }
  
      @Override
      protected void onSaveInstanceState(Bundle savedInstanceState) {
          super.onSaveInstanceState(savedInstanceState);
  
          savedInstanceState.putBoolean(KEY_IS_CAPTURING, mIsCapturing);
      }
  
      @Override
      protected void onRestoreInstanceState(Bundle savedInstanceState) {
          super.onRestoreInstanceState(savedInstanceState);
  
          mIsCapturing = savedInstanceState.getBoolean(KEY_IS_CAPTURING, mCameraData == null);
          if (mCameraData != null) {
              setupImageDisplay();
          } else {
              setupImageCapture();
          }
      }
  
      @Override
      protected void onResume() {
          super.onResume();
  
          if (mCamera == null) {
              try {
                  mCamera = Camera.open();
                  mCamera.setPreviewDisplay(mCameraPreview.getHolder());
                  if (mIsCapturing) {
                      mCamera.startPreview();
                  }
              } catch (Exception e) {
                  Toast.makeText(CameraActivity.this, "Unable to open camera.", Toast.LENGTH_LONG)
                          .show();
              }
          }
      }
  
      private View.OnClickListener mCaptureImageButtonClickListener = new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              captureImage();
          }
      };
  
      private View.OnClickListener mRecaptureImageButtonClickListener = new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              setupImageCapture();
          }
      };
  
      private View.OnClickListener mDoneButtonClickListener = new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (mCameraData != null) {
                  Intent intent = new Intent();
                  intent.putExtra(EXTRA_CAMERA_DATA, mCameraData);
                  setResult(RESULT_OK, intent);
              } else {
                  setResult(RESULT_CANCELED);
              }
              finish();
          }
      };
  
      @Override
      protected void onPause() {
          super.onPause();
  
          if (mCamera != null) {
  
              mCamera.release();
              mCamera = null;
          }
      }
  
      @Override
      public void onPictureTaken(byte[] data, Camera camera) {
  
  
          mCameraData = data;
          setupImageDisplay();
      }
  
      @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(this, "Unable to start camera preview.", Toast.LENGTH_LONG).show();
              }
          }
      }
  
      @Override
      public void surfaceCreated(SurfaceHolder holder) {
      }
  
      @Override
      public void surfaceDestroyed(SurfaceHolder holder) {
      }
  
      private void captureImage() {
          mCamera.takePicture(null, null, this);
      }
  
      private void setupImageCapture() {
          mCameraImage.setVisibility(View.INVISIBLE);
          mCameraPreview.setVisibility(View.VISIBLE);
          mCamera.startPreview();
          mCaptureImageButton.setText(R.string.capture_image);
          mCaptureImageButton.setOnClickListener(mCaptureImageButtonClickListener);
      }
  
      private void setupImageDisplay() {
          DisplayMetrics displayMetrics = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
          int height = displayMetrics.heightPixels;
          int width = displayMetrics.widthPixels;
          Log.d("tag", "height " + height + "width " + width);
          BitmapFactory.Options scalingOptions = new BitmapFactory.Options();
          scalingOptions.inSampleSize = 5;
          final Bitmap bmp = BitmapFactory.decodeByteArray(mCameraData, 0, mCameraData.length, scalingOptions);
  
          mCameraImage.setImageBitmap(bmp);
  
          mCamera.stopPreview();
          mCameraPreview.setVisibility(View.INVISIBLE);
          mCameraImage.setVisibility(View.VISIBLE);
          mCaptureImageButton.setText(R.string.recapture_image);
          mCaptureImageButton.setOnClickListener(mRecaptureImageButtonClickListener);
      }
  
  }
  </code></pre>
  
  <p>and xml </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#999999"
      android:orientation="vertical" &gt;
  
      &lt;TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="@string/capturing_image" /&gt;
  
      &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;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dp"
          android:gravity="center"
          android:orientation="horizontal" &gt;
  
          &lt;Button
              android:id="@+id/capture_image_button"
              android:layout_width="150dp"
              android:layout_height="wrap_content"
              android:text="@string/capture_image" /&gt;
  
          &lt;Button
              android:id="@+id/done_button"
              android:layout_width="150dp"
              android:layout_height="wrap_content"
              android:text="@string/done" /&gt;
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>im also try </p>
  
  <pre><code>Camera.Parameters parameters = prCamera.getParameters();
          Camera.Size bestSize = null;
          prSupportedPreviewSizes =parameters.getSupportedPreviewSizes();
          mSupportedFlashModes = parameters.getSupportedFlashModes();
          bestSize = prSupportedPreviewSizes.get(0);
          for(int i = 1; i &lt; prSupportedPreviewSizes.size(); i++){
              if((prSupportedPreviewSizes.get(i).width * prSupportedPreviewSizes.get(i).height) &gt; (bestSize.width * bestSize.height)){
                  bestSize = prSupportedPreviewSizes.get(i);
              }
          }
  
  
  
          List&lt;Integer&gt; supportedPreviewFormats =  parameters.getSupportedPreviewFormats();
          Iterator&lt;Integer&gt; supportedPreviewFormatsIterator = supportedPreviewFormats.iterator();
          while(supportedPreviewFormatsIterator.hasNext()){
              Integer previewFormat =supportedPreviewFormatsIterator.next();
              if (previewFormat == ImageFormat.YV12) {
                  parameters.setPreviewFormat(previewFormat);
              }
          }
  
          List&lt;Camera.Size&gt; sizes = parameters.getSupportedPictureSizes();
          int w = 0, h = 0;
          for (Camera.Size size : sizes) {
              if (size.width &gt; w || size.height &gt; h) {
                  w = size.width;
                  h = size.height;
              }
  
          }
  
  
          parameters.setPreviewSize(bestSize.width, bestSize.height);
  
          parameters.setPictureSize(bestSize.width, bestSize.height);
          parameters.setPictureSize(w, h);
          parameters.setJpegQuality(100);
  
  
          // Set the camera to Auto Flash mode.
          if (mSupportedFlashModes != null &amp;&amp; mSupportedFlashModes.contains(Camera.Parameters.FLASH_MODE_AUTO)){
  
              parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
              prCamera.setParameters(parameters);
          }
          camera.setParameters(parameters);
  </code></pre>
  
  <p>But I am getting same result.</p>