Comment on page
Barcode Scanner
With Easy ML Kit's barcode scanning API, you can read data encoded using most standard barcode formats. Barcode scanning happens on the device, and doesn't require a network connection.
Barcodes are a convenient way to pass information from the real world to your app. In particular, when using 2D formats such as QR code, you can encode structured data such as contact information or WiFi network credentials. Because Easy ML Kit can automatically recognize and parse this data, your app can respond intelligently when a user scans a barcode.
Import VoxelBusters.EasyMLKit and VoxelBusters.CoreLibrary namespaces
using VoxelBusters.EasyMLKit;
using VoxelBusters.CoreLibrary;
Create an instance of the BarcodeScanner instance.
private BarcodeScanner CreateBarcodeScanner()
{
scanner = new BarcodeScanner();
return scanner;
}
private IImageInputSource CreateImageInputSource(Texture2D texture)
{
return new ImageInputSource(texture);
}
private IImageInputSource CreateLiveCameraInputSource()
{
IImageInputSource inputSource = new LiveCameraInputSource()
{
EnableFlash = false,
IsFrontFacing = false
};
return inputSource;
}
For preparing, you need to pass on above created Input source along with BarcodeScannerOptions and a callback to know when prepare is complete.
private BarcodeScannerOptions CreateBarcodeScannerOptions()
{
BarcodeScannerOptions.Builder builder = new BarcodeScannerOptions.Builder();
builder.SetScannableFormats(BarcodeFormat.QR_CODE);//BarcodeFormat.QR_ALL;
return builder.Build();
}
private void Prepare()
{
IImageInputSource inputSource = CreateImageInputSource(TEXTURE);
BarcodeScannerOptions options = CreateBarcodeScannerOptions();
Debug.Log("Starting prepare...");
scanner.Prepare(inputSource, options, OnPrepareComplete);
}
private void OnPrepareComplete(BarcodeScanner scanner, Error error)
{
Debug.Log("Prepare complete..." + error);
if (error == null)
{
Debug.Log("Prepare completed successfully!");
}
else
{
Debug.Log("Failed preparing Barcode scanner : " + error.Description);
}
}
Once prepare is complete, you can start processing which gives the result in a callback.
private void OnPrepareComplete(BarcodeScanner scanner, Error error)
{
Debug.Log("Prepare complete..." + error);
if (error == null)
{
Debug.Log("Prepare completed successfully!");
scanner.Process(OnProcessUpdate);
}
else
{
Debug.Log("Failed preparing Barcode scanner : " + error.Description);
}
}
private void OnProcessUpdate(BarcodeScanner scanner, BarcodeScannerResult result)
{
if (!result.HasError())
{
Debug.Log(string.Format("Received {0} Barcodes", result.Barcodes.Count));
foreach (Barcode each in result.Barcodes)
{
Debug.Log(string.Format("Format : {0}, Value Type : {1}, Raw Value : {2}, Bounding Box : {3}", each.Format, each.ValueType, each.RawValue, each.BoundingBox));
}
if(result.Barcodes.Count > 0)
{
scanner.Close(null);
}
}
else
{
Debug.Log("Barcode scanner failed processing : " + result.Error.Description, false);
}
});
Close the scanner once you are done processing.
scanner.Close(null);//Or pass a callback to know once its complete