Android:使用zxing进行条形码扫描并以片段形式返回结果

模拟人生

我正在尝试在Fragment中使用zxing库。我可以使扫描仪工作并捕获条形码,但是始终运行活动​​中的onActivityResult方法,而不是Frament中的onActivityResult方法。

我试图在这个mentionned了不同的解决方案,但没有成功。

这是我的活动代码:

package com.test01.test01_tp2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.google.android.material.navigation.NavigationView;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;


public class MainActivityLibrarian extends AppCompatActivity  {

    DrawerLayout drawerLayout;
    String name;
    TextView tvName;

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


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

        drawerLayout = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView nav_view = findViewById(R.id.nav_view);
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new HomeLibrarianFragment()).commit();

        //Get the username and update the name in the drawer
        name = getIntent().getStringExtra("name");
        View headerView = nav_view.getHeaderView(0);
        tvName = headerView.findViewById(R.id.txtNameHeader);
        tvName.setText(name);


        nav_view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

                switch(menuItem.getItemId())
                {
                    case R.id.nav_add_loan:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new AddLoanFragment()).commit();
                        break;
                    case R.id.nav_list_loans:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ListLoansFragment()).commit();
                        break;
                    case R.id.nav_add_readers:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new AddReadersFragment()).commit();
                        break;
                    case R.id.nav_list_readers:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ListReadersFragment()).commit();
                        break;
                    case R.id.nav_add_books:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new AddBooksFragment()).commit();
                        break;
                    case R.id.nav_list_books:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ListBooksFragment()).commit();
                        break;
                    /*case R.id.close:
                        finish();
                        break;*/
                }

                drawerLayout.closeDrawer(GravityCompat.START);
                return true;
            }
        });

    }


    @Override
    public void onBackPressed() {
        if(drawerLayout.isDrawerOpen(GravityCompat.START))
            drawerLayout.closeDrawer(GravityCompat.START);
        else
            super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        System.out.println("the code is catch");
    }
}

这是我尝试使用条形码阅读器的片段代码:

package com.test01.test01_tp2;


import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;


/**
 * A simple {@link Fragment} subclass.
 */
public class AddLoanFragment extends Fragment {

    //View Objects
    private Button btnAdd, btnClear;
    private EditText etBarcodeReader, etBarcodeBook;
    private ImageButton imBtnBarcodeBook, imBtnBarcodeReader;

    //barcode scanner Object
    private IntentIntegrator barcodeScanner;


    public AddLoanFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_add_loan, container, false);

        //View Objects
        btnAdd = root.findViewById(R.id.btn_add);
        btnClear = root.findViewById(R.id.btn_clear);
        imBtnBarcodeBook = root.findViewById(R.id.imageButtonBarcodeBook);
        imBtnBarcodeReader = root.findViewById(R.id.imageButtonBarcodeReader);
        etBarcodeBook = root.findViewById(R.id.editTextBookBarcode);
        etBarcodeReader = root.findViewById(R.id.editTextReaderBarcode);

        imBtnBarcodeReader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               barcodeScanner = new IntentIntegrator(getActivity());
               barcodeScanner.forSupportFragment(AddLoanFragment.this).initiateScan();

            }
        });
        // Inflate the layout for this fragment
        return root;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        System.out.println("never here");
        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if (scanResult != null) {
            // handle scan result
        }
        // else continue with any other code you need in the method
    }

}

当我查看控制台时,我总是看到:代码已被捕获。我不明白:永远不会在这里。

请注意,barcodeScanner.forSupportFragment(AddLoanFragment.this).initiateScan();通过实例引用访问静态成员'com.google.zxing.integration.android.IntentIntegrator.forSupportFragment(androidx.fragment.app.Fragment)'时,我会得到警告,这可能很有用我不确定这是什么意思,如果那是问题。

我想我尝试了上面提到的帖子中提出的所有答案,但都没有成功。

谢谢你的帮助。

模拟人生

我发现了问题所在。当照相机开始查找条形码时,它将以横向模式开始。我想以纵向模式使用扫描仪,并旋转手机以重新开始活动(并留下我的片段)。我通过以人像模式启动相机来解决问题。为此,我在AndroidManifest.xml文件中添加了以下代码:

<activity
        android:name="com.journeyapps.barcodescanner.CaptureActivity"
        android:screenOrientation="portrait"
        tools:replace="screenOrientation" />

这是我片段的代码:

package com.test01.test01;


import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;


/**
 * A simple {@link Fragment} subclass.
 */
public class AddLoanFragment extends Fragment {
    private int mode;
    //View Objects
    private Button btnAdd, btnClear;
    private EditText etBarcodeReader, etBarcodeBook;
    private ImageButton imBtnBarcodeBook, imBtnBarcodeReader;

    public AddLoanFragment() {
        // Required empty public constructor
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_add_loan, container, false);

        //View Objects
        btnAdd = root.findViewById(R.id.btn_add);
        btnClear = root.findViewById(R.id.btn_clear);
        imBtnBarcodeBook = root.findViewById(R.id.imageButtonBarcodeBook);
        imBtnBarcodeReader = root.findViewById(R.id.imageButtonBarcodeReader);
        etBarcodeBook = root.findViewById(R.id.editTextBookBarcode);
        etBarcodeReader = root.findViewById(R.id.editTextReaderBarcode);

        imBtnBarcodeReader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               mode = 1;
               scanFromFragment();
            }
        });
        imBtnBarcodeBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mode = 2;
                scanFromFragment();
            }
        });
        // Inflate the layout for this fragment
        return root;
    }

    public void scanFromFragment() {
        IntentIntegrator.forSupportFragment(this).initiateScan();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Toast.makeText(getContext(),"Cancelled from fragment", Toast.LENGTH_LONG).show();
            } else {
                if (mode == 1) {
                    etBarcodeReader.setText(result.getContents());
                } else if (mode == 2) {
                    etBarcodeBook.setText(result.getContents());
                }
            }
        }
    }

}

现在可以删除我的Activity中的OnActivityResult()方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将ZXing Library集成到Android Studio进行条形码扫描?

来自分类Dev

使用Zxing读取9个扩展条形码中的3个-在Android中

来自分类Dev

生成的条形码Zxing Android的大小

来自分类Dev

Android使用Zxing生成QR码和条形码

来自分类Dev

使用Zbar或Zxing在各个方向进行条形码扫描

来自分类Dev

在Java中使用Zxing扫描多个条形码

来自分类Dev

在WebView Android中扫描条形码

来自分类Dev

扫描条形码时,Zxing库的CaptureActivity没有返回

来自分类Dev

在Android应用中使用zxing扫描条形码阅读器数据

来自分类Dev

使用Java Zxing API将条形码内容写为条形码下方的标签

来自分类Dev

片段中的条形码扫描仪

来自分类Dev

Zxing-动态更改扫描的条形码类型

来自分类Dev

错误进行条形码扫描的Android Mobile Vision

来自分类Dev

条形码扫描损坏

来自分类Dev

通过使用React Native,Expo扫描条形码来进行Webview Uri重定向

来自分类Dev

使用PyQt5进行条形码扫描

来自分类Dev

如何在其他地方使用条形码扫描仪的结果?

来自分类Dev

我应该使用新活动还是片段来扫描条形码?

来自分类Dev

使用Zbar或Zxing在各个方向进行条形码扫描

来自分类Dev

如何使用Android中的Zxing库从摄像头预览中解码条形码?

来自分类Dev

如何使用AngularJS在页面中附加多个扫描的条形码结果数据

来自分类Dev

在Java中使用Zxing扫描多个条形码

来自分类Dev

如何使用zxing扫描条形码并使用jsonp返回?

来自分类Dev

扫描的条形码很少,但很少使用Zxing

来自分类Dev

使用Javascript进行条形码解码

来自分类Dev

片段中的条形码扫描仪

来自分类Dev

使用蓝牙条形码扫描仪模拟进行测试自动化

来自分类Dev

将Zxing条形码扫描仪集成到我的android应用中

来自分类Dev

在LiveChange事件上进行条形码扫描自动触发

Related 相关文章

  1. 1

    如何将ZXing Library集成到Android Studio进行条形码扫描?

  2. 2

    使用Zxing读取9个扩展条形码中的3个-在Android中

  3. 3

    生成的条形码Zxing Android的大小

  4. 4

    Android使用Zxing生成QR码和条形码

  5. 5

    使用Zbar或Zxing在各个方向进行条形码扫描

  6. 6

    在Java中使用Zxing扫描多个条形码

  7. 7

    在WebView Android中扫描条形码

  8. 8

    扫描条形码时,Zxing库的CaptureActivity没有返回

  9. 9

    在Android应用中使用zxing扫描条形码阅读器数据

  10. 10

    使用Java Zxing API将条形码内容写为条形码下方的标签

  11. 11

    片段中的条形码扫描仪

  12. 12

    Zxing-动态更改扫描的条形码类型

  13. 13

    错误进行条形码扫描的Android Mobile Vision

  14. 14

    条形码扫描损坏

  15. 15

    通过使用React Native,Expo扫描条形码来进行Webview Uri重定向

  16. 16

    使用PyQt5进行条形码扫描

  17. 17

    如何在其他地方使用条形码扫描仪的结果?

  18. 18

    我应该使用新活动还是片段来扫描条形码?

  19. 19

    使用Zbar或Zxing在各个方向进行条形码扫描

  20. 20

    如何使用Android中的Zxing库从摄像头预览中解码条形码?

  21. 21

    如何使用AngularJS在页面中附加多个扫描的条形码结果数据

  22. 22

    在Java中使用Zxing扫描多个条形码

  23. 23

    如何使用zxing扫描条形码并使用jsonp返回?

  24. 24

    扫描的条形码很少,但很少使用Zxing

  25. 25

    使用Javascript进行条形码解码

  26. 26

    片段中的条形码扫描仪

  27. 27

    使用蓝牙条形码扫描仪模拟进行测试自动化

  28. 28

    将Zxing条形码扫描仪集成到我的android应用中

  29. 29

    在LiveChange事件上进行条形码扫描自动触发

热门标签

归档