Android Zxing change orientation to portrait

别说谁变了你拦得住时间么 提交于 2019-11-28 15:57:50

问题


I'm trying to rotate Zxing display after reading a few questions and posts about the issue. After following the instructions, the display did rotate, but the rectangle of the scanner is not positioned as it should (as can be seen on the image attached).

This is what I have done:

  1. in CameraConfigurationManager:

    camera.setDisplayOrientation(90);
    
  2. in 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. in 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;
    


回答1:


After a lot of struggling, I found the problem, and I hope it will help someone in the future.

On initFromCameraParameters method in CameraConfigurationManager there is an assumption that the scan is ALWAYS in landscape mode, and therefor a fix when width < height. If You follow the steps in the question and remove this check, it works fine.




回答2:


Thank you for your answer!! it really helped me, one thing that I noticed is that at least on zxing 2.1 you need to pass "rotatedData" to buildLuminanceSource instead of just "data", the line end up like this:

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

Hopefully this helps someone else!




回答3:


As of zxing library:2.2.0 support for orientation change is inherent

Add/edit the following in manifest:

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

Set additional property at call to scanner :

IntentIntegrator integrator = new IntentIntegrator(this);

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

Reference link : https://github.com/journeyapps/zxing-android-embedded#changing-the-orientation




回答4:


Well I made a small change in ProjectLibrary (xzing project) and able to change orientation landscape to portrait

In setDesiredCameraParameters method of class CameraConfigurationManager added

camera.setDisplayOrientation(90);

.. in my original project's AndroidManifest.xml file. I set screenOrientation = portrait and Its working fine on my 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. In CameraConfigurationManager:

    camera.setDisplayOrientation(90);
    
  2. In 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. In 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. In CameraConfigurationManager:

    if    (width > height) {
      Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect");
      int temp = width;
      width = height;
      height = temp;
    }
    
  5. Change android:screenOrientation="portrait" for CaptureActivity in manifest.




回答6:


I'm a developer of Barcode Scanner. Yes, it takes a lot more than this to make it scan in portrait mode. You have to "rotate" the image data, and account for the orientation of the device, its default orientation, and its sensor's orientation.

Barcode Scanner+ scans in portrait mode, and you can integrate with it via Intent in exactly the same way that you integrate with Barcode Scanner. (However it's a for-pay app.)




回答7:


I've tried various patches, suggested in other answers, but barcode recognition remained unreliable.

I highly recommend using repository below in portrait mode. Try it, it's fast and stable. I used it in my hybrid app.

https://github.com/Dbuggerx/BarcodeScanner




回答8:


try this : add android:screenOrientation="sensorPortrait"

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



回答9:


In your library go to manifest file change the below line under activity tag

android:screenOrientation="portrait"


来源:https://stackoverflow.com/questions/10216943/android-zxing-change-orientation-to-portrait

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!