【问题标题】:How to use Zxing in portrait mode?如何在竖屏模式下使用 Zxing?
【发布时间】:2020-11-03 15:59:35
【问题描述】:

目前 zxing 库仅支持横向模式。对于我的应用,我需要在纵向模式下使用

【问题讨论】:

标签: android


【解决方案1】:

这是纵向模式扫描的解决方案

首先在您的应用级 gradle 文件中声明这两行

implementation 'com.journeyapps:zxing-android-embedded:3.0.1@aar'
implementation 'com.google.zxing:core:3.2.0'

在您的 xml 文件中定义一个按钮,并在按钮的 Onclick 侦听器中在 MainActivity java 文件中编写以下代码

IntentIntegrator integrator = new IntentIntegrator(this);
    integrator.setPrompt("Scan a barcode");
    integrator.setCameraId(0);  // Use a specific camera of the device
    integrator.setOrientationLocked(true);
    integrator.setBeepEnabled(true);
    integrator.setCaptureActivity(CaptureActivityPortrait.class);
    integrator.initiateScan();

在 onCreate() 方法之后在 MainActivity java 文件中编写以下代码

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
    if(result.getContents() == null) {
        Log.d("MainActivity", "Cancelled scan");
        Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
    } else {
        Log.d("MainActivity", "Scanned");
        st_scanned_result = result.getContents();
        Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();

    }
  }

}

然后创建一个名为 CaptureActivityPortrait 的类来扩展 CaptureActivity。该类如下所示

  package soAndSo(Your PackageName);

  import com.journeyapps.barcodescanner.CaptureActivity;

  public class CaptureActivityPortrait extends CaptureActivity {
  }

最重要的是在清单文件中声明您的 CaptureActivityPortrait,如下所示

<activity android:name=".CaptureActivityPortrait"
        android:screenOrientation="sensorPortrait"
        android:stateNotNeeded="true"
        android:theme="@style/zxing_CaptureTheme"
        android:windowSoftInputMode="stateAlwaysHidden"></activity>

【讨论】:

  • 这应该被接受。你拯救了我的一天。谢谢你,小伙伴。我从谷歌视觉切换到这个,找不到源如何将默认横向更改为纵向。
  • 在更新应用程序 gradle 后: compile 'com.google.zxing:core:3.2.1' 并删除清单声明中的 zxing 主题它也对我有用。谢谢分配!
  • 你先生是个英雄。
【解决方案2】:

只需查看在纵向模式下使用 Zxing 的问题。

【讨论】:

【解决方案3】:

我想在纵向模式下使用条形码阅读器。我找到了solution here 正如该线程前面发布的评论中所述。我想把它作为一个答案,这样更容易为有同样问题的人找到解决方案。

要在纵向模式下使用扫描仪,您需要在AndroidManifest.xml 中添加以下活动。

<activity
    android:name="com.journeyapps.barcodescanner.CaptureActivity"
    android:screenOrientation="portrait"
    tools:replace="screenOrientation" />

就是这样。

查看链接了解更多详情。

【讨论】:

  • 绝对一份复制粘贴解决了我的问题...谢谢
  • 很高兴知道这有帮助! :)
  • 完美解决方案
  • 这是哪个版本的zxing?
  • 哈哈...很高兴听到这个消息!干杯!
【解决方案4】:

setDisplayOrientation(int)不影响PreviewCallback.onPreviewFrame传入的字节数组的顺序。 (有关更多信息,请参阅 JavaDoc)

表示需要旋转previewCallback返回的数据,但这是yuv数据,需要转成rgb数据再旋转。可以纵向模式扫描条码,但需要更长的时间,因为需要更多时间来处理从 yuv 到 rgb 的数据并旋转 rgb 数据。 所以这里的问题是我们如何从 previewcallback 获取纵向模式的 yuv 数据?当我们为相机参数设置 setPreviewSize 时,如果不支持 previewsize 则会出现异常。这是这个问题的问题。如果相机驱动程序不支持纵向模式的 previewSize(高度 > 宽度),则无法在纵向模式下获取 yuv 数据。其余的取决于您,您可以在纵向模式下扫描条形码,但需要更长的时间,或者您必须将屏幕方向更改为横向才能更快地获得结果。

【讨论】:

    【解决方案5】:

    请检查如下(Official Documentation

    将此活动添加到清单文件

    <activity
            android:name="com.journeyapps.barcodescanner.CaptureActivity"
            android:screenOrientation="fullSensor"
            tools:replace="screenOrientation" />
    

    将积分器 OrientationLocked 设置为 false

    IntentIntegrator integrator = new IntentIntegrator(this);
    integrator.setOrientationLocked(false);
    integrator.initiateScan();
    

    希望对你有帮助

    【讨论】:

      【解决方案6】:
      1. 为了使屏幕纵向工作,为活动设置纵向方向(例如在清单中),然后配置相机:在 CameraConfigurationManager.setDesiredCameraParameters(Camera camera) 中使用 camera.setDisplayOrientation(90)。但请注意:

        • setDisplayOrientation(int) 需要 Android 2.2
        • setDisplayOrientation(int) 不会影响 PreviewCallback.onPreviewFrame 中传递的字节数组的顺序。 (有关更多信息,请参阅 JavaDoc)
      2. 因为预览帧总是“横向”,我们需要旋转它们。我使用了comment #11 提供的顺时针旋转。不要忘记在旋转后交换宽度和高度参数。 decodeHandler.java,在decode(byte[] data, int width, int height)中buildLuminanceSource之前旋转数据

        rotatedData = new byte[data.length];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++)
                rotatedData[x * height + height - y - 1] = data[x + y * width];
        }
        int tmp = width; // Here we are swapping, that's the difference to #11
        width = height;
        height = tmp;
        
      3. 我还按照#c11 的建议修改了CameraManager.java,getFramingRectInPreview():

        rect.left = rect.left * cameraResolution.y / screenResolution.x;
        rect.right = rect.right * cameraResolution.y / screenResolution.x;
        rect.top = rect.top * cameraResolution.x / screenResolution.y;
        rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;
        
      4. 我没有修改 getCameraResolution()。这是与#c11 的第二个不同之处。

      因此,我已经完成了 UPC 和其他一维码纵向扫描工作。

      附:您也可以调整FramingRect的大小(即扫描时屏幕上可见的矩形),FramingRectInPreview会自动调整。

      【讨论】:

        【解决方案7】:

        只需将这些代码添加到项目的 AndroidManifest.xml 中

        <activity android:name="com.journeyapps.barcodescanner.CaptureActivity"
                android:screenOrientation="sensorPortrait"
                android:stateNotNeeded="true"
                android:theme="@style/zxing_CaptureTheme"
                android:windowSoftInputMode="stateAlwaysHidden"
                tools:replace="android:screenOrientation" />
        

        【讨论】:

          【解决方案8】:

          使用这个android库https://github.com/SudarAbisheck/ZXing-Orient

          它支持纵向和横向。

          【讨论】:

          • @Drew 在我看来这是一个不错的答案...... OP 想要一个支持更多肖像的 ZXing 版本,而这个答案正是如此。它帮助我解决了同样的要求。
          • 我确定确实如此。多年前,此站点上的资源请求很好。指导方针发生了变化。当新的答案带有仅链接的答案时,您通常会得到我给您的 cmets。
          【解决方案9】:

          将此 android:screenOrientation="sensorPortrait" 添加到您的清单中

          <activity android:name=".CaptureActivity"
                        android:screenOrientation="sensorPortrait"
                        android:clearTaskOnLaunch="true"
                        android:stateNotNeeded="true"
                        android:theme="@style/CaptureTheme"
                        android:windowSoftInputMode="stateAlwaysHidden"
          tools:replace="android:theme"/>
          

          【讨论】:

            猜你喜欢
            • 2013-10-13
            • 1970-01-01
            • 1970-01-01
            • 2016-02-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多