- Print
- DarkLight
Embedding a Barcode Scanner in your app
Embedding a Barcode Scanner in your app
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 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, such as:
// String barcodeTypes[] = {BarcodeType2.CODE_128.name()};
// args.putStringArray(ScannerFragment.ARG_BARCODE2_TYPES, barcodeTypes);
// or:
// args.putBoolean(ScannerFragment.ARG_ZOOM_IN_MODE, true);
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 barcode scanning is available to download here.