9

I am working with such a great library zxing to read QR code. I already got QR code successfully.

Now, My application runs in landscape mode and camera takes the whole screen and red QR detection rectangle box is in the middle. I wanna change this to portrait mode & replace the camera view only with the red QR detection rectangle box.

I change CaptureActivity tag which is inside the zxing library manifest file.

android:screenOrientation="portrait".

But found nothing what I actually want. I don't know where I have to change/write code to get this.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Shihab Uddin
  • 6,699
  • 2
  • 59
  • 74
  • 1
    The tweak is simple, though need a little bit of changes across multiple files. Here I have put a complete solution for this question: http://stackoverflow.com/questions/16252791/how-to-show-zxing-camera-in-portrait-mode-android/16252917#16252917 – Roy Lee Apr 27 '13 at 14:55
  • 1
    @Roylee: thanks, your code also runs well.. – Shihab Uddin Jun 23 '13 at 10:46

3 Answers3

8

First, please don't copy our app completely. In this case you're going to need to write your own app anyway; it is definitely not as simple as changing the layout orientation. Not only is the UI not written for portrait, but neither is the app code.

If you want to use only fixed portrait mode, then this is not so hard. Fully supporting all 4 rotations, with front/back cameras, gets hard to get right. But not just 1 case.

First you need to design a portrait-mode UI. (Don't copy the red-line design please.)

The actual app code doesn't care about orientation for QR codes. You don't have to do anything there at all. But you do need to make changes to get the screen display right.

You will need to query Camera.getOrientation() to detect how the camera is mounted. Usually it's such that "up" is to your right, and I think just about every device works this way for compatibility. But technically you need to know so you know how much to rotate the preview image.

Then you use Camera.setDisplayOrientation() to tell it how much to rotate the image to make it rotated correctly for your portrait mode screen. This is usually "90".

Finally you need to select the preview size with Camera.Parameters.setPreviewSize() but will possibly need to flip the values you've chosen (480x800 vs 800x480) because you're in portrait.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
6

I was in same problem and got stuck about 2 days on it. Actually you have to do some tasks to achieve your goal.

  1. Download Zxing library for read QR code.. (Hope you have already)
  2. Create a project and add Zxing library.
  3. Your main.xml file should look like:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    
    <FrameLayout
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center_horizontal">
        <include layout="@layout/capture"/>
    </FrameLayout>
    
    </LinearLayout>
    
  4. Your main Activity should look like:

     public class ScannerActivity extends CaptureActivity {
    
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
    
            }
    
            @Override 
            public void handleDecode(Result rawResult, Bitmap barcode) 
            {
       Toast.makeText(this.getApplicationContext(), "Scanned code " + rawResult.getText(), Toast.LENGTH_LONG).show();
            }
        }
    
  5. In the manifest file add permission following:

     <uses-permission android:name="android.permission.CAMERA"/>
    
  6. and finally very IMPORTANT task you need to do for camera rotation problem, replace the following method into the

CameraManager.java (in the package com.google.zxing.client.android.camera)

@SuppressLint("NewApi") public void startPreview() {
    Camera theCamera = camera;
    if (theCamera != null && !previewing) {
        theCamera.setDisplayOrientation(90);
      theCamera.startPreview();
      previewing = true;
    }
  }

that's all . run and enjoy :-)

thanks..

Saiful
  • 279
  • 4
  • 10
2

I had really terrible experience from this library for the same reason. It seems that even if you succeed showing the camera in portrait mode, it will be stretched (or the output image would be stretched, or the barcode won't be sensed, or some devices had it working fine yet others didn't).

I suspect that Android simply "likes" the camera preview to be in landscape mode, as this is its natural orientation when the user takes pictures.

In the end, what I've done is using landscape mode and rotate everything else, so the user thinks it's portrait mode, but it's not. I think many apps use this technique.

I suggest that if you somehow manage to handle this problem, test it on as many devices as you can.

halfer
  • 19,824
  • 17
  • 99
  • 186
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • 1
    The library itself doesn't care at all about orientation. The app is landscape-only, so you can't use it for portrait -- but you should be writing a new app anyway. Android cameras are "usually" mounted so that landscape is the natural orientation but you have to query the API to actually know for sure! Yes, you don't actually have to rotate the image, but you do have to match preview resolution and rotation correctly. – Sean Owen Mar 23 '13 at 23:07
  • do you have any sample to show how to do it, for both simple camera app (preview&capture) and one for this library? – android developer Mar 24 '13 at 06:57
  • Problem is that there is no adequate example/tutorial. Only this app lol – Igor Konoplyanko Nov 19 '13 at 14:21