Cropping Video In Android

crop_video

Hey guyz, First of all to cropping video we need to use TextureView and this component available from android API 14.

Step 1: Create activity/fragment layout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextureView
        android:id="@+id/textureView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

Step 2: Add video file

If you want to add video to your project you should add it to /res/raw directory

Step 3: Add below code to activity/fragment

    ...

    public static final int MY_VIDEO_WIDTH = 1920;
    public static final int MY_VIDEO_HEIGHT = 1080;

    // I'm using ButterKnife binding my views you can use findViewById instead
    @BindView(R.id.textureView)
    TextureView textureView;
    MediaPlayer mediaPlayer;

    ...

    @Override
    public void onStart() {
        super.onStart();
        textureView.setSurfaceTextureListener(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
        Surface surface = new Surface(surfaceTexture);
        scaleToCropCenter(MY_VIDEO_WIDTH, MY_VIDEO_HEIGHT, this.textureView.getWidth(), this.textureView.getHeight());

        try {
            mediaPlayer = MediaPlayer.create(getActivity(), R.raw.video);
            mediaPlayer.setSurface(surface);
            mediaPlayer.setLooping(true);
            mediaPlayer.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void scaleToCropCenter(int videoWidth, int videoHeight, int viewWidth, int viewHeight) {
        float scaleX = 1.0f;
        float scaleY = 1.0f;

        if (videoWidth > viewWidth && videoHeight > viewHeight) {
            scaleX = division(videoWidth, viewWidth);
            scaleY = division(videoHeight, viewHeight);
        } else if (videoWidth < viewWidth && videoHeight < viewHeight) {             scaleY = division(viewWidth , videoWidth);             scaleX = division(viewHeight , videoHeight);         } else if (viewWidth > videoWidth) {
            scaleY = division(division(viewWidth, videoWidth), division(viewHeight, videoHeight));
        } else if (viewHeight > videoHeight) {
            scaleX = division(division(videoWidth, viewWidth), division(viewHeight, videoHeight));
        }

        int pivotPointX = viewWidth / 2;
        int pivotPointY = viewHeight / 2;

        Matrix matrix = new Matrix();
        matrix.setScale(scaleX, scaleY, pivotPointX, pivotPointY);

        textureView.setTransform(matrix);
        textureView.setLayoutParams(new FrameLayout.LayoutParams(viewWidth, viewHeight));
    }

    private float division(float value1, float value2) {
        if (value1 > value2)
            return value1 / value2;
        else return value2 / value1;
    }

    ...

That’s it 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s