UNPKG

nativescript-barcodeview

Version:
284 lines 12.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var barcodeview_common_1 = require("./barcodeview-common"); var perms = require("nativescript-perms"); var image_source_1 = require("@nativescript/core/image-source/image-source"); var color_1 = require("@nativescript/core/color"); var BarcodeView = /** @class */ (function (_super) { __extends(BarcodeView, _super); function BarcodeView() { var _this = _super.call(this) || this; _this._hasSupport = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) !== null; if (_this._hasSupport) { if (typeof AVAudioSession.sharedInstance().setCategoryModeOptionsError === 'function') { // if music was playing, it would stop unless we do this: AVAudioSession.sharedInstance().setCategoryModeOptionsError(AVAudioSessionCategoryPlayback, AVAudioSessionModeDefault, 1 /* MixWithOthers */); } } return _this; } BarcodeView.prototype.createNativeView = function () { var v = _super.prototype.createNativeView.call(this); var types = getBarcodeTypes(this.formats); this._reader = QRCodeReader.readerWithMetadataObjectTypes(types); if (this._hasSupport) { v.layer.insertSublayerAtIndex(this._reader.previewLayer, 0); } return v; }; BarcodeView.prototype.disposeNativeView = function () { this._reader = null; this._player = null; this.lastText = null; // this._scanner.delegate = null; // this._scanner = null; }; BarcodeView.prototype.initNativeView = function () { var _this = this; _super.prototype.initNativeView.call(this); this._reader.setCompletionWithBlock(function (text, format) { if (!_this.reportDuplicates && text === _this.lastText) { // Prevent duplicate scans return; } _this.lastText = text; if (_this.beepOnScan) { if (!_this._player) { var beepPath = NSBundle.mainBundle.pathForResourceOfType('beep', 'caf'); if (beepPath) { _this._player = AVAudioPlayer.alloc().initWithContentsOfURLError(NSURL.fileURLWithPath(beepPath)); _this._player.numberOfLoops = 1; _this._player.volume = 0.7; // this is not the actual volume, as that really depends on the device volume _this._player.prepareToPlay(); } } _this._player && _this._player.play(); } var barcodeFormat = getBarcodeFormat(format); if (shouldReturnEAN13AsUPCA(barcodeFormat, text, _this.formats)) { barcodeFormat = 'UPC_A'; text = text.substring(1); } _this.notify({ eventName: barcodeview_common_1.BarcodeView.scanResultEvent, object: _this, format: getBarcodeFormat(format), text: text }); }); if (!this.pause) { this.resumeScanning(); } }; BarcodeView.prototype.onLayout = function (left, top, right, bottom) { _super.prototype.onLayout.call(this, left, top, right, bottom); if (this._hasSupport && this.nativeViewProtected && this._reader) { this._reader.previewLayer.frame = this.nativeViewProtected.layer.bounds; } }; BarcodeView.prototype.pauseScanning = function () { if (this._reader && this._reader.running()) { this._reader.stopScanning(); } }; BarcodeView.prototype.resumeScanning = function () { var _this = this; if (this._reader && !this._reader.running()) { perms .request('camera') .then(function (r) { return r[0] === 'authorized'; }) .then(function (r) { if (r) { _this._reader.startScanning(); } }) .catch(function (err) { console.error(err); setTimeout(function () { throw err; }, 0); }); } }; BarcodeView.prototype.triggerScan = function (view) { // this.nativeViewProtected.decodeSingle(this.callback); }; BarcodeView.prototype[barcodeview_common_1.pauseProperty.setNative] = function (value) { value ? this.pauseScanning() : this.resumeScanning(); }; BarcodeView.prototype[barcodeview_common_1.torchOnProperty.setNative] = function (value) { this._reader.setTorch(value); }; BarcodeView.prototype[barcodeview_common_1.formatsProperty.setNative] = function (value) { console.log('formatsProperty', value); // if (this.initFormats !== null && this.initFormats !== value) { // this.initFormats = null; // } var types = getBarcodeTypes(value); this._reader.setMetadataObjectTypes(types); }; BarcodeView.prototype[barcodeview_common_1.preferFrontCameraProperty.setNative] = function (value) { this._reader.switchDeviceInput(); }; return BarcodeView; }(barcodeview_common_1.BarcodeView)); exports.BarcodeView = BarcodeView; var shouldReturnEAN13AsUPCA = function (barcodeFormat, value, requestedFormats) { return barcodeFormat === 'EAN_13' && value.indexOf('0') === 0; // why not add the line below? Well, see https://github.com/EddyVerbruggen/nativescript-barcodeview/issues/176 // && (!requestedFormats || requestedFormats.indexOf("UPC_A") > -1); }; function nativeFormat(format) { switch (format) { case 'QR_CODE': return AVMetadataObjectTypeQRCode; case 'PDF_417': return AVMetadataObjectTypePDF417Code; case 'AZTEC': return AVMetadataObjectTypeAztecCode; case 'UPC_E': return AVMetadataObjectTypeUPCECode; case 'CODE_39': return AVMetadataObjectTypeCode39Code; case 'CODE_39_MOD_43': return AVMetadataObjectTypeCode39Mod43Code; case 'CODE_93': return AVMetadataObjectTypeCode93Code; case 'CODE_128': return AVMetadataObjectTypeCode128Code; case 'DATA_MATRIX': return AVMetadataObjectTypeDataMatrixCode; case 'EAN_8': return AVMetadataObjectTypeEAN8Code; case 'ITF': return AVMetadataObjectTypeITF14Code; case 'INTERLEAVED_2_OF_5': return AVMetadataObjectTypeInterleaved2of5Code; // see https://github.com/EddyVerbruggen/nativescript-barcodeview/issues/176 case 'EAN_13': case 'UPC_A': return AVMetadataObjectTypePDF417Code; } return null; } var getBarcodeFormat = function (nativeFormat) { if (nativeFormat === AVMetadataObjectTypeQRCode) return 'QR_CODE'; else if (nativeFormat === AVMetadataObjectTypePDF417Code) return 'PDF_417'; else if (nativeFormat === AVMetadataObjectTypeAztecCode) return 'AZTEC'; else if (nativeFormat === AVMetadataObjectTypeUPCECode) return 'UPC_E'; else if (nativeFormat === AVMetadataObjectTypeCode39Code) return 'CODE_39'; else if (nativeFormat === AVMetadataObjectTypeCode39Mod43Code) return 'CODE_39_MOD_43'; else if (nativeFormat === AVMetadataObjectTypeCode93Code) return 'CODE_93'; else if (nativeFormat === AVMetadataObjectTypeCode128Code) return 'CODE_128'; else if (nativeFormat === AVMetadataObjectTypeDataMatrixCode) return 'DATA_MATRIX'; else if (nativeFormat === AVMetadataObjectTypeEAN8Code) return 'EAN_8'; else if (nativeFormat === AVMetadataObjectTypeITF14Code) return 'ITF'; else if (nativeFormat === AVMetadataObjectTypeEAN13Code) return 'EAN_13'; else if (nativeFormat === AVMetadataObjectTypeInterleaved2of5Code) return 'INTERLEAVED_2_OF_5'; else { console.log('Unknown format scanned: ' + nativeFormat + ', please report this at https://github.com/EddyVerbruggen/nativescript-barcodeview/issues'); return nativeFormat; } }; var getBarcodeTypes = function (formatsString) { var types = []; 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.push(nFormat); } } } else { types.push(AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypeDataMatrixCode, AVMetadataObjectTypeITF14Code, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode, AVMetadataObjectTypeInterleaved2of5Code); } return types; }; function generateBarCode(_a) { var text = _a.text, type = _a.type, width = _a.width, height = _a.height, frontColor = _a.frontColor, backColor = _a.backColor; var data = NSString.stringWithString(text).dataUsingEncoding(NSISOLatin1StringEncoding); var generatorKey; switch (type) { case 'QR_CODE': generatorKey = 'CIQRCodeGenerator'; break; case 'PDF_417': generatorKey = 'CIPDF417BarcodeGenerator'; break; case 'AZTEC': generatorKey = 'CIAztecCodeGenerator'; break; case 'UPC_E': case 'CODE_39': case 'CODE_39_MOD_43': case 'CODE_93': case 'EAN_13': case 'EAN_8': case 'UPC_A': case 'CODE_128': generatorKey = 'CICode128BarcodeGenerator'; break; } if (generatorKey) { var filter = CIFilter.filterWithName(generatorKey); if (filter) { filter.setValueForKey(data, 'inputMessage'); // filter.setValueForKey('H', 'inputCorrectionLevel'); var output = filter.outputImage; if (output) { if (frontColor || backColor) { //change qrcode color : #1e3259 var filterFalseColor = CIFilter.filterWithName('CIFalseColor'); filterFalseColor.setDefaults(); filterFalseColor.setValueForKey(output, 'inputImage'); // convert method if (frontColor) { var color = frontColor instanceof color_1.Color ? frontColor : new color_1.Color(frontColor); filterFalseColor.setValueForKey(CIColor.colorWithCGColor(color.ios.CGColor), 'inputColor0'); } else { filterFalseColor.setValueForKey(CIColor.colorWithString('black'), 'inputColor0'); } if (backColor) { var color = backColor instanceof color_1.Color ? backColor : new color_1.Color(backColor); filterFalseColor.setValueForKey(CIColor.colorWithCGColor(color.ios.CGColor), 'inputColor1'); } else { filterFalseColor.setValueForKey(CIColor.colorWithString('white'), 'inputColor1'); } output = filterFalseColor.outputImage; } var scaleX = width / output.extent.size.width; var scaleY = height / output.extent.size.height; output = output.imageByApplyingTransform(CGAffineTransformMakeScale(scaleX, scaleY)); var context = CIContext.context(); if (context) { // if we create the UIImage directly from the CIImage then it wont be drawn correctly in UIImageView // the contentMode won't be respected, and it won't even draw with SDAnimatedImageView var CGImage = context.createCGImageFromRect(output, output.extent); return new image_source_1.ImageSource(UIImage.imageWithCGImageScaleOrientation(CGImage, UIScreen.mainScreen.scale, 0 /* Up */)); } } } } return null; } exports.generateBarCode = generateBarCode; //# sourceMappingURL=barcodeview.ios.js.map