forked from NathanaelA/nativescript-zxing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zxing.ios.js
140 lines (117 loc) · 5 KB
/
zxing.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**********************************************************************************
* (c) 2016-2017, Master Technology
*
* Licensed under the APACHE license or contact me for a Support or Commercial License
*
* I do contract work in most languages, so let me solve your problems!
*
* Any questions please feel free to email me or put a issue up on the github repo
* Version 0.0.4 Nathan@master-technology.com
*********************************************************************************/
"use strict";
/* global ZXBarcodeFormat, ZXMultiFormatWriter, interop, UIImage, ZXImage, ZXCGImageLuminanceSource, ZXBinaryBitmap, ZXHybridBinarizer, ZXDecodeHints, ZXMultiFormatReader */
function NativeZXing() {
if (!this instanceof NativeZXing) { // jshint ignore:line
//noinspection JSValidateTypes
return new NativeZXing();
}
}
NativeZXing.prototype.createBarcode = function(options) {
var encode="NOTHING", width=100, height=100, format = this.QR_CODE;
if (options) {
if (options.encode) {
encode = options.encode;
}
if (options.width) {
width = options.width;
}
if (options.height) {
height = options.height;
}
if (typeof options.format !== "undefined") {
format = options.format;
}
}
var error = new interop.Reference();
var writer = ZXMultiFormatWriter.writer();
var result = writer.encodeFormatWidthHeightError(encode, format, width, height, error);
if (result) {
return UIImage.alloc().initWithCGImage(ZXImage.imageWithMatrix(result).cgimage);
} else {
return error.localizedDescription().toString();
}
};
NativeZXing.prototype.decodeBarcode = function(bitmap, options) {
if (bitmap === null)
{
return null;
}
var source = ZXCGImageLuminanceSource.alloc().initWithCGImage(bitmap);
var hybBitmap = ZXBinaryBitmap.binaryBitmapWithBinarizer(ZXHybridBinarizer.binarizerWithSource(source));
source = null;
var hints = ZXDecodeHints.hints();
if (options && options.formats) {
for (var i=0;i<options.formats.length;i++) {
hints.addPossibleFormat(options.formats[i]);
}
}
if (options && options.tryHarder) {
hints.tryHarder = true;
}
if (options && options.characterSet) {
hints.encoding = options.characterSet;
}
var error = new interop.Reference();
var reader = ZXMultiFormatReader.reader();
try
{
var results = {};
var result = reader.decodeHintsError(hybBitmap, hints, error);
if (result) {
hybBitmap = null;
results.format = this._getBarcodeFormatText(result.barcodeFormat);
results.barcode = result.text.toString();
return results;
} else {
console.log("Err", error.localizedDescription().toString());
}
}
catch (err)
{
// console.log(err);
}
return null;
};
NativeZXing.prototype._getBarcodeFormatText = function(id) {
switch (id) {
case NativeZXing.QR_CODE: return 'QR_CODE';
case NativeZXing.EAN_8: return 'EAN_8';
case NativeZXing.UPC_E: return 'UPC_E';
case NativeZXing.EAN_13: return 'EAN_13';
case NativeZXing.UPC_A: return 'UPC_A';
case NativeZXing.CODE_39: return 'CODE_39';
case NativeZXing.CODE_93: return 'CODE_93';
case NativeZXing.CODE_128: return 'CODE_128';
case NativeZXing.ITF: return 'ITF';
case NativeZXing.PDF_417: return 'PDF_417';
case NativeZXing.CODABAR: return 'CODABAR';
case NativeZXing.DATA_MATRIX: return 'DATA_MATRIX';
case NativeZXing.AZTEC: return 'AZTEC';
default: return 'UNKNOWN';
}
};
// Create Mapping to make this simpler to choose a barcode type.
NativeZXing.QR_CODE = NativeZXing.prototype.QR_CODE = ZXBarcodeFormat.kBarcodeFormatQRCode;
NativeZXing.EAN_8 = NativeZXing.prototype.EAN_8 = ZXBarcodeFormat.kBarcodeFormatEan8;
NativeZXing.UPC_E = NativeZXing.prototype.UPC_E = ZXBarcodeFormat.kBarcodeFormatUPCE;
NativeZXing.EAN_13 = NativeZXing.prototype.EAN_13 = ZXBarcodeFormat.kBarcodeFormatEan13;
NativeZXing.UPC_A = NativeZXing.prototype.UPC_A = ZXBarcodeFormat.kBarcodeFormatUPCA;
NativeZXing.CODE_39 = NativeZXing.prototype.CODE_39 = ZXBarcodeFormat.kBarcodeFormatCode39;
NativeZXing.CODE_93 = NativeZXing.prototype.CODE_93 = ZXBarcodeFormat.kBarcodeFormatCode93;
NativeZXing.CODE_128 = NativeZXing.prototype.CODE_128 = ZXBarcodeFormat.kBarcodeFormatCode128;
NativeZXing.ITF = NativeZXing.prototype.ITF = ZXBarcodeFormat.kBarcodeFormatITF;
NativeZXing.PDF_417 = NativeZXing.prototype.PDF_417 = ZXBarcodeFormat.kBarcodeFormatPDF417;
NativeZXing.CODABAR = NativeZXing.prototype.CODABAR = ZXBarcodeFormat.kBarcodeFormatCodabar;
NativeZXing.DATA_MATRIX = NativeZXing.prototype.DATA_MATRIX = ZXBarcodeFormat.kBarcodeFormatDataMatrix;
NativeZXing.AZTEC = NativeZXing.prototype.AZTEC = ZXBarcodeFormat.kBarcodeFormatAztec;
module.exports = NativeZXing;