nativescript-dynamsoft-barcode-reader
Version:
Nativescript plugin for Dynamsoft Barcode Reader
111 lines • 4.47 kB
JavaScript
import { BarcodeReaderCommon } from './common';
var ScanHandler = /** @class */ (function () {
function ScanHandler() {
}
ScanHandler.prototype.onScanned = function (results) { };
return ScanHandler;
}());
var DecodingRunnable = /** @class */ (function (_super) {
__extends(DecodingRunnable, _super);
function DecodingRunnable(reader, frame, handler) {
var _this = _super.call(this) || this;
_this.dbr = reader;
_this.frame = frame;
_this.handler = handler;
return global.__native(_this);
}
DecodingRunnable.prototype.run = function () {
console.log("run decode");
var results = this.dbr.decodeBuffer(this.frame.getImageData(), this.frame.getWidth(), this.frame.getHeight(), this.frame.getStrides()[0], this.frame.getPixelFormat());
this.handler.onScanned(results);
};
return DecodingRunnable;
}(java.lang.Runnable));
export class BarcodeReader extends BarcodeReaderCommon {
constructor() {
super();
this.dbr = new com.dynamsoft.dbr.BarcodeReader();
}
initLicense(license, listener) {
com.dynamsoft.dbr.BarcodeReader.initLicense(license, new com.dynamsoft.dbr.DBRLicenseVerificationListener({
DBRLicenseVerificationCallback: function (isSuccessful, exception) {
if (listener) {
listener(isSuccessful, exception);
}
}
}));
}
initRuntimeSettingsWithString(template) {
this.dbr.initRuntimeSettingsWithString(template, com.dynamsoft.dbr.EnumConflictMode.CM_OVERWRITE);
}
decodeFrameAsync(frame) {
let pThis = this;
return new Promise(function (resolve, reject) {
let handler = new ScanHandler();
handler.onScanned = function (results) {
resolve(pThis.wrapResult(results));
};
let runnable = new DecodingRunnable(pThis.dbr, frame, handler);
let decodingThread = new java.lang.Thread(runnable);
decodingThread.start();
});
}
decodeFrame(frame) {
let results = this.dbr.decodeBuffer(frame.getImageData(), frame.getWidth(), frame.getHeight(), frame.getStrides()[0], frame.getPixelFormat());
return this.wrapResult(results);
}
decodeBitmap(bitmap) {
let results = this.dbr.decodeBufferedImage(bitmap);
return this.wrapResult(results);
}
decodeFile(file) {
let results = this.dbr.decodeFile(file);
return this.wrapResult(results);
}
decodeBase64(base64) {
let bitmap = this.base642Bitmap(base64);
return this.decodeBitmap(bitmap);
}
wrapResult(results) {
let textResults = [];
for (let index = 0; index < results.length; index++) {
const result = results[index];
let textResult = {
barcodeText: result.barcodeText,
barcodeFormat: result.barcodeFormatString,
x1: result.localizationResult.resultPoints[0].x,
x2: result.localizationResult.resultPoints[1].x,
x3: result.localizationResult.resultPoints[2].x,
x4: result.localizationResult.resultPoints[3].x,
y1: result.localizationResult.resultPoints[0].y,
y2: result.localizationResult.resultPoints[1].y,
y3: result.localizationResult.resultPoints[2].y,
y4: result.localizationResult.resultPoints[3].y
};
textResults.push(textResult);
}
return textResults;
}
base642Bitmap(base64) {
let decode = android.util.Base64.decode(base64, android.util.Base64.DEFAULT);
return android.graphics.BitmapFactory.decodeByteArray(decode, 0, decode.length);
}
setCameraEnhancer(dce) {
this.dbr.setCameraEnhancer(dce);
}
startScanning() {
this.dbr.startScanning();
}
stopScanning() {
this.dbr.stopScanning();
}
setTextResultListener(listener) {
let pThis = this;
this.dbr.setTextResultListener(new com.dynamsoft.dbr.TextResultListener({
textResultCallback: function (id, imageData, textResults) {
listener(pThis.wrapResult(textResults));
}
}));
}
}
//# sourceMappingURL=index.android.js.map