如何在Zxing Scanner Camera View中添加按钮?

时间:2018-02-07 17:09:08

标签: java android qr-code zxing

我想在Zxing Scanner Camera View中添加一个按钮,尝试了很多程序,看到许多与Zxing库中的添加按钮相关的答案并提到了这个“How to add buttons in Zxing Scanner Camera View”,但是没有人在指导我,谢谢提前,我的代码是

MainActivity.Java

NSInteger

main.xml中

NSNumber

1 个答案:

答案 0 :(得分:0)

试试我的代码,它看起来像这样

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.package.name.TakeSingleScanActivity">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">




<android.support.v7.widget.Toolbar
    android:id="@+id/toolbarAdjustScan"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="@color/colorPrimary"
    android:elevation="6dp"
    android:minHeight="56dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

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

        <TextView
            android:id="@+id/titleToolbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="15dp"
            android:ellipsize="end"
            android:layout_weight="1"
            android:maxLines="1"
            android:textColor="@color/white"
            android:textSize="18dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dp"
            android:id="@+id/searchAdjustBtn"
            android:layout_marginLeft="15dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="SEARCH   "
            android:textColor="@color/white"
            android:textSize="13dp" />
    </LinearLayout>

</android.support.v7.widget.Toolbar>


<TextView
    android:id="@+id/textv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    android:gravity="center_horizontal"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:text="Point at any barcode to scan"
    android:textColor="@color/white"
    android:textSize="14sp"
    android:textStyle="normal" />

<RelativeLayout
    android:layout_marginBottom="120dp"
    android:layout_below="@+id/textv"
    android:id="@+id/relative_scan_take_single"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


</RelativeLayout>
</LinearLayout>

<LinearLayout
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:background="@color/colorPrimaryDark"
    android:layout_height="120dp"
    android:orientation="horizontal">

    <Button
        android:id="@+id/doneBtn"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="bottom"
        android:text="Done"
        android:textAllCaps="true"
        android:textColor="@color/white"
        android:textSize="22dp" />


</LinearLayout>

</RelativeLayout>

扫描程序的Java代码:

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

    }

private void init() {

        //Scanner
        mScannerView = new ZXingScannerView(this);
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.relative_scan_take_single);
        rl.addView(mScannerView);
        mScannerView.setResultHandler(this);
        mScannerView.startCamera();
        mScannerView.setSoundEffectsEnabled(true);
        mScannerView.setAutoFocus(true);

    }

 @Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on resume
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();           // Stop camera on pause
    }


@Override
    public void handleResult(Result rawResult) {
        // Do something with the result here
        Log.e(TAG, rawResult.getText()); // Prints scan results
        Log.e(TAG, rawResult.getBarcodeFormat().toString());

        Log.e("SCAN_RESULT", "" + rawResult.getText());
        //dataSingle.put("0",rawResult.getText());

    }