【问题标题】:Video cropping w/ Matrix android java使用 Matrix android java 进行视频裁剪
【发布时间】:2019-02-22 19:17:51
【问题描述】:

任务是按给定点(如矩形)裁剪视频并显示裁剪后的视频。
code 用于裁剪视频(0, 0, videoWidth/2, videoHeight)的first halh。但是当我尝试显示第二个(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));
}

【问题讨论】:

    标签: java android matrix video crop


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 2016-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      相关资源
      最近更新 更多