使用 Matrix android java 进行视频裁剪
Posted
技术标签:
【中文标题】使用 Matrix android java 进行视频裁剪【英文标题】:Video cropping w/ 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));
【问题讨论】:
【参考方案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
【讨论】:
以上是关于使用 Matrix android java 进行视频裁剪的主要内容,如果未能解决你的问题,请参考以下文章