用相机时的方向变化问题 [英] Problem with camera when orientation changes

查看:149
本文介绍了用相机时的方向变化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个调用不同的活动时,设备方向的变化,从横向到纵向或反之亦然的应用程序。在横向模式的活动是一个增强现实的,所以我用相机展示环境给用户。然而,当我回去试试肖像,应用程序崩溃,并显示以下错误:

I'm doing an application that calls a different activity when the device orientation changes from landscape to portrait or viceversa. The activity for the landscape mode is an augmented reality one, so I use the camera to show the surroundings to the user. However, when I try to go back to portrait, the app crashes and displays this error:

08-17 16:05:42.622: ERROR/AndroidRuntime(9769): FATAL EXCEPTION: main
08-17 16:05:42.622: ERROR/AndroidRuntime(9769): java.lang.RuntimeException: set display orientation failed
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at android.hardware.Camera.setDisplayOrientation(Native Method)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at es.ucm.look.ar.Preview.surfaceChanged(Preview.java:102)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at android.view.SurfaceView.updateWindow(SurfaceView.java:549)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at android.view.SurfaceView.setFrame(SurfaceView.java:294)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at android.view.View.layout(View.java:7169)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at android.view.View.layout(View.java:7175)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at android.view.View.layout(View.java:7175)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at android.view.View.layout(View.java:7175)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at android.os.Looper.loop(Looper.java:130)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at java.lang.reflect.Method.invokeNative(Native Method)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at java.lang.reflect.Method.invoke(Method.java:507)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):     at dalvik.system.NativeStart.main(Native Method)

从纵向到横向的变化正常工作。

The change from portrait to landscape works normally.

这是我的Andr​​oid清单(活动EmergenciesList是一个为肖像模式,而MapaMovilAR为lanscape):

This is my android manifest (the activity "EmergenciesList" is the one for portrait mode, and "MapaMovilAR" for lanscape):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.list" android:versionCode="1" android:versionName="1.0">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="@string/app_name"
    android:name="cl.puc.memoria.MapaMovilApplication">
    <activity android:name="cl.puc.memoria.EmergenciesList"
        android:configChanges="orientation" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"></action>
            <category android:name="android.intent.category.LAUNCHER">    </category>
        </intent-filter>
    </activity>
    <activity android:name="cl.puc.memoria.AR.MapaMovilAR"
        android:configChanges="orientation">
    </activity>
    <activity android:name="es.ucm.look.ar.LookAR"></activity>

</application>
<uses-sdk android:minSdkVersion="7" />

</manifest> 

最后,这是code处理的方向变化:

And finally this is the code for handling the orientation change:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Intent el = new Intent(MapaMovilAR.this,
                cl.puc.memoria.EmergenciesList.class);
        startActivity(el);
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

有没有什么可以导致崩溃?

Is there anything that could be causing the crash?

推荐答案

使用:

mCamera.stopPreview();
mCamera.setDisplayOrientation(mRotation);
mCamera.startPreview();

这篇关于用相机时的方向变化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