Crop camera preview for SurfaceView

Issue

I have a SurfaceView for barcode scan (by this tutorial).

<SurfaceView
        android:id="@+id/camera_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

Need to set there cropped camera preview with 1/4 height of the screen.

I’m set a view size based on screen size:

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);

But camera preview just shrinks inside!

Here is how i set it

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();
            }
        });

How can i crop camera preview? Need just a bottom 1/4 part.

Also need to add a transparent layer with rectangle viewfinder over it.

Solution

I solved it with a ViewGroup.

In layout:

    <com.superup.smartshelf.barcode.BarcodeCameraPreview
        android:id="@+id/preview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.25"/>

In class:

public class BarcodeCameraPreview extends ViewGroup {

private SurfaceView mSurfaceView;

public BarcodeCameraPreview(final Context context, AttributeSet attrs) {
    super(context, attrs);

    mSurfaceView = new SurfaceView(context);
    mSurfaceView.getHolder().addCallback(new SurfaceCallback());
    addView(mSurfaceView);
}

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // crop view
        int actualPreviewWidth = getResources().getDisplayMetrics().widthPixels;
        int actualPreviewHeight = getResources().getDisplayMetrics().heightPixels;


        if (mSurfaceView != null) {
            mSurfaceView.layout(0, 0, actualPreviewWidth, actualPreviewHeight);
        }
    }

// camera methods

}

In activity:

BarcodeCameraPreview camera = (BarcodeCameraPreview) findViewById(R.id.preview);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(metrics.widthPixels, metrics.heightPixels / 4);
camera.setLayoutParams(layoutParams);

Answered By – Andrey Rankov

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published