NativeScript General example
Creating Barkoder Configuration #
The view model, app/main-view-model.ts of an app made and set up as on the Installation page (ns create BarkoderApp --ts, the package, webpack.config.js). It finds the BarkoderView of the page, sets the license key, and calls the methods of the next sections. This configuration includes the license key necessary for validating and activating the SDK.
If the license key is invalid or missing, the scan results are marked (Unlicensed).
import { Observable } from '@nativescript/core'
import { BarkoderConstants } from 'barkoder-nativescript/barkoder-nativescript.common'
export class HelloWorldModel extends Observable implements BarkoderConstants.BarkoderResultCallback {
private barkoderView: any
constructor(args) {
super()
this.barkoderView = args.object.page.getViewById('barkoderView')
this.barkoderView.setLicenseKey("LICENSE_KEY")
this.setBarcodeTypeEnabled()
this.setBarkoderSettings()
}
// the methods of the next sections go here, inside the class
}
The page, app/main-page.xml: the scanner view, a button that starts scanning and two labels for the result (xmlns:Barkoder is the plugin's module):
<Page xmlns="http://schemas.nativescript.org/tns.xsd" xmlns:Barkoder="barkoder-nativescript/barkoder-nativescript.common" navigatingTo="navigatingTo">
<ActionBar title="barKoder" />
<StackLayout>
<Barkoder:BarkoderView id="barkoderView" width="100%" height="70%"/>
<Button text="Start scanning" tap="{{ startScanning }}" />
<Label text="{{ message }}" textWrap="true" />
<Label text="{{ message2 }}" />
</StackLayout>
</Page>
app/main-page.ts gives the view model the page's navigatingTo event:
import { EventData, Page } from '@nativescript/core'
import { HelloWorldModel } from './main-view-model'
export function navigatingTo(args: EventData) {
const page = <Page>args.object
page.bindingContext = new HelloWorldModel(args)
}
Enabling Symbologies #
Here’s a detailed and refined explanation of how you can enable individual symbologies for barcode scanning
private setBarcodeTypeEnabled() : void {
this.barkoderView.setBarcodeTypeEnabled(
[
BarkoderConstants.DecoderType.Ean13,
BarkoderConstants.DecoderType.QR,
BarkoderConstants.DecoderType.Aztec,
BarkoderConstants.DecoderType.AztecCompact,
BarkoderConstants.DecoderType.COOP25,
BarkoderConstants.DecoderType.Codabar,
BarkoderConstants.DecoderType.Code128,
BarkoderConstants.DecoderType.Code11,
BarkoderConstants.DecoderType.Code25,
BarkoderConstants.DecoderType.Code32,
BarkoderConstants.DecoderType.Datalogic25,
BarkoderConstants.DecoderType.Datamatrix,
BarkoderConstants.DecoderType.Code93,
BarkoderConstants.DecoderType.Code39,
BarkoderConstants.DecoderType.Code128,
BarkoderConstants.DecoderType.Msi,
BarkoderConstants.DecoderType.QRMicro
]
)
}
Setting barkoder settings #
The setBarkoderSettings method
Next define a method, setBarkoderSettings, where we will update our settings for the barcode scanner view (barkoderView); the constructor calls it.
In our case we will enable image results and include barcode location within the image, also make the region of interest visible and enable pinch-to-zoom functionality.
Finally, configure the ROI to cover the central 90% of the scanner view.
These configurations enhance the user experience by providing more detailed scan results, interactive zooming, and visual feedback about the scan area.
private setBarkoderSettings(): void {
this.barkoderView.setImageResultEnabled(true)
this.barkoderView.setLocationInImageResultEnabled(true)
this.barkoderView.setRegionOfInterestVisible(true)
this.barkoderView.setPinchToZoomEnabled(true)
this.barkoderView.setRegionOfInterest(5, 5, 90, 90)
}
Starting Scan #
The view model's startScanning method (the page's button calls it) starts the camera; the view model is the callback, so each result comes to its scanningFinished.
startScanning(): void {
this.barkoderView.startScanning(this)
}
Handling Results #
Implementing the BarkoderResultCallback allows your view controller to receive scanning results. The scanningFinished method is called when scanning is complete, providing the results, optional thumbnails, and the captured image.
scanningFinished(results: any[], thumbnails: any[], resultImage: any): void {
this.showResult(results)
}
showResult #
The showResult function updates the UI based on the state of the scanning process and the results obtained: the first result's text and type in the page's labels (message, message2), or that nothing was found (a picture scanned with scanImage can have no barcode, and results[0] would throw).
private showResult(results: any[]): void {
if (results.length === 0) { // a picture without a barcode (scanImage)
this.notifyPropertyChange('message', 'No barcode found')
this.notifyPropertyChange('message2', '')
return
}
console.log(`${results[0].textualData}`)
this.notifyPropertyChange('message', `${results[0].textualData}`)
this.notifyPropertyChange('message2', `${results[0].barcodeTypeName}`)
}