nativescript-barcodeview
Version:
Scan QR/barcodes with your NativeScript app.
203 lines • 8.51 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var barcodeview_common_1 = require("./barcodeview-common");
var application_1 = require("@nativescript/core/application");
var perms = require("nativescript-perms");
var image_source_1 = require("@nativescript/core/image-source/image-source");
var color_1 = require("@nativescript/core/color");
function nativeFormat(format) {
var NBarcodeFormat = com.google.zxing.BarcodeFormat;
switch (format) {
case 'QR_CODE':
return NBarcodeFormat.QR_CODE;
case 'PDF_417':
return NBarcodeFormat.PDF_417;
case 'AZTEC':
return NBarcodeFormat.AZTEC;
case 'UPC_E':
return NBarcodeFormat.UPC_E;
case 'CODE_39':
return NBarcodeFormat.CODE_39;
case 'CODE_93':
return NBarcodeFormat.CODE_93;
case 'CODE_128':
return NBarcodeFormat.CODE_128;
case 'DATA_MATRIX':
return NBarcodeFormat.DATA_MATRIX;
case 'EAN_8':
return NBarcodeFormat.EAN_8;
case 'ITF':
return NBarcodeFormat.ITF;
// see https://github.com/EddyVerbruggen/nativescript-barcodeview/issues/176
case 'EAN_13':
case 'UPC_A':
return NBarcodeFormat.PDF_417;
}
return null;
}
var getBarcodeTypes = function (formatsString) {
var types = new java.util.ArrayList();
var BarcodeFormat = com.google.zxing.BarcodeFormat;
if (formatsString) {
var formats = formatsString.split(',');
for (var _i = 0, formats_1 = formats; _i < formats_1.length; _i++) {
var format = formats_1[_i];
format = format.trim();
var nFormat = nativeFormat(format);
if (nFormat) {
types.add(nFormat);
}
}
}
else {
types.add(BarcodeFormat.UPC_E);
types.add(BarcodeFormat.CODE_39);
types.add(BarcodeFormat.EAN_8);
types.add(BarcodeFormat.EAN_13);
types.add(BarcodeFormat.CODE_93);
types.add(BarcodeFormat.CODE_128);
types.add(BarcodeFormat.DATA_MATRIX);
types.add(BarcodeFormat.ITF);
types.add(BarcodeFormat.PDF_417);
types.add(BarcodeFormat.QR_CODE);
types.add(BarcodeFormat.AZTEC);
}
return types;
};
var BarcodeView = /** @class */ (function (_super) {
__extends(BarcodeView, _super);
function BarcodeView() {
return _super !== null && _super.apply(this, arguments) || this;
}
BarcodeView.prototype.createNativeView = function () {
return new com.journeyapps.barcodescanner.BarcodeView(this._context);
};
BarcodeView.prototype.onActivityResume = function () {
this.nativeViewProtected.resume();
};
BarcodeView.prototype.onActivityPause = function () {
this.nativeViewProtected.pause();
};
BarcodeView.prototype.initNativeView = function () {
var _this = this;
_super.prototype.initNativeView.call(this);
var barcodeView = this.nativeViewProtected;
this.callback = new com.journeyapps.barcodescanner.BarcodeCallback({
barcodeResult: function (result) {
var text = result.getText();
if (!text) {
return;
}
// console.log('barcodeResult', text, this.reportDuplicates, this.lastText)
if (!_this.reportDuplicates && text === _this.lastText) {
// Prevent duplicate scans
return;
}
_this.lastText = text;
if (BarcodeView.beepManager && _this.beepOnScan) {
BarcodeView.beepManager.playBeepSound();
}
_this.notify({
eventName: barcodeview_common_1.BarcodeView.scanResultEvent,
object: _this,
format: result.getBarcodeFormat().toString(),
text: result.getText()
});
},
possibleResultPoints: function (param0) { }
});
barcodeView.decodeContinuous(this.callback);
application_1.android.on('onActivityResumed', this.onActivityResume, this);
application_1.android.on('onActivityPaused', this.onActivityPause, this);
if (!this.formats) {
var types = getBarcodeTypes(null);
barcodeView.setDecoderFactory(new com.journeyapps.barcodescanner.DefaultDecoderFactory(types));
}
if (!this.pause) {
this.resumeScanning();
}
};
BarcodeView.prototype.disposeNativeView = function () {
var barcodeView = this.nativeViewProtected;
barcodeView.decodeContinuous(null);
barcodeView.stopDecoding();
barcodeView.pause();
this.callback = null;
// this.beepManager = null;
application_1.android.off('onActivityResumed', this.onActivityResume, this);
application_1.android.off('onActivityPaused', this.onActivityPause, this);
_super.prototype.disposeNativeView.call(this);
};
BarcodeView.prototype.pauseScanning = function () {
this.nativeViewProtected.pause();
};
BarcodeView.prototype.resumeScanning = function () {
var _this = this;
perms
.request('camera')
.then(function (r) { return r[0] === 'authorized'; })
.then(function (r) {
if (r) {
_this.nativeViewProtected.resume();
}
});
};
BarcodeView.prototype.triggerScan = function (view) {
this.nativeViewProtected.decodeSingle(this.callback);
};
BarcodeView.prototype[barcodeview_common_1.formatsProperty.setNative] = function (value) {
this.formats = value;
var types = getBarcodeTypes(value);
this.nativeViewProtected.setDecoderFactory(new com.journeyapps.barcodescanner.DefaultDecoderFactory(types));
};
BarcodeView.prototype[barcodeview_common_1.torchOnProperty.setNative] = function (value) {
this.nativeViewProtected.setTorch(value);
};
BarcodeView.prototype[barcodeview_common_1.preferFrontCameraProperty.setNative] = function (value) {
this.preferFrontCamera = value;
var barcodeView = this.nativeViewProtected;
var settings = barcodeView.getCameraSettings();
if (barcodeView.isPreviewActive()) {
barcodeView.pause();
}
//swap the id of the camera to be used
if (value && settings.getRequestedCameraId() == android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK) {
settings.setRequestedCameraId(android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
}
else if (!value && settings.getRequestedCameraId() == android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT) {
settings.setRequestedCameraId(android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK);
}
barcodeView.setCameraSettings(settings);
barcodeView.resume();
};
BarcodeView.prototype[barcodeview_common_1.beepOnScanProperty.setNative] = function (value) {
if (value && !BarcodeView.beepManager) {
BarcodeView.beepManager = new com.google.zxing.client.android.BeepManager(this._context);
}
};
BarcodeView.prototype[barcodeview_common_1.pauseProperty.setNative] = function (value) {
value ? this.pauseScanning() : this.resumeScanning();
};
return BarcodeView;
}(barcodeview_common_1.BarcodeView));
exports.BarcodeView = BarcodeView;
function generateBarCode(_a) {
var text = _a.text, type = _a.type, width = _a.width, height = _a.height, frontColor = _a.frontColor, backColor = _a.backColor;
var barcodeEncoder = new com.nativescript.barcodeview.BarcodeEncoder();
if (frontColor) {
var color = frontColor instanceof color_1.Color ? frontColor : new color_1.Color(frontColor);
barcodeEncoder.frontColor = color.android;
}
if (backColor) {
var color = backColor instanceof color_1.Color ? backColor : new color_1.Color(backColor);
barcodeEncoder.backColor = color.android;
}
var nFormat = nativeFormat(type);
if (nFormat) {
var bitmap = barcodeEncoder.encodeBitmap(text, nFormat, width, height);
return new image_source_1.ImageSource(bitmap);
}
return null;
}
exports.generateBarCode = generateBarCode;
//# sourceMappingURL=barcodeview.android.js.map