【问题标题】:How to add buttons in Zxing Scanner Camera View如何在 Zxing 扫描仪相机视图中添加按钮
【发布时间】:2023-10-12 07:42:01
【问题描述】:

我目前的 Zxing 扫描仪屏幕是这样的,

我想在上面添加一个文本视图和几个按钮,我尝试了很多程序,但没有任何效果,谁能指导我,我将虚心期待您的帮助

这是代码

Java 文件:

    private ZXingScannerView mScannerView;
    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        setContentView(R.layout.activity_qrresult);
        mScannerView = new ZXingScannerView(this); 
        setContentView(mScannerView);              


    }

    @Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this); 
        mScannerView.startCamera();          
        Toast.makeText(getApplicationContext(),"Scan QR code to Send 
     Money",Toast.LENGTH_LONG).show();
    }

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

    @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here
        if(rawResult.getText().toString().equals("Test Ewallet Test")){
            Toast.makeText(getApplicationContext(), rawResult.getText(), 
    Toast.LENGTH_SHORT).show();
            Intent go = new Intent(qrresult.this,sendmoney.class);
            startActivity(go);

        }else {
            Toast.makeText(getApplicationContext(), "Couldn't scan the 
     QRcode, Please Try again ", Toast.LENGTH_SHORT).show();

            mScannerView.resumeCameraPreview(this);
        }



        // mScannerView.resumeCameraPreview(this);

        // If you would like to resume scanning, call this method below:
        //mScannerView.resumeCameraPreview(this);
    }
}

这里是 xml 文件

 <?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"
tools:context="com.example.tajveezrehman.applicationtest.yourqr">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <Button
        android:id="@+id/flashlight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.8"
        android:text="Button" />

    <Button
        android:id="@+id/open image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.8"
        android:text="Button" />

    <TextView
        android:id="@+id/camtxt"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp"
        android:layout_weight="0.8"
        android:text="@string/scanner"
        android:textAlignment="center"
        android:textColor="#000000"
        android:textSize="20sp" />

  </LinearLayout>

 </RelativeLayout>

【问题讨论】:

    标签: java android xml qr-code zxing


    【解决方案1】:

    像这样使用RelativeLayout

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <me.dm7.barcodescanner.zxing.ZXingScannerView
        android:id="@+id/zxscan"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />
    
    <ImageView
        android:id="@+id/btn_flash"
        android:layout_marginBottom="15dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:src="@drawable/flash_100"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

    它看起来像:

    【讨论】:

    • 应用程序崩溃它给出“应用程序已停止”
    • 请检查我发布的 Logcat
    • 从你的java文件中删除setContentView(mScannerView);
    • mScannerView= (ZXingScannerView) findViewById(R.id.zxscan);替换mScannerView = new ZXingScannerView(this);
    • ZXingScannerView zscanner = (ZXingScannerView) findViewById(R.id.zxscan); mScannerView = new ZXingScannerView(this); // 以编程方式初始化扫描仪视图 zscanner.addView(mScannerView);我正在使用它及其工作,可以吗?
    最近更新 更多