扫描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);
}
}
答案 0 :(得分:0)
完成扫描并想返回数据后,添加以下代码
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();
您可以在呼叫活动的onActivityResult
中获取数据。