1

正如@Aegonis 在这篇文章中所问

我正在尝试将相机与 Android 连接以对视频帧进行编码。

问题是,在这部分中,尝试做与他相同的事情,作为在他的主题中发布的另外 2 个用户,它崩溃了:

// 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 的缓冲区。那么...您将如何解决这个问题?

4

1 回答 1

1

尝试这个

Parameters camParams = camera.getParameters();
int size = imageWidth * imageHeight;
size  = size * ImageFormat.getBitsPerPixel(camParams.getPreviewFormat()) / 8;
mBuffer = new byte[size]; // class variable
camera.addCallbackBuffer(mBuffer);
于 2013-06-03T12:50:51.160 回答