java - 相机方向改变

标签 java android camera orientation

我一直在开发一个基于 Redlaser 扫描仪库的条形码扫描仪应用程序,我正在尝试在扫描仪覆盖层上实现一个按钮,以切换相机的方向。我已经解决这个问题很多时间了,但仍然找不到任何解决方案。以下是我尝试过但尚未成功的解决方案列表。也许有人可以改进它们或想出一些不同的东西。

1.Rotate UI -> 保持方向不变,只旋转按钮。 Not Acceptable 旋转不适用于 Android 2.3,应用需要与 2.3 兼容。

2.使用android:configChanges="keyboardHidden|orientation|screenSize"setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);改变方向。 不起作用:这会旋转 View ,但图像会失真,当我向上移动手机时,相机会向左移动。

3.使用camera.setDisplayOrientation(90);使用解决方案 2。不可能 camera对象位于 RedLaser 库代码中,我无权访问它。也就是说,在我的 ScannerActivity.java 中我没有任何 Camera 对象。所以我不知道如何引用正在使用的那个。

4.使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 不确定: 似乎有效,但这会重新启动 Activity ,我需要保留数据。我熟悉this article ,但我还没有找到使用它的好方法。我希望保存 Activity 中的所有数据,其中包括一些 ArrayList<BarcodeResult> .

有什么想法吗?

最佳答案

好的,我知道了!我的下午有这个好主意:

5.在布局 xml 中复制粘贴整个 UI 并将原始的用于纵向,新的用于横向:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

 <LinearLayout
    android:id="@+id/preview_frame_overlay"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
    android:orientation="vertical"
    android:visibility="visible" >

  <!-- here is the rest of the portait layout code -->

 </LinearLayout>

<LinearLayout
    android:id="@+id/preview_frame_overlay2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
    android:orientation="vertical"
    android:rotation="90"
    android:visibility="gone" >

<!-- here is the  rest of the landscape layout code -->

</LinearLayout>

当然你应该使用android:configChanges="keyboardHidden|orientation|screenSize"

并且在您的扫描 Activity 中,您应该根据方向更改可见和可用的按钮:

//in onCreate
if (!ProductionActivity.settingLandscape) // if landscape set to portait
                {

                    portraitView.setVisibility(View.VISIBLE);
                    landscapeView.setVisibility(View.GONE);
                    findViewById(R.id.view_finder).setVisibility(View.VISIBLE);
                    findViewById(R.id.view_finder2).setVisibility(View.GONE);
                    ChangeToLandscape(false);

                } else
                {

                    portraitView.setVisibility(View.GONE);
                    landscapeView.setVisibility(View.VISIBLE);
                    findViewById(R.id.view_finder).setVisibility(View.GONE);
                    findViewById(R.id.view_finder2).setVisibility(View.VISIBLE);
                    ChangeToLandscape(true); 

                    android.view.ViewGroup.LayoutParams params1 = landscapeView.getLayoutParams(); //used to resize screen
                    params1.height = mDisplay.getWidth();
                    landscapeView.setLayoutParams(params1);

                }

以及更新按钮引用和委托(delegate)的 ChangeToLandscape 方法:

    private void ChangeToLandscape(boolean landscape)
        {
            if (landscape)
                {
                    hintTextView = (TextView) findViewById(R.id.hint_text2);
                    foundTextView = (TextView) findViewById(R.id.num_found_text2);
                    tvLastBarcode = (TextView) findViewById(R.id.tv_lastBarcode2);
                    doneButton = (Button) findViewById(R.id.button_done2);
                    bMultiscan = (Button) findViewById(R.id.bMultiscan2);
                    toggleTorchButton = (Button) findViewById(R.id.button_toggle_torch2);
                    toggleLineButton = (Button) findViewById(R.id.button_toggle_line2);
                    bRotate = (Button) findViewById(R.id.bRotate2);
                    viewfinderView = findViewById(R.id.view_finder2);

                } else
                {
                    hintTextView = (TextView) findViewById(R.id.hint_text);
                    foundTextView = (TextView) findViewById(R.id.num_found_text);
                    tvLastBarcode = (TextView) findViewById(R.id.tv_lastBarcode);
                    doneButton = (Button) findViewById(R.id.button_done);
                    bMultiscan = (Button) findViewById(R.id.bMultiscan);
                    toggleTorchButton = (Button) findViewById(R.id.button_toggle_torch);
                    toggleLineButton = (Button) findViewById(R.id.button_toggle_line);
                    bRotate = (Button) findViewById(R.id.bRotate);
                    viewfinderView = findViewById(R.id.view_finder);
                }
    //and then we re-assign the buttons delegates
    doneButton.setOnClickListener(new OnClickListener()
                {
                    public void onClick(View v)
                        {
                            doneScanning();
                        }
                }); //....etc
    }

使用这个想法, Activity 永远不会重新启动,因此我的数据得以保留并且效果很好。

关于java - 相机方向改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16917014/

相关文章:

java - Apache Commons FileUpload getString() 方法 - Java

android - IllegalArgumentException:无法在 FileProvider.getUriForFile 上找到包含 xxx 的配置根目录

android - 如何使用新的 MediaCodec getInputBuffer(int index)

c++ - 如何将 "camera"放入 OpenGL 的立方体中

java - Log4j - 强制日志消息参数

java - 静态方法隐藏

Android:使用不断变化的 fragment 改变菜单项的可见性

java - 为什么我得到 result.get(CaptureResult.CONTROL_AF_STATE); == 无效?

camera - 如何在 LibGDX 中让相机跟随玩家

java - 遇到语法错误怎么办?