Embedding a Barcode Scanner in Your App
  • 05 Sep 2024
  • 1 Minute to read
  • Contributors
  • Dark
    Light

Embedding a Barcode Scanner in Your App

  • Dark
    Light

Article summary

If you need a bit more control over barcode scanning, you can also embed a barcode scanner directly within your app. This gives you the flexibility to customize the barcode scanning UI or to filter discovered barcodes before taking action on them. The Barcode SDK provides a ScannerFragment which can be embedded in your app like any other Android fragment. You can embed directly in layout xml using the <fragment> tag or use FragmentManager to dynamically add it to your activity. For example:

import com.vuzix.sdk.barcode.ScannerFragment;
import com.vuzix.sdk.barcode.ScanResult2;
ScannerFragment scannerFragment = new ScannerFragment();
Bundle args = new Bundle();
// specify any scanner args here scannerFragment.setArguments(args);
getFragmentManager().beginTransaction().replace(R.id.fragment_container, scannerFragment).commit();

You can register a listener with ScannerFragment to get callbacks when a successful scan takes place or an error occurs. For example:

scannerFragment.setListener2(new ScannerFragment.Listener2() {
	@Override public void onScan2Result(Bitmap bitmap, ScanResult2[] scanResults) {
		// handle barcode scanning results
	}
	@Override public void onError() {
		// scanner fragment encountered a fatal error and will no longer provide results
	}
});


For more information on using ScannerFragment, including supported arguments, refer to the JavaDoc for the ScannerFragment class.

A sample Android Studio project demonstrating embedding a barcode scanning is available on our Github.


Was this article helpful?