正如@Aegonis在此post中所问的
我正在尝试将Camera与Android连接以对视频帧进行编码。
问题是,在这一部分中,尝试与他做同样的事情,就像在他的主题中发布的另外两个用户一样,它崩溃了:
// called from Camera.setPreviewCallbackWithBuffer(...) in other class
public void encoder(byte[] input) {
try {
ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();
ByteBuffer[] outputBuffers = mediaCodec.getOutputBuffers();
int inputBufferIndex = mediaCodec.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0) {
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
inputBuffer.put(input);
mediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length, 0, 0);
}
...
我从setPreviewCallbackWithBuffer进行的呼叫是这样的:
int bufSize= 460800;
camera.addCallbackBuffer(new byte[bufSize]);
camera.setPreviewCallbackWithBuffer(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera arg1) {
codecs.encoder(data);
}
});
...
出现以下错误消息
03-04 20:30:46.762: E/AndroidRuntime(26196): java.nio.BufferOverflowException
03-04 20:30:46.762: E/AndroidRuntime(26196): at java.nio.Buffer.checkPutBounds(Buffer.java:189)
03-04 20:30:46.762: E/AndroidRuntime(26196): at java.nio.ReadWriteDirectByteBuffer.put(ReadWriteDirectByteBuffer.java:100)
03-04 20:30:46.762: E/AndroidRuntime(26196): at java.nio.ByteBuffer.put(ByteBuffer.java:712)
很好,字节缓冲区很大,但是setPreviewCallbackWithBuffer告诉您至少使用一个大小为460800的缓冲区。那么...您将如何解决此问题?
最佳答案
尝试这个
Parameters camParams = camera.getParameters();
int size = imageWidth * imageHeight;
size = size * ImageFormat.getBitsPerPixel(camParams.getPreviewFormat()) / 8;
mBuffer = new byte[size]; // class variable
camera.addCallbackBuffer(mBuffer);