How to use Android MediaCodec encode Camera data(YUV420sp) How to use Android MediaCodec encode Camera data(YUV420sp) android android

How to use Android MediaCodec encode Camera data(YUV420sp)


I have solved the problem.As follows:

private synchronized void encode(byte[] data){    inputBuffers = mMediaCodec.getInputBuffers();// here changes    outputBuffers = mMediaCodec.getOutputBuffers();    int inputBufferIndex = mMediaCodec.dequeueInputBuffer(-1);    Log.i(TAG, "inputBufferIndex-->" + inputBufferIndex);    //......

And next,you will find your encoded video color is not right, for more information,please go to here MediaCodec and Camera: colorspaces don't match


The YUV420 formats output by the camera are incompatible with the formats accepted by the MediaCodec AVC encoder. In the best case, it's essentially NV12 vs. NV21 (U and V planes are reversed), requiring a manual reordering. In the worst case, as of Android 4.2, the encoder input format may be device-specific.

You're better off using MediaRecorder to connect the camera hardware to the encoder.

Update:It's now possible to pass the camera's Surface preview to MediaCodec, instead of using the YUV data in the ByteBuffer. This is faster and more portable. See the CameraToMpegTest sample here.