web-dev-qa-db-ja.com

AndroidでポートレートモードのZxingカメラ

portraitのカメラでZxingの向きを表示したい。

これをどのように行うことができますか?

40
Roy Lee

仕組みは次のとおりです。

ステップ1:次の行を追加して、buildLuminanceSource(..) indecode(byte [] data、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:次の行を追加して、カメラを回転しますinsetDesiredCameraParameters(...)

CameraConfigurationManager.Java

_camera.setDisplayOrientation(90);
_

ステップ5:アクティビティの方向をポートレートに設定することを忘れないでください。つまり、マニフェスト

106
Roy Lee

すべての方向をサポートし、アクティビティを回転するときに自動的に変更するには、変更する必要があるのはCameraManager.Javaクラスだけです。

そして、このメソッドを削除しますgetCurrentOrientation()fromCaptureActivity.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's 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);
    }
7
Rensodarwin

私のzxlibのフォークを使用できます https://github.com/rusfearuth/zxing-lib-without-landscape-only 。横モードのみを無効にしました。ランドスケープ/ポートレートを設定して、正しいカメラビューを表示できます。

5
Rusfearuth

_CameraConfigurationManager.Java_にcamera.setDisplayOrientation(90);を追加するとうまくいきました。

3
Samit Dawane

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>
2
LinhNguyen

zxing 3.0の場合、作業用ライブラリ https://github.com/xiaowei4895/zxing-Android-portrait ポートレートモードの場合

ありがとうございました

2
Sameer Z.

最適なライブラリのみのソリューションはこれだと思います...

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

Maven形式のプロジェクトの依存関係としてbuild.gradleに含めることができます...

dependencies {
  compile ''me.sudar:zxing-orient:2.1.1@aar''
}
2
dodgy_coder

これは、上記のソリューションの同期バージョンになるはずです

https://github.com/zxing/zxing/tree/4b124b109d90ac2960078ce68e15a39885fc1b5b

1
sivi

@royleeの変更に加えて、可能な限り最高のプレビューとQRコード認識品質を得るために、CameraConfigurationManager.Javaに以下を適用する必要がありました

    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's portrait 
+    // We're landscape-only, and have apparently seen issues with display thinking it's portrait
     // when waking from sleep. If it's not landscape, assume it's 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,
1
simne7