Android Zxing改变方向为肖像

时间:2012-04-18 19:52:43

标签: android zxing

我正在尝试在阅读几个问题和关于这个问题的帖子之后轮换Zxing显示。 按照说明操作后,显示屏会旋转,但扫描仪的矩形会旋转 没有定位(如附图所示)。

这就是我所做的:

    CameraConfigurationManager中的
  1. camera.setDisplayOrientation(90);
    
  2. DecodeHandler.java中的
  3. 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;
    
  4. CameraManager.java中的
  5. 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;
    
  6. enter image description here

9 个答案:

答案 0 :(得分:30)

经过大量的挣扎,我发现了问题,希望将来有人帮助。

initFromCameraParameters中的CameraConfigurationManager方法中,假设扫描为ALWAYS in landscape mode,并在width < height时进行修复。 如果您按照问题中的步骤删除此检查,则可以正常工作。

答案 1 :(得分:14)

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

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

希望这有助于其他人!

答案 2 :(得分:8)

我在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> 

答案 3 :(得分:7)

从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

答案 4 :(得分: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"的{​​{1}}。

答案 5 :(得分:4)

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

Barcode Scanner+以纵向模式扫描,您可以通过Intent以与integrate with Barcode Scanner完全相同的方式进行扫描。 (但这是一个付费应用程序。)

答案 6 :(得分:1)

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

我强烈建议在纵向模式下使用以下存储库。尝试一下,它快速而稳定。我在我的混合应用程序中使用它。

https://github.com/Dbuggerx/BarcodeScanner

答案 7 :(得分:1)

尝试:添加SELECT Column1 FROM Table1 AS t WHERE EXISTS ( SELECT 1 FROM Table1 AS x WHERE t.Column1 LIKE x.Column2 + '%' )

android:screenOrientation="sensorPortrait"

答案 8 :(得分:0)

在您的书架中转到清单文件,在活动标签下更改以下行

android:screenOrientation="portrait"