6

I am using Zbar for reading QRCode. I use this https://github.com/DushyanthMaguluru/ZBarScanner example for my activity. The Question is how can I show cameraView on my FrameLayout ?

EDIT:

        @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    mCamera = getCameraInstance();

    if(!isCameraAvailable())
    {
        cancelRequest();
        return;
    }

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

    mAutoFocusHandler = new Handler();

    setupScanner();

    mPreview = new CameraPreview(this, this, autoFocusCB);
    //setContentView(mPreview);
    FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
    preview.addView(mPreview);
}

userAsk
  • 125
  • 1
  • 8
  • What did you try so far? You can place a `SurfaceView` inside. I believe there are `some` tutorials on the web. – gunar Aug 06 '13 at 11:54
  • @gunar - can you give me a link – userAsk Aug 06 '13 at 11:56
  • Reading this again, why do you need to show the camera yourself? You're supposed to call a zbar specific activity and provide camera permissions. Something like: http://stackoverflow.com/questions/14494034/zbar-integration-into-android-app – gunar Aug 06 '13 at 11:58
  • I can decode QR Code. But I want to customize cameraPreview. For Example: I want to show button, imageView on cameraPreview. – userAsk Aug 06 '13 at 12:01
  • I have main.xml. I build some button, imageView and FrameLayout. I only want to show camera in this FrameLayout. – userAsk Aug 06 '13 at 12:03
  • Then you only need to add the `CameraPreview` View in that FrameLayout. You don't need to handle the camera preview. – gunar Aug 06 '13 at 12:06
  • I would rather have a big `RelativeLayout` with CameraPreview inside (match_parent on both v/h) and add the button and imageView after that. – gunar Aug 06 '13 at 12:09
  • I think I haven't explained good. I have this https://github.com/DushyanthMaguluru/ZBarScanner example. and I have main.xml file. In this Zbar example, I want to show the cameraPreview on my FrameLayout with another items(button, imageView, ...). How can I do this? I think I have to build SurfaceView for this issue. But I dont know how. Can you write code sample? – userAsk Aug 06 '13 at 12:16
  • You have a `CameraPreview` class that manages the camera preview. Look in ZBarScannerActivity how that's used (created on the fly). Instead of calling `setContentView(mPreview)` you could create a viewgroup layout that adds this `CameraPreview` object and others: your label and image. Does it make sense? – gunar Aug 06 '13 at 12:21
  • That means you will have to modify the project yourself ... or import those 2 classes to your project as the license is permissive (MIT). – gunar Aug 06 '13 at 12:23
  • on scanner activity I add setContentView(R.layout.main); and FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview); preview.addView(mPreview); but it s noteworking – userAsk Aug 06 '13 at 12:24
  • how related is your `scanner activity` to `ZBarScannerActivity`? Is it the same or does it start the `ZBarScannerActivity`? – gunar Aug 06 '13 at 12:25
  • I have only that changes on ZBarScannerActivity: EDIT: – userAsk Aug 06 '13 at 12:28
  • I can't figure out where you are putting this code, some context please? – Alioo Nov 26 '13 at 17:20

1 Answers1

4

First of all remove the line that seems to me to acquire camera access: mCamera = getCameraInstance();. You don't want to do that as the CameraPreview will do it for you.

I wouldn't use a FrameLayout as items are put one after the other and you want to put your cameraPreview at last. So you should have a LinearLayout in another RelativeLayout (I know, it's not efficient, but that's fine for now). Something like (your main.xml layout):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/zbar_layout_area"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>

    <ImageView
        android:id="@+id/my_own_image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@+id/my_own_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

Now, you would need to put the CameraPreview in zbar_layout_area. For this reason, try to change the code to (blind coding):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (!isCameraAvailable()) {
        // Cancel request if there is no rear-facing camera.
        cancelRequest();
        return;
    }

    // Hide the window title.
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main);

    mAutoFocusHandler = new Handler();

    // Create and configure the ImageScanner;
    setupScanner();

    // Create a RelativeLayout container that will hold a SurfaceView,
    // and set it as the content of our activity.
    mPreview = new CameraPreview(this, this, autoFocusCB);
    LinearLayout zbarLayout = (LinearLayout) findViewById(R.id.zbar_layout_area);
    mPreview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    zbarLayout.addView(mPreview);
}

Also make sure you have set enough prermissions:

<uses-permission android:name="android.permission.CAMERA" />

    <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.camera.autofocus"
        android:required="false" />
gunar
  • 14,660
  • 7
  • 56
  • 87