59022

Not able to convert Zxing into portrait mode in android

Question:

I have followed the following answer to do this. <a href="https://stackoverflow.com/a/16252917/2747591" rel="nofollow">https://stackoverflow.com/a/16252917/2747591</a>

But I am not getting what i want to do.

The image captured by camera is rotated by 90 degree while i am trying to scan. Like if you are clicking a photo of a person using the camera, then in my phone screen it is showing the preview rotated by 90 degree. But that is not what i want as it is making bar code scanning difficult to use. I want preview as it should be. Any ideas?

Here are my changes in the code

<strong>Step 1</strong>

In DecodeHandler.java I have added the following code just before buildLuminanceSource

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);

<strong>Step 2</strong>

Modified getFramingRectInPreview() 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;

<strong>Step 3</strong>:

Disable the check for Landscape Mode in initFromCameraParameters(...) in CameraConfigurationManager.java

The instructions is to Remove

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

But I didn't find this code in my Cameraconfiguration file. so it should not matter anyways

<strong>Step 4</strong>

Added following line to rotate camera in setDesiredCameraParameters(...) in CameraConfigurationManager.java just after defining parametres

camera.setDisplayOrientation(90);

<strong>Step 5</strong>

Changed the CaptureActivity orientation from landscape to portrait in my app's manifest file like this

<activity android:name="com.google.zxing.client.android.CaptureActivity" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateAlwaysHidden"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> <intent-filter> <action android:name="com.google.zxing.client.android.SCAN"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>

Answer1:

I have used zxing zxing 2.3 and below solution worked for me.

1 In CameraConfigurationManager class, setDesiredCameraParameters Method add below code below pointed line

-> Camera.Parameters parameters = camera.getParameters();

if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { camera.setDisplayOrientation(90); }

2 In CameraManager class, getFramingRect Method replace code as below

int width = MIN_FRAME_WIDTH; int height = MIN_FRAME_HEIGHT; if (context.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) { int tmp = 7 * screenResolution.x / 8; width = (tmp) < MIN_FRAME_WIDTH ? MIN_FRAME_WIDTH : (tmp); tmp = 1 * screenResolution.y / 3; height = (tmp) < MIN_FRAME_WIDTH ? MIN_FRAME_WIDTH : ((tmp) > MAX_FRAME_HEIGHT ? MAX_FRAME_HEIGHT : (tmp)); }else{ // Original Code width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, > MAX_FRAME_WIDTH); height = findDesiredDimensionInRange(screenResolution.y, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT); }

3 In CameraManager class, getFramingRectInPreview Method replace code as below

if (context.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) { 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; }else{ // Original code commented 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; }

4 In DecodeHandler class, decode Method add below code below pointed line

-> Result rawResult = null;

if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){ 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]; } data = rotatedData; int tmp = width; width = height; height = tmp; }

Please find my working code

<a href="http://www.compyutech.co.in/repo/zxing-dynamic.zip" rel="nofollow">http://www.compyutech.co.in/repo/zxing-dynamic.zip</a>

Hope this will help you....

Recommend

  • Deploying GWT app from GAE to Tomcat
  • Rows to Columns SQL Server
  • XML Catalog file failing to resolve
  • Create directory tree with forward slash in directory name [closed]
  • Can I break down a large-scale correlation matrix?
  • store in char column getting from decimal column in db2
  • simple Feed forward (newff) network in MATLAB
  • Expression simplification in SymPy
  • Encoded unicode characters lost
  • How do I avoid redefining the parent element class in CSS
  • Why is 0x20 subtracted from the stack pointer in the prologue of this function's code?
  • Lua Torch7 array index notation
  • .Net TextBox With AutoComplete and custom sorting (not alphabetical)
  • CSS animation do not work for svg in
  • Morphological Reconstruction in OpenCV
  • CSS while hover a div how to display other class(or) div?
  • Update data in d3.js group
  • Inserting a (g) node in the middle of a tree (SVG) using jQuery
  • iOS 9 errors and correct conversion to swift 2
  • Enumerating Controls on a Form
  • JQuery .html() remove line break on IE 8
  • order post according to custom array position
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Disable Enter in editText android
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • Change multiple background-images with jQuery
  • Avoid links criss cross / overlap in d3.js using force layout
  • Algorithm for a smudge tool?
  • Recording logins for password protected directories
  • Splitting given String into two variables - php
  • What is the “return” in scheme?
  • jQuery show() function is not executed in Safari if submit handler returns true
  • Can Jackson SerializationFeature be overridden per field or class?
  • Check if a string to interpolate provides expected placeholders
  • javascript inside java/jsp code
  • How to check if every primary key value is being referenced as foreign key in another table
  • WinForms: two way TextBox problem
  • How to disable jQuery.jplayer autoplay?
  • Android Studio and gradle
  • python regex in pyparsing