【问题标题】:Custom QrCode Scanner after scanning the app is getting close in fragment扫描应用程序后的自定义 QrCode 扫描仪在片段中越来越接近
【发布时间】:2019-03-04 11:19:40
【问题描述】:

扫描 QrCode 后没有得到任何结果,在父活动中也实现了 onActivityResult 但扫描后应用程序关闭

这是我的代码

implementation 'com.journeyapps:zxing-android-embedded:3.5.0'


public class TestFragment extends Fragment implements  DecoratedBarcodeView.TorchListener{

    private static final int SELECT_PHOTO = 100;
    public String barcode;
    private CaptureManager capture;
    private DecoratedBarcodeView barcodeScannerView;
    private ImageView switchFlashlightButton;
    private boolean isFlashLightOn = false;

    View TestView;
    private Context context;

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



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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        TestView= inflater.inflate(R.layout.fragment_test, container, false);

        barcodeScannerView = TestView.findViewById(R.id.zxing_barcode_scanner);

        barcodeScannerView.setTorchListener(this);


        switchFlashlightButton = (ImageView) TestView.findViewById(R.id.switch_flashlight);

        if (!hasFlash()) {
            switchFlashlightButton.setVisibility(View.GONE);
        } else {
            switchFlashlightButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switchFlashlight();
                }
            });
        }


        //start capture
        capture = new CaptureManager((Activity) context, barcodeScannerView);
        capture.initializeFromIntent(getActivity().getIntent(), savedInstanceState);
        capture.decode();



        return TestView;
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
        capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


    // TODO: Rename method, update argument and hook method into UI event

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;

    }

    @Override
    public void onDetach() {
        super.onDetach();

    }

    @Override
    public void onTorchOn() {

    }

    @Override
    public void onTorchOff() {

    }

    private boolean hasFlash() {
        return getApplicationContext().getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }

    public void switchFlashlight() {
        if (isFlashLightOn) {
            barcodeScannerView.setTorchOff();
            isFlashLightOn = false;
        } else {
            barcodeScannerView.setTorchOn();
            isFlashLightOn = true;
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        capture.onResume();
    }

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

    @Override
    public void onDestroy() {
        super.onDestroy();
        capture.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        capture.onSaveInstanceState(outState);
    }


}

【问题讨论】:

  • 请发布一些堆栈跟踪
  • 扫描后没有任何错误。
  • 你解决了吗?

标签: android qr-code zxing


【解决方案1】:

当您完成扫描并想要返回数据时添加以下代码

Intent data = new Intent();
String text = "Result to be returned...."
//---set the data to pass back---
data.setData(Uri.parse(text));
setResult(RESULT_OK, data);
//---close the activity---
finish();

这样就可以获取到调用activity的onActivityResult中的数据了。

【讨论】:

    猜你喜欢
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 2012-05-21
    相关资源
    最近更新 更多