Видео обрезка ж / Матрица Android Java - PullRequest
0 голосов
/ 22 февраля 2019

Задача - обрезать видео по заданным точкам (например, прямоугольнику) и отобразить обрезанное видео.
Код работает с кадрированием первой половины видео (0,0, ширина видео / 2, высота видео).Но когда я попытался отобразить второй (videoWidth / 2, 0, videoWidth, videoHeight), это то, что было отображено .

Видео отображается на TextureView внутри FrameLayout.

Часть, которая не работает:

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

1 Ответ

0 голосов
/ 24 февраля 2019

Ответ нашел Дмитрий Яценко .Полный код .

Простой способ обрезать видео по заданным точкам (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
...