Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camera is wrong unless keyboard is open

Tags:

android

I have 2 androids here.

In both of them, when I turn on my app, the camera shows up horribly wrong (turned 90 degrees sideways and stretched usually...)

In one of the phones, there are a keyboard, and when I open the keyboard the app work correctly... Since the keyboard forces a orientation, I figured that the issue is that the phones expect always the same orientation but the SDK disagrees.

How I then inform the camera what orientation it is supposed to use all the time?

like image 374
speeder Avatar asked Mar 31 '11 18:03

speeder


1 Answers

If your application runs on v2.2 or above you can rotate camera orientation to portrait using camera.setDisplayOrientation(90).

Prior to v2.2 the camera will only display in landscape orientation, so you're forced to set the activity to landscape orientation.

To support devices prior to v2.2 (API level 8) and after, one solution is to default the activity orientation to landscape in AndroidManifest.xml. Then at runtime check the API level and if froyo or above, change the activity orientation to portrait and rotate the camera display.

//in activity onCreate method
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO)
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

//After opening camera - call via reflection
Method rotateMethod = android.hardware.Camera.class.getMethod("setDisplayOrientation", int.class);
rotateMethod.invoke(mCamera, 90);

This is the most straightforward solution and hopefully as new devices come out v2.1 and below will drop off the radar.

like image 60
Ryan Reeves Avatar answered Oct 04 '22 20:10

Ryan Reeves