Skip to content
This repository has been archived by the owner on May 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5 from smartface/v2.0.0
Browse files Browse the repository at this point in the history
V2.0.0
  • Loading branch information
doganekici authored Jan 4, 2019
2 parents e4816ff + 6872931 commit 54eb215
Show file tree
Hide file tree
Showing 9 changed files with 471 additions and 647 deletions.
101 changes: 101 additions & 0 deletions Barcode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const System = require("sf-core/device/system");
var isIOS = System.OS === "iOS";

function Barcode(params) {
Object.defineProperties(this, {
'format': {
get: () => this._format,
set: e => this._format = e,
enumerable: true
},
'text': {
get: () => this._text,
set: e => this._text = e,
enumerable: true
}
});

if (params) {
for (var param in params) {
this[param] = params[param];
}
}
}

Object.defineProperty(Barcode, "FormatType", {
value: {},
enumerable: true
});

Object.defineProperties(Barcode.FormatType, {
'AZTEC': {
value: isIOS ? 0 : 'AZTEC',
enumerable: true
},
'CODABAR': {
value: isIOS ? 1 : 'CODABAR',
enumerable: true
},
'CODE_39': {
value: isIOS ? 2 : 'CODE_39',
enumerable: true
},
'CODE_93': {
value: isIOS ? 3 : 'CODE_93',
enumerable: true
},
'CODE_128': {
value: isIOS ? 4 : 'CODE_128',
enumerable: true
},
'DATA_MATRIX': {
value: isIOS ? 5 : 'DATA_MATRIX',
enumerable: true
},
'EAN_8': {
value: isIOS ? 6 : 'EAN_8',
enumerable: true
},
'EAN_13': {
value: isIOS ? 7 : 'EAN_13',
enumerable: true
},
'ITF': {
value: isIOS ? 8 : 'ITF',
enumerable: true
},
'MAXICODE': {
value: isIOS ? 9 : 'MAXICODE',
enumerable: true
},
'PDF_417': {
value: isIOS ? 10 : 'PDF_417',
enumerable: true
},
'QR_CODE': {
value: isIOS ? 11 : 'QR_CODE',
enumerable: true
},
'RSS_14': {
value: isIOS ? 12 : 'RSS_14',
enumerable: true
},
'RSS_EXPANDED': {
value: isIOS ? 13 : 'RSS_EXPANDED',
enumerable: true
},
'UPC_A': {
value: isIOS ? 14 : 'UPC_A',
enumerable: true
},
'UPC_E': {
value: isIOS ? 15 : 'UPC_E',
enumerable: true
},
'UPC_EAN_EXTENSION': {
value: isIOS ? 16 : 'UPC_EAN_EXTENSION',
enumerable: true
}
});

