Derekyy
Derekyy

Reputation: 1038

How to make ZXing barcode scanner not in full-screen, but below a fragment

I have made ZXing in portrait mode. Now I am going to make it appears under a fragment. How could I do that? It just always be in full-screen, which is definitely not what i want. Many Thanks for any hints or sample.

Like his work, but it is a fragment and it is a barcode scanner.

Exactly what I want.

QRCodeReader.java:

public class QRCodeReader extends Activity implements TopBar.OnFragmentInteractionListener, QRCodeReaderInner.OnFragmentInteractionListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.page_qrcodereader);

    FragmentManager fm = getFragmentManager();
    Fragment f = fm.findFragmentById(R.id.fragment_scanner);

    if (f == null) {
        f = QRCodeReaderInner.newInstance("param1", "param2");
        fm.beginTransaction().add(R.id.fragment_scanner, f).commit();
    }
}
...
}

page_qrcodereader.xml

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<fragment
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:name="com.innoasia.kaytami.innoasia.ui.generic.TopBar"
    android:id="@+id/fragment" />

<fragment
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:name="com.innoasia.kaytami.innoasia.ui.page.QRCodeReaderInner"
    android:id="@+id/fragment_scanner" />
</LinearLayout>

Upvotes: 3

Views: 9886

Answers (2)

Abhishek Dhiman
Abhishek Dhiman

Reputation: 1651

The simplest thing that you can do is modify the the capture.xml of ZXing library and make the click callback to your Activity either by startActivityForResult or by creating interface that will provide the callbacks to your Activity.

Here is a small example as what I have did to make my Scanner look like it is inside a TabActivity:

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

<SurfaceView
    android:id="@+id/preview_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true" />

<com.google.zxing.client.android.ViewfinderView
    android:id="@+id/viewfinder_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/transparent" />

<LinearLayout
    android:id="@+id/result_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/result_view"
    android:orientation="vertical"
    android:padding="4dip"
    android:visibility="gone" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="top"
        android:orientation="horizontal"
        android:padding="12dip" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="right"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/barcode_image_view"
                android:layout_width="160dip"
                android:layout_height="wrap_content"
                android:layout_marginBottom="4dip"
                android:adjustViewBounds="true"
                android:maxHeight="160dip"
                android:maxWidth="160dip"
                android:scaleType="centerInside" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/format_text_view_label"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingRight="4dip"
                    android:text="@string/msg_default_format"
                    android:textColor="@color/result_minor_text"
                    android:textSize="14sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/format_text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/result_minor_text"
                    android:textSize="14sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/type_text_view_label"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingRight="4dip"
                    android:text="@string/msg_default_type"
                    android:textColor="@color/result_minor_text"
                    android:textSize="14sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/type_text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/result_minor_text"
                    android:textSize="14sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/time_text_view_label"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingRight="4dip"
                    android:text="@string/msg_default_time"
                    android:textColor="@color/result_minor_text"
                    android:textSize="14sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/time_text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/result_minor_text"
                    android:textSize="14sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/meta_text_view_label"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingRight="4dip"
                    android:text="@string/msg_default_meta"
                    android:textColor="@color/result_minor_text"
                    android:textSize="14sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/meta_text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/result_minor_text"
                    android:textSize="14sp" />
            </LinearLayout>
        </LinearLayout>

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/contents_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:autoLink="web"
                android:paddingLeft="12dip"
                android:textColor="@color/result_text"
                android:textColorLink="@color/result_text"
                android:textSize="22sp" />
        </ScrollView>
    </LinearLayout>

</LinearLayout>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center_horizontal"
    android:background="@color/transparent"
    android:textColor="@color/status_text"
    android:textSize="14sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center_horizontal"
    android:orientation="horizontal"
    android:paddingTop="4dp" >

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="10dp" />

    <ImageView
        android:layout_width="1sp"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:background="#3B6816" />

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:background="@null" />

    <ImageView
        android:layout_width="1sp"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:background="#3B6816" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="10dp" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/black"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

Let me know if this helps you...

Upvotes: 0

Rethinavel
Rethinavel

Reputation: 3952

Check this out:

You have to make YourActivity to extend the CaptureActivity. There is an override method called handleDecode(Result rawResult, Bitmap barcode). You will get the scanned result here.

    public class ScanCard extends CaptureActivity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.scan_card);
       }
    @Override
    public void handleDecode(Result rawResult, Bitmap barcode) {
        // TODO Auto-generated method stub
        super.handleDecode(rawResult, barcode);
            mScanResult = rawResult.getText().toString();
            // Result After scanning the QR code.
        }
    }
    }

Add the following in R.layout.scan_card xml file. I just set the width and height of the scanning area to "300dip" Adjust to your own size.

    <RelativeLayout
                android:id="@+id/relativeScanLayout"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="5dip" >

                <FrameLayout
                    android:id="@+id/frame_scan"
                    android:layout_width="300dip"
                    android:layout_height="300dip"
                    android:layout_centerInParent="true"
                    android:layout_gravity="center" >

                    <include layout="@layout/capture" />

                // Include the scanning area here

                </FrameLayout>

Make sure you haved added the Zxing Project to your build path. Let me know if you have any queries.

Upvotes: 2

Related Questions