来自片段的呼叫会调用位于另一个片段中的扫描程序。 几天来我一直在尝试合并Zxing库。我终于合并了一个打开扫描仪片段的代码。扫描刮条出现后,什么也没有发生。
我尝试了在Intentet上找到的各种库。还尝试在扫描程序处于活动状态而不是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);
}
MoviesListFragment
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); }
}
答案 0 :(得分:0)
如果该库可以读取条形码,则摄像机的质量可能会有很大的不同。我们使用该库,并且在某些设备上存在扫描问题。另外,某些条形码很难读取。
我认为我们使用的是较旧版本的zxing,但是在我们的片段中,我们实现了此回调,并且扫描程序将在扫描时自动触发
分级版本: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);