【问题标题】:Camera orientation change相机方向改变
【发布时间】:2013-06-04 11:48:30
【问题描述】:

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

1.Rotate UI -> 保持原样并旋转按钮。 不可接受:旋转不适用于 Android 2.3,应用需要与 2.3 兼容。

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

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

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

有什么想法吗?

【问题讨论】:

  • 文章介绍了如何保存重要数据。你到底有什么问题?
  • 那么,如何在会话之间保存 'ArrayList' 项?或任何其他类型的非原始类型。
  • 好吧,您可以将其序列化为文件或写入数据库。你认为什么是“好方法”?我想您必须将那篇文章与关于 data persistence 的文章结合起来。

标签: java android camera orientation


【解决方案1】:

好的,我明白了!我的下午有一个好主意:

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"

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

//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);

                }

还有我们更新按钮引用和委托的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
    }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多