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

nom7f22z  于 2024-01-04  发布在  Android
关注(0)|答案(2)|浏览(267)

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


的数据
我想添加一个textview和它的按钮夫妇,我已经尝试了许多程序,但没有工作可以任何人指导我,我会虚心期待您的帮助
这里的代码
Java文件:

  1. private ZXingScannerView mScannerView;
  2. @Override
  3. public void onCreate(Bundle state) {
  4. super.onCreate(state);
  5. setContentView(R.layout.activity_qrresult);
  6. mScannerView = new ZXingScannerView(this);
  7. setContentView(mScannerView);
  8. }
  9. @Override
  10. public void onResume() {
  11. super.onResume();
  12. mScannerView.setResultHandler(this);
  13. mScannerView.startCamera();
  14. Toast.makeText(getApplicationContext(),"Scan QR code to Send
  15. Money",Toast.LENGTH_LONG).show();
  16. }
  17. @Override
  18. public void onPause() {
  19. super.onPause();
  20. mScannerView.stopCamera(); // Stop camera on pause
  21. }
  22. @Override
  23. public void handleResult(Result rawResult) {
  24. // Do something with the result here
  25. if(rawResult.getText().toString().equals("Test Ewallet Test")){
  26. Toast.makeText(getApplicationContext(), rawResult.getText(),
  27. Toast.LENGTH_SHORT).show();
  28. Intent go = new Intent(qrresult.this,sendmoney.class);
  29. startActivity(go);
  30. }else {
  31. Toast.makeText(getApplicationContext(), "Couldn't scan the
  32. QRcode, Please Try again ", Toast.LENGTH_SHORT).show();
  33. mScannerView.resumeCameraPreview(this);
  34. }
  35. // mScannerView.resumeCameraPreview(this);
  36. // If you would like to resume scanning, call this method below:
  37. //mScannerView.resumeCameraPreview(this);
  38. }
  39. }

字符串
heres xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.example.tajveezrehman.applicationtest.yourqr">
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:orientation="vertical"
  12. android:layout_alignParentTop="true"
  13. android:layout_alignParentLeft="true"
  14. android:layout_alignParentStart="true">
  15. <Button
  16. android:id="@+id/flashlight"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_weight="0.8"
  20. android:text="Button" />
  21. <Button
  22. android:id="@+id/open image"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:layout_weight="0.8"
  26. android:text="Button" />
  27. <TextView
  28. android:id="@+id/camtxt"
  29. android:layout_width="match_parent"
  30. android:layout_height="match_parent"
  31. android:layout_marginTop="20dp"
  32. android:layout_weight="0.8"
  33. android:text="@string/scanner"
  34. android:textAlignment="center"
  35. android:textColor="#000000"
  36. android:textSize="20sp" />
  37. </LinearLayout>
  38. </RelativeLayout>

rsl1atfo

rsl1atfo1#

使用RelativeLayout,如下所示:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <me.dm7.barcodescanner.zxing.ZXingScannerView
  6. android:id="@+id/zxscan"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. />
  10. <ImageView
  11. android:id="@+id/btn_flash"
  12. android:layout_marginBottom="15dp"
  13. android:layout_centerHorizontal="true"
  14. android:layout_alignParentBottom="true"
  15. android:src="@drawable/flash_100"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content" />

字符串
它看起来像:


的数据

展开查看全部
icomxhvb

icomxhvb2#

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <com.google.android.material.appbar.AppBarLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:theme="@style/AppTheme.AppBarOverlay">
  11. <androidx.appcompat.widget.Toolbar
  12. android:id="@+id/toolbar"
  13. android:layout_width="match_parent"
  14. android:layout_height="?attr/actionBarSize"
  15. android:background="?attr/colorPrimary"
  16. app:popupTheme="@style/AppTheme.PopupOverlay" />
  17. </com.google.android.material.appbar.AppBarLayout>
  18. <com.google.android.material.floatingactionbutton.FloatingActionButton
  19. android:id="@+id/fab_action"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_gravity="bottom|end"
  23. android:layout_marginEnd="@dimen/fab_margin"
  24. android:contentDescription="@string/add_claim"
  25. android:src="@drawable/ic_add_white_24dp"
  26. android:tint="@android:color/white"
  27. app:backgroundTint="@color/colorAccent"
  28. app:elevation="6dp"
  29. app:pressedTranslationZ="12dp"
  30. app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
  31. app:layout_constraintBottom_toBottomOf="parent"
  32. app:layout_constraintRight_toRightOf="parent"
  33. app:tint="@android:color/white" />
  34. <FrameLayout
  35. android:id="@+id/content_frame"
  36. android:layout_width="match_parent"
  37. android:layout_height="match_parent"
  38. app:layout_behavior="@string/appbar_scrolling_view_behavior" />
  39. </androidx.coordinatorlayout.widget.CoordinatorLayout>

字符串
这是我的BracodeActivity

  1. public class BarcodeActivity extends BaseActivity {
  2. private FloatingActionButton fab;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_barcode);
  7. fab = findViewById(R.id.fab_action);
  8. fab.setOnClickListener(v -> Toast.makeText(this, "Excellent!!!", Toast.LENGTH_SHORT).show());
  9. Toolbar toolbar = findViewById(R.id.toolbar);
  10. setSupportActionBar(toolbar);
  11. setupToolbar(getResources().getString(R.string.app_name), DEFAULT);
  12. BarcodeFragment barcodeFragment = new BarcodeFragment();
  13. getSupportFragmentManager().beginTransaction()
  14. .replace(R.id.content_frame, barcodeFragment)
  15. .commit();
  16. }
  17. @Override
  18. public boolean onOptionsItemSelected(MenuItem item) {
  19. if (item.getItemId() == android.R.id.home) {
  20. finish();
  21. return true;
  22. } else {
  23. return super.onOptionsItemSelected(item);
  24. }
  25. }
  26. }


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

  1. public class BarcodeFragment extends Fragment implements ZXingScannerView.ResultHandler {
  2. private ZXingScannerView mScannerView;
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. }
  7. @Override
  8. public void onResume() {
  9. super.onResume();
  10. mScannerView.setResultHandler(this);
  11. mScannerView.startCamera();
  12. }
  13. @Override
  14. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  15. Bundle savedInstanceState) {
  16. mScannerView = new ZXingScannerView(getActivity());
  17. return mScannerView;
  18. }
  19. @Override
  20. public void handleResult(Result rawResult) {
  21. String result = rawResult.getText();
  22. Intent data = new Intent();
  23. if (result == null || result.isEmpty()) {
  24. getActivity().setResult(RESULT_CANCELED);
  25. } else {
  26. data.putExtra(SCAN_RESULT, result);
  27. getActivity().setResult(CommonStatusCodes.SUCCESS, data);
  28. }
  29. getActivity().finish();
  30. }
  31. }


的数据

展开查看全部

相关问题