相机参数setRotation()方法不会旋转onPreviewFrame()

时间:2015-07-22 10:46:02

标签: java android rotation video-streaming android-camera

我有自定义视图,可以扩展SurfaceView并实现Camera.PreviewCallback。我正在使用此视图作为相机预览。还实现了捕获视频帧以缓冲和流式传输的功能。

当设备的方向改变时,我用适当的参数调用setRotation()。

Camera.Parameters parameters = svVideo.getCamera().getParameters();
parameters.setRotation(rotation);
svVideo.getCamera().setParameters(parameters);

但遗憾的是,onPreviewFrame()回调中捕获的帧的方向没有变化。我想要实现的是,如果我旋转流设备,发送到其他设备的视频流将相应地旋转。

我还试图拍摄一些改变了旋转的照片,如上所述。 setRotation()仅影响使用前置摄像头拍摄的照片的旋转(这很奇怪),来自后置摄像头的照片完全不受影响。

我的问题是:我怎样才能获得适合流式传输的正确旋转帧或者在我自己的回调中旋转它们?

这是我的onPreviewFrame方法:

@Override
public void onPreviewFrame(final byte[] frame, final Camera camera) {
    final long now = System.nanoTime();
    if (outq.remainingCapacity() >= 2 && (now - this.frame) >= 1000000000 / fps) { //Don't encode more then we can handle, and also not more then FPS.
        queueFrameForEncoding(frame, now);
    }
    getEncodedFrames();
    camera.addCallbackBuffer(frame); //Recycle buffer
}

1 个答案:

答案 0 :(得分:3)

byte[] rotatedData = new byte[imgToDecode.length]; 
    for (int y = 0; y < imgHeight; y++) {
        for (int x = 0; x < imgWidth; x++)
            rotatedData[x * imgHeight + imgHeight - y - 1] = imgToDecode[x + y * imgWidth];
    }

旋转后你应该交换宽度和高度

int buffer = height;
height = width;
width = buffer;