Android Question CCTV MJPEG camera output stream rotation

BertI

Member
Licensed User
Longtime User
Though I have tried much searching I can't yet find the best answer to this. The problem itself seems common - namely the image sent in the video stream (jpegs) from this example: https://www.b4x.com/android/forum/threads/mjpeg-cctv-server.73792/post-468593 is not in the expected orientation on some devices, e.g in my case rotated by 90 degrees.

I notice there is this line in the CamEx class:
B4X:
    r.RunMethod2("setRotation", result, "java.lang.int")
However this doesn't appear to change anything if you use different values for the result parameter. Manipulating "setDisplayOrientation" does work in terms of changing the preview orientation but this does not appear to have a bearing on the orientation of the captured camera data.

Is it that "setRotation" doesn't mean what I assumed it meant, or that it doesn't work on all devices (this one is Android 11, SDK reporting as 30)?

That being so, is there a more efficient way to rotate the image after the capture? I did try using:
B4X:
        Dim b As Bitmap = BytesToImage(jpeg)
        jpeg=ImageToBytes(b.Rotate(270))
in the Camera1_Preview sub (borrowing from: https://www.b4x.com/android/forum/threads/b4x-bytes-to-file.70111/post-445167), but this does slow things down (and I'm guessing will add extra memory loading).

Another problem is that the aspect ratio of the image becomes portrait after the conversion, but I guess there is not much that can be done about that without discarding some image data (e.g cropping top and bottom) due to the basics of the orientation of the sensor array.
 
Top