module.exports = Barcode;
70 changes: 29 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,31 @@
[![License](https://img.shields.io/badge/license-MIT-green.svg?style=flat)](https://raw.githubusercontent.com/smartface/sf-extension-barcode/master/LICENSE)
## Installation
Smartface Barcode Extension can be installed via npm easily from our public npm repository. The installation is pretty easy via Smartface Cloud IDE.
- Run command
```shell
(cd ~/workspace/scripts && npm i -S sf-extension-barcode)
```
- Finally require the extension as:
```javascript
const BarcodeScanner = require("sf-extension-barcode").BarcodeScanner;
```
## How to use
1) Require extension with
- Require extension with
```javascript
const BarcodeScanner = require("sf-extension-barcode").BarcodeScanner;
const { BarcodeScanner } = require("sf-extension-barcode");
```
2) Create an instance of `BarcodeScanner` via below example. You have to create barcode scanner object after `onShow` method. The scanning page is a fullscreen page.
- Create an instance of `BarcodeScanner`. Result will be handled in `onResult` callback.
```javascript
var barcodeScanner = new BarcodeScanner();
```
3) You can customize scanning page using `barcodeScanner.layout`. Look at sample code in [sample](./sample) folder for details.
```javascript
barcodeScanner.layout.addChild(view);
```
4) Set `onResult` callback to handle the result:
```javascript
barcodeScanner.onResult = function(e) {
var barcode = e.barcode;
var format = e.barcode.format;
page.lblBarcode.text = barcode.text;
barcodeScanner.stopCamera();
barcodeScanner.hide();
}
var barcodeScanner = new BarcodeScanner({
layout: myFlexLayout, // Required
width: 200, // Required
height: 200, // Required
onResult: ({ barcode }) => {
let { text, format } = barcode;
alert(text);
barcodeScanner.stopCamera();
barcodeScanner.hide();
}
});
```
5) Finally call `show` method with required parameters to scan barcode. Don't forget to guarantee camera [permission](#permissions) before `show` method.
- Finally call `show` method with no parameters. Camera [permissions](#permissions) must be granted before running `show` method.
```javascript
barcodeScanner.show({page: pageInstance, tag: "myPageTag"});
barcodeScanner.show();
```

## Barcode Format Type
Expand All @@ -61,20 +52,21 @@ Barcode.FormatType.UPC_E
Barcode.FormatType.UPC_EAN_EXTENSION
```

You can get format type via below code.
Barcode format can be retrieved like this.
```javascript
barcodeScanner.onResult = function(e) {
var barcode = e.barcode;
if(barcode.format === Barcode.FormatType.QR_CODE) {
const { Barcode } = require("sf-extension-barcode");
barcodeScanner.onResult = ({ barcode }) => {
if (barcode.format === Barcode.FormatType.QR_CODE) {
console.log("This is a qr code");
}
};
```
## Notes
1. Hiding the barcodeScanner causes [page.onShow](http://ref.smartface.io/#!/api/UI.Page-event-onShow) event to be fired.
2. If there is a need for closing the scanner, it needs to be implemented by the developer. UI close button example is in [sample](./sample) folder. For Android [onBackButtonPressed](http://ref.smartface.io/#!/api/UI.Page-event-onBackButtonPressed) needs to be implemented.
3. Scanner does not hide automatically when scanned
4. A scanner instance can be used only for once per image scan. For each scan action, a new `barcodeScanner` instance should be created and used
5. For Android, if there is an active textbox (keyboard is visible), developer needs to close the keyboard before showing the scanner.
- If there is a need for closing the scanner, it needs to be implemented by the developer. For Android [onBackButtonPressed](http://ref.smartface.io/#!/api/UI.Page-event-onBackButtonPressed) needs to be implemented.
- Scanner does not hide automatically when scanned
- A scanner instance can be used only for once per image scan. For each scan action, a new `BarcodeScanner` instance should be created and used
- For Android, if there is an active textbox (keyboard is visible), developer needs to close the keyboard before showing the scanner.
- It is advised to call `show` method with a timeout value for rendering performance.

## Permissions
For iOS, you have to add camera permission to Info.plist.
Expand All @@ -89,15 +81,11 @@ For Android, you have to add camera permission to AndroidManifest.xml.
<uses-feature android:name="android.hardware.camera.autofocus" />
```
## Sample
The folder [sample](./sample) holds the example codes. You can use it with the package.
```javascript
Router.add("barcodeScanner", require("sf-extension-barcode/sample/barcodeScanner"));
Router.go("barcodeScanner");
```
There are some examples in [sample](./sample) folder. You can use them.
## Credits
This barcode library is based on:
1) Android implementation: https://github.com/dm77/barcodescanner
2) iOS implementation: https://github.com/TheLevelUp/ZXingObjC
- [Android](https://github.com/dm77/barcodescanner) implementation
- [iOS](https://github.com/TheLevelUp/ZXingObjC) implementation
## Need Help?
Please [submit an issue](https://github.com/smartface/sf-extension-barcode/issues) on GitHub and provide information about your problem.
## Support & Documentation & Useful Links
Expand Down
Loading

0 comments on commit 54eb215

Please sign in to comment.