Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

android - Camera is wrong unless keyboard is open

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...