Created
November 21, 2013 08:38
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit 8eed215edb5f1cd086e185d09c1b771015193ccc | |
Author: Jiang Bian <[email protected]> | |
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I additionally applied the following in order to improve the preview and recognition quality: