【发布时间】:2018-05-14 19:29:43
【问题描述】:
条形码扫描仪读取条形码并显示在控制台日志上,但如何使其在 Google Book API 上查找图书。书名、作者和出版年份等图书详细信息应显示在不同的页面上。这是我关注的插件:https://github.com/EddyVerbruggen/nativescript-barcodescanner
这是我的 hardcopy-view-model.js 文件
var observable_1 = require("data/observable");
var dialogs_1 = require("ui/dialogs");
var view = require("ui/core/view");
var nativescript_barcodescanner = require("nativescript-barcodescanner");
const httpModule = require("http");
var BarCodeModel = (function (_super) {
__extends(BarCodeModel, _super);
function BarCodeModel() {
_super.call(this);
this.barcodeScanner = new nativescript_barcodescanner.BarcodeScanner();
}
BarCodeModel.prototype.doCheckHasCameraPermission = function () {
this.barcodeScanner.hasCameraPermission().then(function (permitted) {
dialogs_1.alert({
title: "Has Camera permission?",
message: permitted ? "YES" : "NO",
okButtonText: "OK"
});
}, function (err) {
dialogs_1.alert(err);
});
};
BarCodeModel.prototype.doScanWithTorch = function () {
this.scan(false, true, true, "landscape");
};
;
BarCodeModel.prototype.doScanLandscape = function () {
this.scan(false, true, true, "landscape");
};
;
BarCodeModel.prototype.scan = function () {
this.barcodeScanner.scan({
cancelLabel: "EXIT. Also, try the volume buttons!",
cancelLabelBackgroundColor: "#333333",
message: "Tap the bulb button or Use the volume button for turning on
light",
showFlipCameraButton: false,
preferFrontCamera: false,
showTorchButton: true,
beepOnScan: true,
torchOn: false,
closeCallback: function () { console.log("Scanner closed"); },
resultDisplayDuration: 500,
orientation: "landscape",
openSettingsIfPermissionWasPreviouslyDenied: true
}).then(
function(result) {
console.log("---- scanned " +result.text);
httpModule.request({
url: "https://www.googleapis.com/books/v1/volumes?
q=isbn:"+result,
method: "GET"
}).then((response) => {
var obj = response.content.toJSON();
console.log("Book: " +obj);
console.log(JSON.parse(response));
alert({
title:"Scan Result",
message: "Barcode Format: " + result.format + "\nCode: " +
result.text + "\nBook Title: " + result.response,
okButtonText:"OK"
});
});
},
function(error) {
console.log("No scan: " + error);
});
};
;
return BarCodeModel;
}(observable_1.Observable));
exports.BarCodeModel = BarCodeModel;
【问题讨论】:
-
@RandyCasburn,它应该是这样吗:httpModule.request({ url: "googleapis.com/books/v1/...; method: "GET"..... 是我的 httpRequest代码正确吗?
-
那么您从 Google 图书中得到了什么?这是一个可以正常工作的有效搜索示例:googleapis.com/books/v1/volumes?q=isbn:9781620331538 - 这让我认为“结果”的内容不是有效的 ISBN。
-
@RandyCasburn,结果的内容是 9781416900955,我扫描了一本书。但输出仅在控制台 JS 中显示:Scanner closed JS: ----scanned 9781416900955
-
我希望它在警报中显示标题、作者、发布日期和页数。
标签: javascript android google-api nativescript barcode-scanner