2012-10-07 40 views
2

在我的相機應用中,UI方向固定爲縱向。如何檢測設備旋轉以設置EXIF方向標籤(Android)

預覽通過設置camera.setdisplayorientation(90)正確顯示在橫向和縱向上。

我可以拍照並保存到文件中。當我通過ImageView顯示它時, 橫向拍攝的圖片可以正確顯示。 (在我左側的手機的頂部)

但是...在縱向方向採取了一個沒有那麼幸運... 它看起來像向左轉90度。

我嘗試通過傳感器檢測設備方向,以便我可以設置EXIF標頭 但是......迄今爲止...對我來說太難以實現......任何人都可以幫助我解決這個問題?

回答

4

使用getRotation方法:

Display display = ((WindowManager) 
context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
int rotation = display.getRotation(); 

從文檔:

Returns the rotation of the screen from its "natural" orientation. 
The returned value may be Surface.ROTATION_0 (no rotation), Surface.ROTATION_90, Surface. 
ROTATION_180, or Surface.ROTATION_270. 
For example, if a device has a naturally tall screen, and the user has turned it on its side to go into a landscape orientation, the value returned here may be either Surface.ROTATION_90 or Surface.ROTATION_270 depending on the direction it was turned. 
The angle is the rotation of the drawn graphics on the screen, which is the opposite direction of the physical rotation of the device. 
For example, if the device is rotated 90 degrees counter-clockwise, to compensate rendering will be rotated by 90 degrees clockwise and thus the returned value here will be Surface.ROTATION_90. 

getRotation從Android 2.2的推出。如果您的目標是較舊的設備,請使用getOrientation。

得到答案的形式在這裏: how to detect orientation of android device?

如果你只是想檢測設備的方向,那麼你可以使用OrientationEventListener。

這裏的官方文檔: http://developer.android.com/reference/android/view/OrientationEventListener.html#onOrientationChanged(int)

但你使用任何東西之前,看看這個優秀的博客帖子大約方位的Android操作: http://android-developers.blogspot.in/2010/09/one-screen-turn-deserves-another.html

+0

感謝您的回覆速度快,但是...因爲我UI固定爲肖像,所以... display.getRotation()總是返回0. –

+0

回答更新爲反映您的問題 –

+0

omg,這種方法非常好用! –