|
| 1 | +<img src="https://raw.githubusercontent.com/Yoonit-Labs/nativescript-yoonit-camera/development/logo_cyberlabs.png" width="300"> |
| 2 | + |
| 3 | +# NativeScript Yoonit Camera |
| 4 | + |
| 5 | +  |
| 6 | + |
| 7 | +A NativeScript plugin to provide: |
| 8 | +- Modern Android Camera API (Camera X) |
| 9 | +- MLKit integration |
| 10 | +- Camera preview (Front & Back) |
| 11 | +- Face detection (With Min & Max size (Soon)) |
| 12 | +- Landmark detection (Soon) |
| 13 | +- Face crop |
| 14 | +- Face capture |
| 15 | +- Frame capture (Soon) |
| 16 | +- Face ROI (Soon) |
| 17 | +- QR Code scanning |
| 18 | +- Better props to setup the initialization your component |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +```javascript |
| 23 | +npm i -s @yoonit/nativescript-camera |
| 24 | +``` |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +All the functionalities that the `@yoonit/nativescript-camera` provides is accessed through the `YoonitCamera` component, that includes the camera preview. Below we have the basic usage code, for more details, your can see the [**Methods**](#methods), [**Events**](#events) or the [**Demo Vue**](https://github.com/Yoonit-Labs/nativescript-yoonit-camera/tree/development/demo-vue). |
| 29 | + |
| 30 | + |
| 31 | +#### VueJS Plugin |
| 32 | +`main.js` |
| 33 | +```javascript |
| 34 | +import Vue from 'nativescript-vue' |
| 35 | +import YoonitCamera from '@yoonit/nativescript-camera/vue' |
| 36 | + |
| 37 | +Vue.use(YoonitCamera) |
| 38 | +``` |
| 39 | + |
| 40 | +After that, you can access the camera object in your entire project using `this.$yoo.camera` |
| 41 | + |
| 42 | +#### Vue Component |
| 43 | +`App.vue` |
| 44 | +```vue |
| 45 | +<template> |
| 46 | + <Page @loaded="onLoaded"> |
| 47 | + <YoonitCamera |
| 48 | + ref="yooCamera" |
| 49 | + @faceDetected="doFaceDetected" |
| 50 | + @faceImage="doFaceImage" |
| 51 | + @endCapture="doEndCapture" |
| 52 | + @qrCodeContent="doQRCodeContent" |
| 53 | + @status="doStatus" |
| 54 | + @permissionDenied="doPermissionDenied" |
| 55 | + /> |
| 56 | + </Page> |
| 57 | +</template> |
| 58 | +
|
| 59 | +<script> |
| 60 | + export default { |
| 61 | + data: () => ({}), |
| 62 | +
|
| 63 | + methods: { |
| 64 | + async onLoaded(args) { |
| 65 | + console.log('[YooCamera] Getting Camera view') |
| 66 | + this.$yoo.camera.registerElement(this.$refs.yooCamera) |
| 67 | +
|
| 68 | + console.log('[YooCamera] Getting permission') |
| 69 | + if (await this.$yoo.camera.requestPermission()) { |
| 70 | + console.log('[YooCamera] Permission granted, start preview') |
| 71 | +
|
| 72 | + this.$yoo.camera.preview() |
| 73 | + } |
| 74 | + }, |
| 75 | +
|
| 76 | + doFaceDetected({ faceDetected }) { |
| 77 | + console.log('[YooCamera] faceDetected', faceDetected) |
| 78 | + }, |
| 79 | +
|
| 80 | + doFaceImage({ |
| 81 | + count, |
| 82 | + total, |
| 83 | + image: { |
| 84 | + path, |
| 85 | + source |
| 86 | + } |
| 87 | + }) { |
| 88 | + if (total !== 0) { |
| 89 | + console.log('[YooCamera] doFaceImage', `[${count}] of [${total}] - ${path}`) |
| 90 | + } else { |
| 91 | + console.log('[YooCamera] doFaceImage', `[${count}] ${path}`) |
| 92 | + } |
| 93 | +
|
| 94 | + console.log('[YooCamera] doFaceImage', path, source) |
| 95 | + }, |
| 96 | +
|
| 97 | + doEndCapture() { |
| 98 | + console.log('[YooCamera] doEndCapture') |
| 99 | + }, |
| 100 | +
|
| 101 | + doQRCodeContent({ content }) { |
| 102 | + console.log('[YooCamera] doQRCodeContent', content) |
| 103 | + }, |
| 104 | +
|
| 105 | + doStatus({ status }) { |
| 106 | + console.log('[YooCamera] doStatus', JSON.parse(status)) |
| 107 | + }, |
| 108 | +
|
| 109 | + doToggleLens() { |
| 110 | + const currentCameraLens = this.$yoo.camera.getLens() |
| 111 | +
|
| 112 | + console.log('[YooCamera] doToggleLens', currentCameraLens) |
| 113 | +
|
| 114 | + this.$yoo.camera.toggleLens() |
| 115 | + }, |
| 116 | +
|
| 117 | + doStartCapture(captureType) { |
| 118 | + console.log('[YooCamera] doStartCapture', captureType) |
| 119 | +
|
| 120 | + this.$yoo.camera.startCapture(captureType) |
| 121 | + }, |
| 122 | +
|
| 123 | + doFaceDetectionBox(status) { |
| 124 | + console.log('[YooCamera] doFaceDetectionBox', status) |
| 125 | +
|
| 126 | + this.$yoo.camera.setFaceDetectionBox(status) |
| 127 | + }, |
| 128 | +
|
| 129 | + doPermissionDenied() { |
| 130 | + console.log('[YooCamera] doPermissionDenied') |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +</script> |
| 135 | +``` |
| 136 | + |
| 137 | +## API |
| 138 | + |
| 139 | +#### Methods |
| 140 | +| Function | Parameters | Return Type | Valid values | Description | |
| 141 | +|-|-|-|-|-| |
| 142 | +| **`requestPermission`** | - | promise | - | Ask to user to give the permission to access camera. |
| 143 | +| **`hasPermission`** | - | boolean | - | Return if application has camera permission. |
| 144 | +| **`preview`** | - | void | - | Start camera preview if has permission. |
| 145 | +| **`startCapture`** | `captureType: string` | void | `none` default capture type. `face` for face recognition. `barcode` to read barcode content. | Set capture type none, face or barcode. |
| 146 | +| **`stopCapture`** | - | void | - | Stop any type of capture. |
| 147 | +| **`toggleLens`** | - | void | - | Set camera lens facing front or back. |
| 148 | +| **`getLens`** | - | number | - | Return `number` that represents lens face state: 0 for front 1 for back camera. |
| 149 | +| **`setFaceNumberOfImages`** | `faceNumberOfImages: number` | void | Any positive `number` value | Default value is 0. For value 0 is saved infinity images. When saved images reached the "face number os images", the `onEndCapture` is triggered. |
| 150 | +| **`setFaceDetectionBox`** |`faceDetectionBox: boolean` | void | `true` or `false` | Set to show face detection box when face detected. |
| 151 | +| **`setFaceTimeBetweenImages`** | `faceTimeBetweenImages: number` | void | Any positive `number` that represent time in milli seconds | Set saving face images time interval in milli seconds. |
| 152 | +| **`setFacePaddingPercent`** | `facePaddingPercent: number` | void | Any positive `number` value | Set face image and bounding box padding in percent. |
| 153 | +| **`setFaceImageSize`** | `faceImageSize: number` | void | Any positive `number` value | Set face image size to be saved. |
| 154 | + |
| 155 | + |
| 156 | +#### Events |
| 157 | +| Event | Parameters | Description | |
| 158 | +|-|-|-| |
| 159 | +| faceImage | `{ count: number, total: number, image: object = { path: string, source: blob } }` | Must have started capture type of face (see `startCapture`). Emitted when the face image file is created: <ul><li>count: current index</li><li>total: total to create</li><li>image.path: the face image path</li><li>image.source: the blob file</li><ul> |
| 160 | +| faceDetected | `{ faceDetected: boolean }` | Emitted `true` while the camera detects a face. Emitted `false` once when a face is no more detected |
| 161 | +| endCapture | - | Emitted when the number of face image files created is equal of the number of images set (see the method `setFaceNumberOfImages`). |
| 162 | +| qrCodeContent | `{ content: string }` | Must have started capture type of barcode (see `startCapture`). Emitted when the camera scan a QR Code. |
| 163 | +| status |`{ type: 'error'/'message', status: string }` | Emitted message error from native. Used more often for debug purpose. |
| 164 | +| permissionDenied | - | Emitted when try to `preview` but there is not camera permission. |
| 165 | + |
| 166 | + |
| 167 | +## To contribute and make it better |
| 168 | + |
| 169 | +Clone the repo, change what you want and send PR. |
| 170 | + |
| 171 | +Contributions are always welcome! |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +Code with ❤ by the [**Cyberlabs AI**](https://cyberlabs.ai/) Front-End Team |
0 commit comments