Camera parameters setRotation () does not rotate frames received in onPreviewFrame ()

I have a custom view that extends SurfaceView and implements Camera.PreviewCallback. I am using this view as a camera preview. Also implemented functionality that captures video frames for buffering and streaming.

When the device orientation changes, I call setRotation () with the appropriate argument.

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

      

But unfortunately there is no reorientation of the frames captured in the onPreviewFrame () callback. What I am trying to achieve is that if I rotate the streaming device, the video streams sent to the other device will rotate accordingly.

I also tried to take some rotated shots as described. setRotation () only affects the rotation of images taken with the front camera (which is strange), photos from the reverse camera are not affected at all.

My question is, how can I get properly rotated frames suitable for streaming or rotating them in a callback?

Here is my onPreviewFrame method:

@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
}

      

+3


source to share


1 answer


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];
    }

      

Also, after rotation, you must change the width and height.



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

      

+2


source







All Articles