title: Android Custom Camera - Crop image inside Rectangle
body: <p>I have a custom camera app which have a centered rectangle view, as you can see below:</p>
  
  <p><img src="https://i.imgur.com/aQogNPh.png" alt="Camera Screenshot"></p>
  
  <p>When I take a picture I want to ignore everything outside the rectangle. The view hasn't any connection with the Camera Preview or SurfaceView in my XML view, as follows:</p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"&gt;
  &lt;FrameLayout
      android:id="@+id/preview"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"&gt;
      &lt;SurfaceView
          android:id="@+id/cameraview"
          android:name="com.kut.camera.KutCameraFragment"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" /&gt;
  
      &lt;View android:id="@+id/viewTamanho"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_marginBottom="400px"
          android:layout_marginTop="300px"
          android:layout_marginStart="70px"
          android:layout_marginEnd="70px"
          android:background="@drawable/border" /&gt;
  
      &lt;RelativeLayout
          android:id="@+id/rel_layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent" &gt;
  
          &lt;LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:alpha="1"
              android:background="@android:color/black"
              android:orientation="vertical"
              android:padding="10dp" &gt;
  
              &lt;RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent" &gt;
  
                  &lt;TextView
                      android:id="@+id/textViewReferan"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_centerHorizontal="true"
                      android:layout_centerVertical="true"
                      android:text="Evidência"
                      android:textColor="@android:color/white"
                      android:textSize="16sp" /&gt;
  
                  &lt;Button
                      android:id="@+id/button_exit"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentEnd="true"
                      android:layout_alignParentRight="true"
                      android:layout_alignParentTop="true"
                      android:background="@android:color/transparent"
                      android:text="Sair"
                      android:textColor="#2799CF" /&gt;
              &lt;/RelativeLayout&gt;
  
              &lt;LinearLayout
                  android:id="@+id/progress_layout"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_gravity="center"
                  android:gravity="center"
                  android:orientation="vertical"
                  android:visibility="gone" &gt;
  
                  &lt;ProgressBar
                      android:id="@+id/progressBar1"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content" /&gt;
  
                  &lt;TextView
                      android:id="@+id/islem_value_textView"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Carregando..." /&gt;
  
              &lt;/LinearLayout&gt;
          &lt;/LinearLayout&gt;
  
          &lt;RelativeLayout
              android:id="@+id/RelativeLayout1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
              android:layout_alignParentLeft="true"
              android:layout_alignParentStart="true"
              android:alpha="0.9"
              android:background="@android:color/black"
              android:padding="10dp" &gt;
  
              &lt;ImageView
                  android:id="@+id/imageView_foto"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerHorizontal="true"
                  android:layout_centerVertical="true"
                  android:src="@drawable/camera" /&gt;
  
              &lt;ImageView
                  android:id="@+id/imageView_photo"
                  android:layout_width="80dp"
                  android:layout_height="100dp"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentStart="true"
                  android:layout_centerVertical="true"
                  android:layout_marginRight="5dp"
                  android:layout_marginEnd="5dp"
                  android:padding="5dp"
                  android:scaleType="fitCenter"
                  android:src="@drawable/fotoicon" /&gt;
          &lt;/RelativeLayout&gt;
      &lt;/RelativeLayout&gt;
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>Can somebody help me how to crop the image properly? I tried to create a new Bitmap based on my XML but obviously it didn't work, something like:</p>
  
  <pre><code>Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
      public void onPictureTaken(byte[] data, Camera camera) {
          //.../
          Bitmap imagemOriginal = BitmapFactory.decodeByteArray(data, 0, data.length);
          Bitmap imagemCortada = Bitmap.createBitmap(imagemOriginal, 70, 400, imagemOriginal.getWidth() - 70,
                  imagemOriginal.getHeight() - 400);
          //.../
      }
  </code></pre>
  
  <p>I put those initial values for x and y, and tried to subtract (based on XML values from View referring to marginTop, Bottom, etc...) from width and Height but I hadn't success because I don't know how to match View coordinates with the image coordinates taken from Camera. Also, it seems for me Bitmap.createBitmap does limited crop, apparently I can't crop it to a rectangle directly.</p>