Skip to content

Commit

Permalink
UPC_A support on iOS? #176
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Jan 10, 2019
1 parent 3672a54 commit 241411a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog

## [3.0.0](https://github.com/EddyVerbruggen/nativescript-barcodescanner/tree/3.0.0) (2019-01-10)
## [2.8.0](https://github.com/EddyVerbruggen/nativescript-barcodescanner/tree/2.8.0) (2019-01-10)
[Full Changelog](https://github.com/EddyVerbruggen/nativescript-barcodescanner/milestone/33?closed=1)

> Breaking change: on iOS we can now distinguish UPC_A codes from EAN_13. Formerly UPC_A was reported as EAN_13 and had a leading '0' (which has now been removed).
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ npm run demo-ng.ios (or demo.ios.device)
* ITF (also known as ITF14)
* PDF_417 (on Android only when passed in explicity via `formats`)
* QR_CODE
* UPC_A
* UPC_A (on iOS only when passed in explicitly via `formats`, see [#176](https://github.com/EddyVerbruggen/nativescript-barcodescanner/issues/176))
* UPC_E

### Android only
Expand Down
4 changes: 2 additions & 2 deletions demo-ng/app/item/item-detail.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<Label class="h2" [text]="item.id + '. '"></Label>
<Label class="h2" [text]="item.name"></Label>
</FlexboxLayout>
<Label class="h4" [text]="item.role"></Label>
<Label class="h4" text="Check the console log for scanned barcodes"></Label>

<BarcodeScanner
height="300"
formats="QR_CODE, EAN_13"
formats="QR_CODE, EAN_13, UPC_A"
beepOnScan="true"
reportDuplicates="true"
preferFrontCamera="false"
Expand Down
18 changes: 14 additions & 4 deletions src/barcodescanner.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class BarcodeScannerView extends BarcodeScannerBaseView {
this.beepOnScan,
true,
this.reportDuplicates,
this.formats,
(text: string, format: string) => {
that.notify({
eventName: BarcodeScannerBaseView.scanResultEvent,
Expand Down Expand Up @@ -217,6 +218,7 @@ export class BarcodeScanner {
arg.beepOnScan !== false,
isContinuous,
arg.reportDuplicates,
arg.formats,
(text: string, format: string) => {
// invoke the callback / promise
if (text === undefined) {
Expand All @@ -228,7 +230,7 @@ export class BarcodeScanner {
let barcodeFormat = getBarcodeFormat(format);
let value = text;

if (barcodeFormat === "EAN_13" && value.indexOf("0") === 0) {
if (shouldReturnEAN13AsUPCA(barcodeFormat, value, arg.formats)) {
barcodeFormat = "UPC_A";
value = value.substring(1);
}
Expand Down Expand Up @@ -272,6 +274,12 @@ export class BarcodeScanner {
}
}

const shouldReturnEAN13AsUPCA = (barcodeFormat: BarcodeFormat, value: string, requestedFormats?: string): boolean => {
return barcodeFormat === "EAN_13" &&
value.indexOf("0") === 0 &&
(!requestedFormats || requestedFormats.indexOf("UPC_A") > -1);
};

const getBarcodeFormat = (nativeFormat: string): BarcodeFormat => {
if (nativeFormat === AVMetadataObjectTypeQRCode) return "QR_CODE";
else if (nativeFormat === AVMetadataObjectTypePDF417Code) return "PDF_417";
Expand All @@ -291,7 +299,7 @@ const getBarcodeFormat = (nativeFormat: string): BarcodeFormat => {
}
};

const getBarcodeTypes = (formatsString: string) => {
const getBarcodeTypes = (formatsString: string): Array<string> => {
const types = [];
if (formatsString) {
let formats = formatsString.split(",");
Expand Down Expand Up @@ -335,14 +343,16 @@ class QRCodeReaderDelegateImpl extends NSObject implements QRCodeReaderDelegate
private _beepOnScan: boolean;
private _isContinuous: boolean;
private _reportDuplicates: boolean;
private _requestedFormats: string;
private _scannedArray: Array<string>;
private _player: AVAudioPlayer;
// initializing this value may prevent recognizing too quickly
private _lastScanResultTs: number = new Date().getTime();

public setCallback(beepOnScan: boolean, isContinuous: boolean, reportDuplicates: boolean, callback: (text?: string, format?: string) => void): void {
public setCallback(beepOnScan: boolean, isContinuous: boolean, reportDuplicates: boolean, requestedFormats: string, callback: (text?: string, format?: string) => void): void {
this._isContinuous = isContinuous;
this._reportDuplicates = reportDuplicates;
this._requestedFormats = requestedFormats;
this._callback = callback;
this._beepOnScan = beepOnScan;
if (this._beepOnScan) {
Expand All @@ -366,7 +376,7 @@ class QRCodeReaderDelegateImpl extends NSObject implements QRCodeReaderDelegate
let barcodeFormat = getBarcodeFormat(type);
let value = result;

if (barcodeFormat === "EAN_13" && value.indexOf("0") === 0) {
if (shouldReturnEAN13AsUPCA(barcodeFormat, value, this._requestedFormats)) {
barcodeFormat = "UPC_A";
value = value.substring(1);
}
Expand Down

0 comments on commit 241411a

Please sign in to comment.