Подтвердить что ты не робот

Камера Zxing в портретном режиме на Android

Я хочу показать ориентацию portrait на камере Zxing.

Как это можно сделать?

4b9b3361

Ответ 1

Вот как это работает.

Шаг 1: добавьте следующие строки, чтобы повернуть данные до buildLuminanceSource(..) в декодировании (байты [], int width, int height)

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;

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

Шаг 2: Изменить getFramingRectInPreview().

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;

Шаг 3: Отключите проверку ландшафтного режима в initFromCameraParameters(...)

CameraConfigurationManager.java

//remove the following
if (width < height) {
  Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect");
  int temp = width;
  width = height;
  height = temp;
}

Шаг 4: добавьте следующую строку для поворота камеры в setDesiredCameraParameters(...)

CameraConfigurationManager.java

camera.setDisplayOrientation(90);

Шаг 5: Не забудьте установить ориентацию активности на портрет. I.e: manifest

Ответ 2

Чтобы поддерживать всю ориентацию и изменять автоматически при выполнении этой операции, все, что вам нужно изменить, это класс CameraManager.java.

И удалите этот метод getCurrentOrientation() из CaptureActivity.java

В CameraManager.java Создайте эту переменную:

int resultOrientation;

Добавьте это в метод openDriver (..):

setCameraDisplayOrientation(context, Camera.CameraInfo.CAMERA_FACING_BACK, theCamera);//this can be set after camera.setPreviewDisplay(); in api13+.

**** Создайте этот метод **** Ссылка: http://developer.android.com/reference/android/hardware/Camera.html

public static void setCameraDisplayOrientation(Context context,int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int degrees = 0;
    switch (display.getRotation()) {
    case Surface.ROTATION_0: degrees = 0; break;
    case Surface.ROTATION_90: degrees = 90; break;
    case Surface.ROTATION_180: degrees = 180; break;
    case Surface.ROTATION_270: degrees = 270; break;
    }


    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        resultOrientation = (info.orientation + degrees) % 360;
        resultOrientation = (360 - resultOrientation) % 360;  // compensate the mirror
    } else {  // back-facing
        resultOrientation = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(resultOrientation);
}

**** Теперь измените getFramingRectInPreview() ****

if(resultOrientation == 180 || resultOrientation == 0){//to work with landScape and reverse landScape
            rect.left = rect.left * cameraResolution.x / screenResolution.x;
            rect.right = rect.right * cameraResolution.x / screenResolution.x;
            rect.top = rect.top * cameraResolution.y / screenResolution.y;
            rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;
        }else{
            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;
        }

И измените этот метод public PlanarYUVLuminanceSource buildLuminanceSource (..)

if(resultOrientation == 180 || resultOrientation == 0){//TODO: This is to use camera in landScape mode
        // Go ahead and assume it YUV rather than die.
        return new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top, rect.width(), rect.height(), false);
    }else{
        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;
        return new PlanarYUVLuminanceSource(rotatedData, width, height, rect.left, rect.top, rect.width(), rect.height(), false);
    }

Ответ 3

Вы можете использовать мою вилку zxlib https://github.com/rusfearuth/zxing-lib-without-landscape-only. Я отключил только ландшафтный режим. Вы можете установить пейзаж/портрет и увидеть правильный вид камеры.

Ответ 4

Добавление camera.setDisplayOrientation(90); в CameraConfigurationManager.java работало для меня.

Ответ 6

Я думаю, что лучшим решением для библиотеки является только один...

https://github.com/SudarAbisheck/ZXing-Orient

Вы можете включить его в build.gradle в качестве зависимости вашего проекта в формате maven...

dependencies {
  compile ''me.sudar:zxing-orient:[email protected]''
}

Ответ 8

В дополнение к модификации @roylee мне пришлось применить следующее к CameraConfigurationManager.java для получения наилучшего предварительного просмотра и качества распознавания QR-кода

    diff --git a/android/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java b/android/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java
index cd9d0d8..4f12c8c 100644
--- a/android/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java
+++ b/android/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java
@@ -56,21 +56,24 @@ public final class CameraConfigurationManager {
     Display display = manager.getDefaultDisplay();
     int width = display.getWidth();
     int height = display.getHeight();
-    // We're landscape-only, and have apparently seen issues with display thinking it portrait 
+    // We're landscape-only, and have apparently seen issues with display thinking it portrait
     // when waking from sleep. If it not landscape, assume it mistaken and reverse them:
+    /*
     if (width < height) {
       Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect");
       int temp = width;
       width = height;
       height = temp;
     }
+    */
     screenResolution = new Point(width, height);
     Log.i(TAG, "Screen resolution: " + screenResolution);
-    cameraResolution = findBestPreviewSizeValue(parameters, screenResolution, false);
+    cameraResolution = findBestPreviewSizeValue(parameters, screenResolution, true);//
     Log.i(TAG, "Camera resolution: " + cameraResolution);
   }

   void setDesiredCameraParameters(Camera camera) {
+    camera.setDisplayOrientation(90);
     Camera.Parameters parameters = camera.getParameters();

     if (parameters == null) {
@@ -99,7 +102,7 @@ public final class CameraConfigurationManager {
   Point getScreenResolution() {
     return screenResolution;
   }
-  
+
   public void setFrontCamera(boolean newSetting) {
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
     boolean currentSetting = prefs.getBoolean(PreferencesActivity.KEY_FRONT_CAMERA, false);
@@ -109,12 +112,12 @@ public final class CameraConfigurationManager {
       editor.commit();
     }
   }
-  
+
   public boolean getFrontCamera() {
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
     return prefs.getBoolean(PreferencesActivity.KEY_FRONT_CAMERA, false);
   }
-  
+
   public boolean getTorch() {
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
     return prefs.getBoolean(PreferencesActivity.KEY_FRONT_LIGHT, false);
@@ -181,7 +184,14 @@ public final class CameraConfigurationManager {
       Camera.Size defaultSize = parameters.getPreviewSize();
       bestSize = new Point(defaultSize.width, defaultSize.height);
     }
+
+    // FIXME: test the bestSize == null case!
+    // swap width and height in portrait case back again
+    if (portrait) {
+        bestSize = new Point(bestSize.y, bestSize.x);
+    }
     return bestSize;
+
   }

   private static String findSettableValue(Collection<String> supportedValues,

Ответ 9

Создайте AnyOrientationCaptureActivity, а затем переопределите CaptureActivity по умолчанию, после чего он будет работать.

public void scanCode() {
    IntentIntegrator integrator = new IntentIntegrator(this);
    integrator.setDesiredBarcodeFormats(CommonUtil.POSEIDON_CODE_TYPES);
    integrator.setPrompt("Scan");
    integrator.setCameraId(0);
    integrator.setBeepEnabled(false);
    integrator.setBarcodeImageEnabled(false);
    integrator.setOrientationLocked(false);
    //Override here
    integrator.setCaptureActivity(AnyOrientationCaptureActivity.class);

    integrator.initiateScan();
}

//create AnyOrientationCaptureActivity extend CaptureActivity
public class AnyOrientationCaptureActivity extends CaptureActivity {
}

Определить в манифесте

<activity
            android:name=".views.AnyOrientationCaptureActivity"
            android:screenOrientation="fullSensor"
            android:stateNotNeeded="true"
            android:theme="@style/zxing_CaptureTheme"
            android:windowSoftInputMode="stateAlwaysHidden"></activity>