Ask Your Question

Android Native OpenCV sample: camera preview changes with orientation.

asked 2013-11-09 11:23:01 -0600

AFK gravatar image

updated 2013-11-11 14:20:21 -0600

Hello everyone,
I have read several questions about this topic, but not one was really focused on what I experienced.
I am running the native opencv sample on my cell and tablet: Samsung Galaxy S2 and Google Nexus 10.
on both devices, the sample did not work very well.

  1. it only started when the orientation was landscape
  2. it crashed when trying to put the device portrait
  3. the optimal resolution calculated was always limited to 640x480, no matter what device was selected

I took a glance at the code, to see if there were some enhancements possible: in fact, yes, it seems there are.
I figured out how to make it select a better resolution, and how to make it work also in portrait mode. In fact, these were little changes, which did not affect algorithms inside the sample.
Now the application works, but what I see is the following:

  1. in landscape mode, 90 deg CCW with respect to portrait, everything is well, the text above is aligned with the image from the camera.
    image description
  2. in portrait mode, the image is stretched and rotated 90 deg CCW.
    image description
  3. in landscape mode, 90 deg CW with respect to portrait, image is rotated 180 deg, text is correct and image upside down.
    image description

Some forum members (Itay) suggest to take the image and rotate (with a combined transpose and flip) in order to have it ready to display. I did not try, but I am positive it will work.
But I am looking for a different solution, if anyone can advice:
the image rotation happens when handling a command of the APP_CMD_INIT_WINDOW type.
Is it possible to check the orientation of the display and prevent OpenCV from rotating the image? Does it make sense? I would prefer not to affect java code or XML.
Did anyone encounter such a behaviour?
Sorry for the length. Thank you


edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-11-12 01:02:20 -0600

OpenCV does not rotate images by it self. All rotations are a part of camera code inside Android or in the way of image output. There were some fixes for the native sample in OpenCV4Android SDK 2.4.7. I recommend you to try it. Also, if you want to fix orientation, you can set portrait orientation in sample manifest in the same way as Java samples do.

edit flag offensive delete link more

answered 2013-11-13 15:26:54 -0600

AFK gravatar image

Thank you Alexander,
if I understand well, the rotation of the image is not an OpenCV feature, correct?
In fact, I guessed so. I take a little of your time just to make a simple example.
I have two Android devices, a Galaxy S2 with Android 4.0.3 and a Nexus 10 with Android 4.3. The two device behave in a very different way for the Camera application (the default official camera app installed, to take photographs).
Galaxy S2: when rotating the camera, from landscape to portrait to landscape etc., the image does not update with the orientation.
Nexus 10: every time orientation changes, images updates: it freezes and restarts after 'processing' the rotation. but anyway, the image still maintains the same aspect and orientation...
It's easier to see it on the device than to explain.
This makes me think it must be an Android feature.
In fact I would like to reproduce the 4.0.3 behaviour (much better IMHO) on 4.3, do you think this could be possible? The change on the manifest will do?

For the native sample, I will surely check it out. I also made some fixes, I will be pleased to send you if I do not find them on the 2.4.7. Thank you again.

edit flag offensive delete link more


landscape orientation in the Manifest file solved the problem.

AFK gravatar imageAFK ( 2013-12-07 06:19:03 -0600 )edit

Question Tools


Asked: 2013-11-09 11:23:01 -0600

Seen: 2,481 times

Last updated: Nov 13 '13