nativescript-barcodescanner
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -1,1 +0,23 @@ | ||
var barcodescanner = {}; | ||
var barcodescanner = {}; | ||
// a few default implementations because not all platforms provide them | ||
barcodescanner.available = function () { | ||
return new Promise(function (resolve) { | ||
resolve(true); | ||
}); | ||
}; | ||
barcodescanner.hasCameraPermission = function () { | ||
return new Promise(function (resolve) { | ||
resolve(true); | ||
}); | ||
}; | ||
barcodescanner.requestCameraPermission = function () { | ||
return new Promise(function (resolve) { | ||
resolve(true); | ||
}); | ||
}; | ||
module.exports = barcodescanner; |
@@ -6,13 +6,43 @@ var barcodescanner = require("./barcodescanner-common"); | ||
var SCANNER_REQUEST_CODE = 444; | ||
var CAMERA_PERMISSION_REQUEST_CODE = 555; | ||
barcodescanner.available = function () { | ||
barcodescanner._cameraPermissionGranted = function () { | ||
var hasPermission = android.os.Build.VERSION.SDK_INT < 23; // Android M. (6.0) | ||
if (!hasPermission) { | ||
hasPermission = android.content.pm.PackageManager.PERMISSION_GRANTED == | ||
android.support.v4.content.ContextCompat.checkSelfPermission(appModule.android.foregroundActivity, android.Manifest.permission.CAMERA); | ||
} | ||
return hasPermission; | ||
}; | ||
barcodescanner.hasCameraPermission = function () { | ||
return new Promise(function (resolve) { | ||
// TODO a real implementation, like on iOS | ||
resolve(true); | ||
resolve(barcodescanner._cameraPermissionGranted()); | ||
}); | ||
}; | ||
barcodescanner.requestCameraPermission = function () { | ||
return new Promise(function (resolve) { | ||
if (!barcodescanner._cameraPermissionGranted()) { | ||
// in a future version we could hook up the callback and change this flow a bit | ||
android.support.v4.app.ActivityCompat.requestPermissions( | ||
appModule.android.foregroundActivity, | ||
[android.Manifest.permission.CAMERA], | ||
CAMERA_PERMISSION_REQUEST_CODE); | ||
// this is not the nicest solution as the user needs to initiate scanning again after granting permission, | ||
// so enhance this in a future version, but it's ok for now | ||
resolve(); | ||
} | ||
}); | ||
}; | ||
barcodescanner.scan = function(arg) { | ||
return new Promise(function (resolve, reject) { | ||
try { | ||
if (!barcodescanner._cameraPermissionGranted()) { | ||
barcodescanner.requestCameraPermission(); | ||
reject("Permission needed"); | ||
return; | ||
} | ||
// the intent name should match the filter name in AndroidManifest.xml, don't change it | ||
@@ -19,0 +49,0 @@ var intent = new android.content.Intent("com.google.zxing.client.android.SCAN"); |
@@ -10,3 +10,4 @@ var barcodescanner = require("./barcodescanner-common"); | ||
// TODO check camera PERMISSION beforehand: https://github.com/yannickl/QRCodeReaderViewController/issues/4 | ||
// TODO consider giving camera PERMISSION beforehand: https://github.com/yannickl/QRCodeReaderViewController/issues/4, | ||
// would fit well with the Android 6 implementation. | ||
barcodescanner.scan = function (arg) { | ||
@@ -13,0 +14,0 @@ return new Promise(function (resolve, reject) { |
var observable = require("data/observable"); | ||
var barcodescanner = require("nativescript-barcodescanner"); | ||
var HelloWorldModel = (function (_super) { | ||
__extends(HelloWorldModel, _super); | ||
function HelloWorldModel() { | ||
var dialogs = require("ui/dialogs"); | ||
var DemoAppModel = (function (_super) { | ||
__extends(DemoAppModel, _super); | ||
function DemoAppModel() { | ||
_super.call(this); | ||
this.counter = 42; | ||
this.set("message", this.counter + " taps left"); | ||
} | ||
HelloWorldModel.prototype.tapAction = function () { | ||
this.counter--; | ||
DemoAppModel.prototype.doCheckAvailable = function () { | ||
barcodescanner.available().then( | ||
function(avail) { | ||
dialogs.alert({ | ||
title: "Scanning available?", | ||
message: avail ? "YES" : "NO", | ||
okButtonText: "OK" | ||
}) | ||
} | ||
) | ||
}; | ||
DemoAppModel.prototype.scan = function (front, flip) { | ||
barcodescanner.scan({ | ||
cancelLabel: "Stop scanning", // iOS only, default 'Close' | ||
message: "Go scan something", // Android only, default is 'Place a barcode inside the viewfinder rectangle to scan it.' | ||
preferFrontCamera: false, // Android only, default false | ||
showFlipCameraButton: true // Android only, default false (on iOS it's always available) | ||
cancelLabel: "Stop scanning", // iOS only, default 'Close' | ||
message: "Go scan something", // Android only, default is 'Place a barcode inside the viewfinder rectangle to scan it.' | ||
preferFrontCamera: front, // Android only, default false | ||
showFlipCameraButton: flip // Android only, default false (on iOS it's always available) | ||
}).then( | ||
function (result) { | ||
console.log("Scan format: " + result.format); | ||
console.log("Scan text: " + result.text); | ||
function(result) { | ||
dialogs.alert({ | ||
title: "Scan result", | ||
message: "Format: " + result.format + ",\nValue: " + result.text, | ||
okButtonText: "OK" | ||
}) | ||
}, | ||
function (error) { | ||
console.log("No scan: " + error); | ||
function(errorMessage) { | ||
console.log("No scan. " + errorMessage); | ||
} | ||
); | ||
if (this.counter <= 0) { | ||
this.set("message", "Hoorraaay! You unlocked the NativeScript clicker achievement!"); | ||
} else { | ||
this.set("message", this.counter + " taps left"); | ||
} | ||
) | ||
}; | ||
return HelloWorldModel; | ||
DemoAppModel.prototype.doScanWithFrontCamera = function () { | ||
this.scan(true, false); | ||
}; | ||
DemoAppModel.prototype.doScanWithBackCamera = function () { | ||
this.scan(false, true); | ||
}; | ||
DemoAppModel.prototype.doCheckHasCameraPermission = function () { | ||
barcodescanner.hasCameraPermission().then( | ||
function(granted) { | ||
dialogs.alert({ | ||
title: "Permission granted?", | ||
message: granted ? "YES" : "NO", | ||
okButtonText: "OK" | ||
}) | ||
} | ||
) | ||
}; | ||
DemoAppModel.prototype.doRequestCameraPermission = function () { | ||
barcodescanner.requestCameraPermission().then( | ||
function() { | ||
console.log("Camera permission requested"); | ||
} | ||
) | ||
}; | ||
return DemoAppModel; | ||
})(observable.Observable); | ||
exports.HelloWorldModel = HelloWorldModel; | ||
exports.mainViewModel = new HelloWorldModel(); | ||
exports.DemoAppModel = DemoAppModel; | ||
exports.mainViewModel = new DemoAppModel(); |
{ | ||
"name": "nativescript-barcodescanner", | ||
"version": "1.1.0", | ||
"description" : "Scan QR/barcodes with a {N} app.", | ||
"main" : "barcodescanner.js", | ||
"version": "1.2.0", | ||
"description": "Scan QR/barcodes with a {N} app.", | ||
"main": "barcodescanner.js", | ||
"nativescript": { | ||
@@ -14,3 +14,3 @@ "platforms": { | ||
"type": "git", | ||
"url": "https://github.com/eddyverbruggen/nativescript-barcodescanner.git" | ||
"url": "git+https://github.com/eddyverbruggen/nativescript-barcodescanner.git" | ||
}, | ||
@@ -17,0 +17,0 @@ "keywords": [ |
@@ -6,2 +6,4 @@ # NativeScript BarcodeScanner | ||
## Prerequisites | ||
Set your Android target to level 23. | ||
NativeScript 1.2.3+ for iOS, 1.3.0+ for Android (`tns --version`), so please upgrade if you need to. | ||
@@ -49,2 +51,32 @@ | ||
); | ||
``` | ||
``` | ||
### function: hasCameraPermission / requestCameraPermission | ||
On Android 6 you need to request permission to use the camera at runtime when targeting API level 23+. | ||
Even if the `uses-permission` tag for the Camera is present in `AndroidManifest.xml`. | ||
Note that `hasCameraPermission` will return true when: | ||
* You're running this on iOS, or | ||
* You're targeting an API level lower than 23, or | ||
* You're using Android < 6, or | ||
* You've already granted permission. | ||
```js | ||
barcodescanner.hasCameraPermission().then( | ||
function(granted) { | ||
// if this is 'false' you probably want to call 'requestCameraPermission' now | ||
console.log("Has Camera Permission? " + result); | ||
} | ||
); | ||
// if no permission was granted previously this wil open a user consent screen | ||
barcodescanner.requestCameraPermission().then( | ||
function() { | ||
console.log("Camera permission requested"); | ||
} | ||
); | ||
``` | ||
Note that the `scan` function will also check fr permission and ask for it if it wasn't previously granted. | ||
If you're relying on that, then you should know that since we're not catching the consent result | ||
the user will then need to allow camera access and launch the scanner again. |
Sorry, the diff of this file is not supported yet
1266806
234
80