带矩阵的视频裁剪 android java

Video cropping w/ Matrix android java

任务是按给定点(如矩形)裁剪视频并显示裁剪后的视频。
code works with cropping the first halh of video(0, 0, videoWidth/2, videoHeight). But when i tryed to display the second one(videoWidth/2, 0, videoWidth, videoHeight), that is what was displayed.

视频显示在 FrameLayout 内的 TextureView 上。

不起作用的部分:

private void updateTextureViewSize(int ax, int ay, int bx, int by) {
    float scaleX;
    float scaleY;

    //proportions between screen and frame dimensions
    scaleX = mVideoWidth / mDisplayWidth;
    scaleY = mVideoHeight / mDisplayHeight;

    float scaleRegionW = mVideoWidth / Math.abs(ax - bx);
    float scaleRegionH = mVideoHeight / Math.abs(ay - by);
    float scaleRegion = scaleRegionW < scaleRegionH ? scaleRegionW : scaleRegionH;

    Matrix matrix = new Matrix();
    if (scaleX > scaleY) {
        matrix.setScale(scaleRegion / scaleY, scaleRegion);
        matrix.postTranslate(-ax * (int) scaleRegion / scaleY, -ay * scaleRegion / scaleY);
    } else {
        matrix.setScale(scaleRegion, scaleRegion / scaleX);
        matrix.postTranslate(-ax * scaleRegion / scaleX, -ay * scaleRegion / scaleX);
    }
    mTextureView.setTransform(matrix);
    mTextureView.setLayoutParams(new FrameLayout.LayoutParams((int) mDisplayWidth, (int) mDisplayHeight));
}

Dmitry Yacenko 找到了答案。 完整的 code.

给定点(ax、ay、bx、xy)裁剪视频的一种简单方法是:

    float scaleX = mDisplayWidth / mVideoWidth, scaleY = mDisplayHeight / mVideoHeight; 
    //proportions between screen and frame dimensions
    float scale = mDisplayHeight / Math.abs(by - ay);
    Matrix matrix = new Matrix();
    matrix.reset();
    matrix.setScale(scale / scaleX, scale / scaleY);
    //scaling video
    matrix.postTranslate(-scale * ax, -scale * ay);
    //move video, so the needed part of it will be displayed properly
    mTextureView.setLayoutParams(new FrameLayout.LayoutParams((int) mDisplayWidth, (int) mDisplayHeight));
    mTextureView.setTransform(matrix);
    //updating the Texture view