1

I have followed the following answer to do this. https://stackoverflow.com/a/16252917/2747591

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

Step 1

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

Step 2

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;

Step 3:

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

Step 4

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

camera.setDisplayOrientation(90);

Step 5

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>
Community
  • 1
  • 1
Rohit Goyal
  • 1,521
  • 3
  • 24
  • 49
  • You do know that asking customers to stand on their heads to take a picture is difficult, so why would you ask us to do the same to help you? Without your code, standing on our heads is actually easier. ;) –  Nov 07 '14 at 08:48
  • @JeremyMiller I have followed the exact same procedure as told in the above answer I have mentioned at the top. That is the only code change. If you have used Zxing you would get it i guess. – Rohit Goyal Nov 07 '14 at 09:06
  • I have and it works, so let me turn the tables: if you've helped someone with their coding before where they claim it's exactly the same, you would get it, I guess. –  Nov 07 '14 at 09:08
  • @JeremyMiller Hahaha. Yeah. Let me re-edit the question. :P – Rohit Goyal Nov 07 '14 at 09:13
  • @JeremyMiller there you go :) – Rohit Goyal Nov 07 '14 at 09:29
  • I just checked the code I use (which only targets QR codes) and none of the stuff you all are using is part of it. But I did notice that my version is 2.1 -- I just import the `core.jar` file and have a few supporting classes. –  Nov 09 '14 at 00:36
  • @JeremyMiller Yeah that's because orientation does not matter in qr code. – Rohit Goyal Nov 09 '14 at 06:42
  • It matters when customers are looking at the image... they have a tendency to desire things to be perfect. –  Nov 09 '14 at 06:45

1 Answers1

0

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

http://www.compyutech.co.in/repo/zxing-dynamic.zip

Hope this will help you....

compyutech
  • 578
  • 3
  • 8
  • 26
  • I have made all these chanes but still the scanner is in landscape mode only from the start. – Rohit Goyal Nov 28 '14 at 08:55
  • @RohitGoyal Check for 1. version. 2. In menifest orientaton of CaptureActivity, it should not be fixed. 3. Try replacing class file from http://www.compyutech.comoj.com/repo/zxing-dynamic.zip 4. If not working, Provide code any how if possible. – compyutech Dec 01 '14 at 04:44