要在SurfaceView中从MediaCodec裁剪视频,您可以按照以下步骤进行操作:
SurfaceView surfaceView = findViewById(R.id.surfaceView);
Surface surface = surfaceView.getHolder().getSurface();
MediaCodec codec = MediaCodec.createDecoderByType("video/avc");
MediaFormat format = MediaFormat.createVideoFormat("video/avc", width, height);
codec.configure(format, surface, null, 0);
codec.start();
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputBufferIndex = codec.dequeueOutputBuffer(bufferInfo, TIMEOUT_US);
while (outputBufferIndex >= 0) {
ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferIndex);
// 在这里对输出缓冲区中的数据进行裁剪处理
// ...
codec.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = codec.dequeueOutputBuffer(bufferInfo, TIMEOUT_US);
}
Canvas canvas = surface.lockCanvas(null);
canvas.drawBitmap(yourCroppedBitmap, 0, 0, null);
surface.unlockCanvasAndPost(canvas);
通过以上步骤,您可以在SurfaceView中从MediaCodec裁剪视频并显示出来。请根据您的需求修改和完善代码示例。