简体   繁体   English

以横向模式返回(android.hardware.Camera)

[英]Returning (android.hardware.Camera) in landscape mode

I am building an app that contains a video capture from a custom camera with a surface view. 我正在构建一个应用程序,其中包含来自具有表面视图的自定义相机的视频捕获。 It works fine in portrait mode, but when I change to landscape mode the camera preview goes to a blank screen. 在人像模式下它可以正常工作,但是当我更改为风景模式时,相机预览将进入空白屏幕。

Here's the code I'm using: 这是我正在使用的代码:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.camera_activity);

        camera1 = Camera.open(camId);
        try {
            camera1.setPreviewDisplay(surfaceHolder);
            camera1.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }

}

In the ConfigurationChanged function I'm not getting the camera1 Object. ConfigurationChanged函数中,我没有得到camera1对象。

How can I solve this? 我该如何解决?

You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter like this: 您需要重写onSaveInstanceState(Bundle savedInstanceState),并将要更改的应用程序状态值写入Bundle参数,如下所示:

You can saved instance of each variable like this 您可以像这样保存每个变量的实例

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putBoolean("myBoolean", false);
  savedInstanceState.putDouble("myDouble", 1.121);
  savedInstanceState.putInt("myInt", 0);
  savedInstanceState.putString("myString", "Your String");
  // etc.
}

and That changes you can retrive like this 而这种变化,您可以像这样检索

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  boolean myBoolean = savedInstanceState.getBoolean("myBoolean");
  double myDouble = savedInstanceState.getDouble("myDouble");
  int myInt = savedInstanceState.getInt("myInt");
  String myString = savedInstanceState.getString("myString");
}

Hope this will help you .cheers! 希望这对您有所帮助!

UPDATE: 更新:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        {   
            p.set("orientation", "portrait");
            p.set("rotation",90);
        }
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
        {                               
            p.set("orientation", "landscape");          
            p.set("rotation", 90);
        }

and set Property Like THis: 并设置如下属性:

Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
camera.setParameters(parameters);

您可以让camera1对象幸免于Activity的死活和复活,或者(效率较低)销毁相机onStop()或(更高效)在配置更改时保留Activity实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM