【问题标题】:Android Zxing change orientation to portraitAndroid Zxing 将方向更改为纵向
【发布时间】:2012-04-18 19:52:43
【问题描述】:

在阅读了一些关于该问题的问题和帖子后,我正在尝试旋转 Zxing 显示。 按照说明操作后,显示器确实旋转了,但扫描仪的矩形 未按应有的位置放置(如所附图片所示)。

这就是我所做的:

  1. 在 CameraConfigurationManager 中:

    camera.setDisplayOrientation(90);
    
  2. 在 DecodeHandler.java 中

    byte[] 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;         
    width = height;
    height = tmp;
    
  3. 在 CameraManager.java 中:

    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;
    

【问题讨论】:

标签: android zxing


【解决方案1】:

经过一番折腾,我找到了问题所在,希望以后能对大家有所帮助。

CameraConfigurationManager 中的initFromCameraParameters 方法上,假设扫描是ALWAYS in landscape mode,因此修复了width &lt; height。 如果您按照问题中的步骤并删除此检查,则可以正常工作。

【讨论】:

  • 您好,我已经实施了第 1 步和第 3 步,现在我可以纵向查看了。我找不到实施 2 的地方,这使我无法纵向阅读代码。我应该将第 2 步中的代码放在哪里?我在任何地方都找不到对 rotateData 的引用。谢谢。
  • 你用的是哪个版本的zxing?
  • 我使用的是 v2.0。我实际上在此链接中找到了代码的位置。 code.google.com/p/zxing/issues/detail?id=178
  • @ROb : Okies....... 朋友 .. 第二点已添加到 byte[] rotateData .... 的解码方法中
  • @Sam - 您需要禁用 com.google.zxing.client.android.camera 中的 CameraConfigurationManager 类的检查。搜索“我们是横向的”并禁用此检查。
【解决方案2】:

从 zxing 库开始:2.2.0 对方向变化的支持是固有的

在清单中添加/编辑以下内容:

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

在调用扫描仪时设置附加属性:

IntentIntegrator integrator = new IntentIntegrator(this);

//allows portrait/landscape mode
integrator.setOrientationLocked(false);//"additional property"
integrator.initiateScan();

参考链接:https://github.com/journeyapps/zxing-android-embedded#changing-the-orientation

【讨论】:

  • 这就是我需要的。谢谢
  • 这对我也有用。值得一提的是,即使 IntentIntegrator 是从不同的 Activity/Fragment 调用的,也应该添加到清单中。
  • 也为我工作
  • 这是哪个版本的zxing?
  • 如何只固定到人像?
【解决方案3】:

谢谢你的回答!!它确实对我有帮助,我注意到的一件事是,至少在 zxing 2.1 上,您需要将“rotatedData”传递给 buildLuminanceSource 而不仅仅是“data”,该行最终是这样的:

PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(rotatedData, width, height);

希望这对其他人有帮助!

【讨论】:

  • 好吧,我也添加了你的答案:)谢谢它的工作!
  • 不改变这一点我觉得很愚蠢。谢谢,这是对问题中提供的信息和接受的答案的一个很好的补充!
【解决方案4】:

我在 ProjectLibrary(xzing 项目)中做了一个小改动,并且能够将方向横向更改为纵向

setDesiredCameraParameters method of class CameraConfigurationManager 中添加

camera.setDisplayOrientation(90);

.. 在我原始项目的 AndroidManifest.xml 文件中。我设置了screenOrientation = portrait,它在我的 ICS 4.0.3 上运行良好

   <activity
        android:name="com.google.zxing.client.android.CaptureActivity"
        android:configChanges="orientation|keyboardHidden"
        android:exported="false"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:windowSoftInputMode="stateAlwaysHidden" >
        <intent-filter>
            <action android:name="com.phonegap.plugins.barcodescanner.SCAN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity> 

【讨论】:

    【解决方案5】:
    1. CameraConfigurationManager:

      camera.setDisplayOrientation(90);
      
    2. DecodeHandler.java:

      byte[] 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;         
      width = height;
      height = tmp;
      
    3. CameraManager.java:

      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. CameraConfigurationManager:

      if    (width > height) {
        Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect");
        int temp = width;
        width = height;
        height = temp;
      }
      
    5. 在清单中将 android:screenOrientation="portrait" 更改为 CaptureActivity

    【讨论】:

    • 这对我有用。最好从@Dayan 获得编辑并指出进行这些更改的方法。我需要做的唯一额外更改是在CaptureActivity#onResume 处注释掉明确的setRequestedOrientation
    • 那是编辑zxing库的源码吗?
    【解决方案6】:

    我是条形码扫描仪的开发人员。是的,要让它以纵向模式扫描,需要的远不止这些。您必须“旋转”图像数据,并考虑设备的方向、默认方向和传感器方向。

    Barcode Scanner+ 以纵向模式扫描,您可以通过 Intent 与它集成,方式与integrate with Barcode Scanner 完全相同。 (但它是一款付费应用。)

    【讨论】:

    • 它确实可以旋转和扫描,唯一的问题是矩形的位置。
    • 我认为您在上面的代码中翻转了 x 和 y。至少显示器是这样运行的。 x 应该与 x 一起使用。
    • 翻转是有意的,正如我在此处的示例中看到的那样:code.google.com/p/zxing/issues/detail?id=178#c46
    • 某些东西肯定是翻转的,正如您从图像中看到的那样。它就像在风景中一样。你确定cameraResolutionscreenResolution 是你的想法吗?
    【解决方案7】:

    我尝试了其他答案中建议的各种补丁,但条形码识别仍然不可靠。

    我强烈建议在纵向模式下使用下面的存储库。试试吧,它又快又稳定。我在我的混合应用中使用了它。

    https://github.com/Dbuggerx/BarcodeScanner

    【讨论】:

      【解决方案8】:

      试试这个:添加android:screenOrientation="sensorPortrait"

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

      【讨论】:

        【解决方案9】:

        在您的库中转到清单文件,更改活动标记下的以下行

        android:screenOrientation="portrait"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-14
          • 1970-01-01
          相关资源
          最近更新 更多