title: Crop camera preview for SurfaceView
body: <p>I have a SurfaceView for barcode scan (by <a href="http://code.tutsplus.com/tutorials/reading-qr-codes-using-the-mobile-vision-api--cms-24680" rel="nofollow">this tutorial</a>).</p>
  
  <pre><code>&lt;SurfaceView
          android:id="@+id/camera_view"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:visibility="gone" /&gt;
  </code></pre>
  
  <p>Need to set there cropped camera preview with 1/4 height of the screen. </p>
  
  <p>I'm set a view size based on screen size:</p>
  
  <pre><code>cameraView = (SurfaceView) findViewById(R.id.camera_view);
  
  final DisplayMetrics metrics = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(metrics);
  
  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(metrics.widthPixels, metrics.heightPixels / 4);
          cameraView.setLayoutParams(layoutParams);
  </code></pre>
  
  <p>But camera preview just shrinks inside!</p>
  
  <p>Here is how i set it</p>
  
  <pre><code>final BarcodeDetector barcodeDetector =
          new BarcodeDetector.Builder(this)
                  .setBarcodeFormats(Barcode.ALL_FORMATS)
                  .build();
  
  final CameraSource cameraSource = new CameraSource
          .Builder(this, barcodeDetector)
          .setRequestedPreviewSize(metrics.widthPixels, metrics.heightPixels / 4)
          .setAutoFocusEnabled(true)
          .build();
  
  cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
              @Override
              public void surfaceCreated(SurfaceHolder holder) {
                      cameraSource.start(cameraView.getHolder());
              }
  
              @Override
              public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
              }
  
              @Override
              public void surfaceDestroyed(SurfaceHolder holder) {
                  cameraSource.stop();
              }
          });
  </code></pre>
  
  <p>How can i crop camera preview? Need just a bottom 1/4 part.</p>
  
  <p>Also need to add a transparent layer with rectangle viewfinder over it.</p>