簡體   English   中英

帶Matrix的視頻裁剪android java

[英]Video cropping w/ Matrix android java

任務是按給定的點(如矩形)裁剪視頻並顯示裁剪后的視頻。
所述代碼可與裁剪所述第一halh視頻的(0,0,videoWidth / 2,videoHeight)。 但是,當我嘗試顯示第二個(videoWidth / 2、0,videoWidth,videoHeight)時, 即顯示了該內容

視頻顯示在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 )找到的。 完整代碼

通過給定點 (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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM