android 如何在Zxing扫描仪相机视图中添加按钮?

nom7f22z  于 11个月前  发布在  Android
关注(0)|答案(2)|浏览(192)

我目前的Zxing扫描仪屏幕看起来像这样,


的数据
我想添加一个textview和它的按钮夫妇,我已经尝试了许多程序,但没有工作可以任何人指导我,我会虚心期待您的帮助
这里的代码
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);
    }
}

字符串
heres 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>

rsl1atfo

rsl1atfo1#

使用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" />

字符串
它看起来像:


的数据

icomxhvb

icomxhvb2#

我尝试通过代码直接添加按钮和文本,但它们从未显示在屏幕上。
因此,我所做的就是创建一个活动activity_barcode. xml。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginEnd="@dimen/fab_margin"
        android:contentDescription="@string/add_claim"
        android:src="@drawable/ic_add_white_24dp"
        android:tint="@android:color/white"
        app:backgroundTint="@color/colorAccent"
        app:elevation="6dp"
        app:pressedTranslationZ="12dp"
        app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:tint="@android:color/white" />

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

字符串
这是我的BracodeActivity

public class BarcodeActivity extends BaseActivity {

    private FloatingActionButton fab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_barcode);
        fab = findViewById(R.id.fab_action);

        fab.setOnClickListener(v -> Toast.makeText(this, "Excellent!!!", Toast.LENGTH_SHORT).show());

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        setupToolbar(getResources().getString(R.string.app_name), DEFAULT);

        BarcodeFragment barcodeFragment = new BarcodeFragment();

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, barcodeFragment)
                .commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }
}


并将该文库添加到片段BacodeFragment中

public class BarcodeFragment extends Fragment implements ZXingScannerView.ResultHandler {

    private ZXingScannerView mScannerView;

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

    @Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this);
        mScannerView.startCamera();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mScannerView = new ZXingScannerView(getActivity());
        return mScannerView;
    }

    @Override
    public void handleResult(Result rawResult) {
        String result = rawResult.getText();
        Intent data = new Intent();

        if (result == null || result.isEmpty()) {
            getActivity().setResult(RESULT_CANCELED);
        } else {
            data.putExtra(SCAN_RESULT, result);
            getActivity().setResult(CommonStatusCodes.SUCCESS, data);
        }

        getActivity().finish();

    }
}


的数据

相关问题