Skip to content

Instantly share code, notes, and snippets.

@border
Created November 21, 2013 08:38
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save border/7577978 to your computer and use it in GitHub Desktop.
Save border/7577978 to your computer and use it in GitHub Desktop.
zxing portrait mode for android base on zxing 2.3 ref: http://stackoverflow.com/questions/16252791/zxing-camera-in-portrait-mode-on-android
commit 8eed215edb5f1cd086e185d09c1b771015193ccc
Author: Jiang Bian <borderj@gmail.com>
Date: Wed Nov 20 19:21:52 2013 +0800
Zxing Camera in Portrait mode
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index db60cba..11a4cc8 100755
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -56,7 +56,7 @@
android:label="@string/app_name"
android:allowBackup="true">
<activity android:name=".CaptureActivity"
- android:screenOrientation="landscape"
+ android:screenOrientation="portrait"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:configChanges="orientation|keyboardHidden"
diff --git a/src/com/google/zxing/client/android/DecodeHandler.java b/src/com/google/zxing/client/android/DecodeHandler.java
index fa99679..c634ce3 100644
--- a/src/com/google/zxing/client/android/DecodeHandler.java
+++ b/src/com/google/zxing/client/android/DecodeHandler.java
@@ -75,6 +75,15 @@ final class DecodeHandler extends Handler {
private void decode(byte[] data, int width, int height) {
long start = System.currentTimeMillis();
Result rawResult = null;
+ 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; // Here we are swapping, that's the difference to #11
+ width = height;
+ height = tmp;
+ data = rotatedData;
PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);
if (source != null) {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
diff --git a/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java b/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java
index 90f0c35..42154c9 100644
--- a/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java
+++ b/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java
@@ -74,6 +74,7 @@ final class CameraConfigurationManager {
}
void setDesiredCameraParameters(Camera camera, boolean safeMode) {
+ camera.setDisplayOrientation(90);
Camera.Parameters parameters = camera.getParameters();
if (parameters == null) {
diff --git a/src/com/google/zxing/client/android/camera/CameraManager.java b/src/com/google/zxing/client/android/camera/CameraManager.java
index 77e116f..a64e4d6 100755
--- a/src/com/google/zxing/client/android/camera/CameraManager.java
+++ b/src/com/google/zxing/client/android/camera/CameraManager.java
@@ -41,8 +41,8 @@ public final class CameraManager {
private static final int MIN_FRAME_WIDTH = 240;
private static final int MIN_FRAME_HEIGHT = 240;
- private static final int MAX_FRAME_WIDTH = 1200; // = 5/8 * 1920
- private static final int MAX_FRAME_HEIGHT = 675; // = 5/8 * 1080
+ private static final int MAX_FRAME_WIDTH = 400; // = 5/8 * 1920
+ private static final int MAX_FRAME_HEIGHT = 600; // = 5/8 * 1080
private final Context context;
private final CameraConfigurationManager configManager;
@@ -252,10 +252,12 @@ public final class CameraManager {
// Called early, before init even finished
return null;
}
- 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;
+
+ 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;
+
framingRectInPreview = rect;
}
return framingRectInPreview;
@alaa7731
Copy link

in Galaxy S2, it shows a blurring when try to scan from near QR Code
and it never get a scan

@bitristan
Copy link

For latest version of zxing, the following code must be deleted, which is in CaptureActivity.java

if (prefs.getBoolean(PreferencesActivity.KEY_DISABLE_AUTO_ORIENTATION, true)) {
    setRequestedOrientation(getCurrentOrientation());
} else {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}

@simne7
Copy link

simne7 commented Mar 30, 2017

I additionally applied the following in order to improve the preview and recognition quality:

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,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment