【问题标题】:A qr scannig Fragment appears but no scan occurs出现 qr 扫描 Fragment 但未进行扫描
【发布时间】:2019-05-17 18:24:11
【问题描述】:

来自 Fragment 的调用调用另一个 Fragment 中的扫描仪。 几天来,我一直在尝试合并 Zxing 库。我终于合并了一个打开扫描仪片段的代码。一旦出现扫描屏幕,什么都不会发生。

我尝试了在意图上找到的各种库。当扫描仪位于 Activity 而不是 Fragmnet 时,还尝试从 Fragment 调用扫描仪。

ScannerFragment

public class ScannerFragment extends Fragment {

    ScanResultReceiver resultCallback;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            resultCallback = (ScanResultReceiver) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement ScanResultReceiver");
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        IntentIntegrator integrator = IntentIntegrator.forSupportFragment(ScannerFragment.this);

//        IntentIntegrator integrator = new IntentIntegrator(getActivity());
        integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
        integrator.setPrompt("Scan a barcode");
        integrator.setCameraId(0);  // Use a specific camera of the device
//        integrator.setResultDisplayDuration(0);
        integrator.initiateScan();
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        //retrieve scan result
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        ScanResultReceiver parentActivity = (ScanResultReceiver) this.getActivity();

        if (scanningResult != null) {
            //we have a result
            String codeContent = scanningResult.getContents();
            String codeFormat = scanningResult.getFormatName();
            // send received data
            if (parentActivity != null) {
                parentActivity.scanResultData(codeFormat,codeContent);
            }
        }else{
            // send exception
            if (parentActivity != null) {
                String noResultErrorMsg = "No scan data received!";
                parentActivity.scanResultData(new NoScanResultException(noResultErrorMsg));
            }
        }
    }

}

ScanningResultReceiver

public interface ScanResultReceiver {

    public void scanResultData(String codeFormat, String codeContent);

    public void scanResultData(NoScanResultException noScanData);
}

电影列表片段

public class MoviesListFragment extends Fragment implements ScanResultReceiver {

    private TextView formatTxt, contentTxt;

    Context context;

    MoviesAdapter moviesAdapter;
    RecyclerView moviesRV;

    FragmentManager fm;
    private FragmentTransaction ft;

    Button addBtn;

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_list_movies2, container, false);

        Log.i(TAG,"In movieListFragment");

        context = getActivity();

        addBtn = rootView.findViewById(R.id.addBtnId);

        formatTxt = rootView.findViewById(R.id.scan_format);
        contentTxt = rootView.findViewById(R.id.scan_content);

        Bundle listBundle = getArguments();
        if (listBundle != null) {

            ArrayList<Movie> moviesList = getArguments().getParcelableArrayList("moviesList");
            if (moviesList != null) {

                moviesAdapter = new MoviesAdapter(context, fm);

                moviesRV = rootView.findViewById(R.id.moviesRVId);
                moviesRV.setLayoutManager(new LinearLayoutManager(context));
                moviesRV.setHasFixedSize(true);
                moviesRV.setAdapter(moviesAdapter);

                moviesAdapter.attachMoviesList(moviesList);
            }
        }

        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.i("Monitoring", "Going to ScannerFragment");

                Fragment ScannerFragment = new ScannerFragment();
                fm = getFragmentManager();
                if (fm != null) {
                    ft = fm.beginTransaction();
                }
                ft.replace(R.id.fragments_container, ScannerFragment);
                ft.addToBackStack(null) // add to back stack
                .commit();
            }
        });
        return rootView;
    }

    @Override
    public void scanResultData(String codeFormat, String codeContent){
        // display it on screen
        formatTxt.setText("FORMAT: " + codeFormat);
        contentTxt.setText("CONTENT: " + codeContent);
    }

    @Override
    public void scanResultData(NoScanResultException noScanData) {
        Toast toast = Toast.makeText(context,noScanData.getMessage(), Toast.LENGTH_SHORT);
        toast.show();
    }


}

NoScanResultException

public class NoScanResultException extends Exception {
    public NoScanResultException() {}
    public NoScanResultException(String msg) { super(msg); }
    public NoScanResultException(Throwable cause) { super(cause); }
    public NoScanResultException(String msg, Throwable cause) { super(msg, cause); }
}

【问题讨论】:

  • 扫描程序是否需要在您的 AndroidManifest 中声明特定权限?
  • 我想知道您是否在 Activity 中进行了调试。 onActivityResult() 它可能会在扫描时到达那里,但不是它的片段实现。您可能需要调用活动一中的片段版本。
  • 我使用权限。我不知道要调试什么,因为没有错误。反正我会试试的。

标签: android qr-code zxing barcode-scanner


【解决方案1】:

相机的质量对图书馆是否可以读取条形码有很大影响。我们使用该库,并且在某些设备上存在扫描问题。某些条形码也很难阅读。

我认为我们使用的是旧版本的 zxing,但在我们的片段中,我们实现了这个回调,扫描器将在扫描时自动触发

Gradle 版本:implementation 'me.dm7.barcodescanner:zxing:1.9.3'

public ClassName implements ZXingScannerView.ResultHandler {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup c, Bundle b) {
        mContentView = inflater.inflate(R.layout.scan, null);

        mScannerView = new ZXingScannerView(getActivity());
        List<BarcodeFormat> formats = new ArrayList<BarcodeFormat>();
        formats.add(BarcodeFormat.UPC_A);
        formats.add(BarcodeFormat.CODE_39);
        mScannerView.setFormats(formats);
        contentContainer.addView(mScannerView, 0);
    }

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

    @Override
    public void handleResult(Result result) {
        // handle the result
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();
    }
}

如果您需要继续扫描,您可以使用: mScannerView.resumeCameraPreview(ScanBarcodeFragment.this);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 2016-08-24
    • 1970-01-01
    • 2020-02-25
    • 2012-08-08
    • 1970-01-01
    相关资源
    最近更新 更多