Skip to content

Commit ad90bd4

Browse files
committed
chore(camera): readme documentation
closes #84
1 parent ea0d1f8 commit ad90bd4

File tree

2 files changed

+214
-7
lines changed

2 files changed

+214
-7
lines changed

packages/camera/README.md

Lines changed: 213 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,220 @@
1-
# Nativescript camera
1+
# NativeScript Camera
2+
3+
# Working with the camera plugin
4+
5+
## Overview
6+
7+
Almost every mobile application needs the option to capture, save and share images.
8+
The NativeScript camera plugin was designed for the first two parts of the job (taking a picture and optionally saving to device storage).
9+
10+
## Installation
211

3-
```javascript
4-
ns plugin add @nativescript/camera
512
```
13+
npm install @nativescript/camera --save
14+
```
15+
16+
## API
17+
18+
### Methods
19+
20+
| Method | Description |
21+
| ------------------------------------ | ----------------------------------------------------------------------------------------------------------------------- |
22+
| takePicture(options?: CameraOptions) | Take a photo using the camera with an optional parameter for setting different camera options. |
23+
| requestPermissions() | Request permission from the user for access to their saved photos as well as access to their camera. Returns a Promise. |
24+
| requestCameraPermissions() | Request permission from the user for access to their camera. Returns a Promise. |
25+
| requestPhotosPermissions() | Request permission from the user for access to their saved photos. Returns a Promise. |
26+
| isAvailable() | Is the device camera available to use. |
27+
28+
### CameraOptions
29+
30+
| Property | Default | Platform | Description |
31+
| --------------- | ------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
32+
| width | 0 | Both | Defines the desired width (in device independent pixels) of the taken image. It should be used with `height` property. If `keepAspectRatio` actual image width could be different in order to keep the aspect ratio of the original camera image. The actual image width will be greater than requested if the display density of the device is higher (than 1) (full HD+ resolutions). |
33+
| height | 0 | Both | Defines the desired height (in device independent pixels) of the taken image. It should be used with `width` property. If `keepAspectRatio` actual image width could be different in order to keep the aspect ratio of the original camera image. The actual image height will be greater than requested if the display density of the device is higher (than 1) (full HD+ resolutions). |
34+
| keepAspectRatio | true | Both | Defines if camera picture aspect ratio should be kept during picture resizing. This property could affect width or height return values. |
35+
| saveToGallery | true | Both | Defines if camera picture should be copied to photo Gallery (Android) or Photos (iOS) |
36+
| allowsEditing | false | iOS | Defines if camera "Retake" or "Use Photo" screen forces the user to crop camera picture to a square and optionally lets them zoom in. |
37+
| cameraFacing | rear | Both | The initial camera facing. Use `'front'` for selfies. |
38+
39+
> Note: The `saveToGallery` option might have unexpected behavior on Android! Some vendor camera apps (e.g. LG) will save all captured images to the gallery regardless of what the value of `saveToGallery` is. This behavior cannot be controlled by the camera plugin and if you must exclude the captured image from the photo gallery, you will need to get a local storage read/write permission and write custom code to find the gallery location and delete the new image from there.
640
741
## Usage
842

9-
// TODO
43+
### Requesting permissions
44+
45+
Both Android and iOS require explicit permissions in order for the application to have access to the camera and save photos to the device. Once the user has granted permissions the camera module can be used.
46+
47+
```TypeScript
48+
import { requestPermissions } from '@nativescript/camera';
49+
50+
requestPermissions().then(
51+
function success() {
52+
// permission request accepted or already granted
53+
// ... call camera.takePicture here ...
54+
},
55+
function failure() {
56+
// permission request rejected
57+
// ... tell the user ...
58+
}
59+
);
60+
```
61+
62+
> **Note for Android:** Older versions of Android that don't use a request permissions popup won't be affected by the usage of the requestPermissions method.
1063
11-
## License
64+
> **Note for iOS:** If the user rejects permissions from the iOS popup, the app is not allowed to ask again. You can instruct the user to go to app settings and enable the camera permission manually from there. Additionally, [App Store Guideline 5.1.1](https://developer.apple.com/app-store/review/guidelines/#data-collection-and-storage) requires apps to clarify the usage of the camera and photo library. To do so, edit your `app/App_Resources/iOS/Info.plist` and add the following clarifications:
65+
66+
```
67+
<key>NSCameraUsageDescription</key>
68+
<string>enter your camera permission request text here</string>
69+
<key>NSPhotoLibraryUsageDescription</key>
70+
<string>enter your photo library permission request text here</string>
71+
```
72+
73+
### Using the camera module to take a picture
74+
75+
Using the camera module is relatively simple.
76+
However, there are some points that need a little bit more explanation.
77+
78+
In order to use the camera module, just require it, as shown in Example 1:
79+
80+
> Example 1: Require camera module in the application
81+
82+
```JavaScript
83+
// JavaScript
84+
const camera = require("@nativescript/camera");
85+
```
86+
87+
```TypeScript
88+
// TypeScript
89+
import * as camera from "@nativescript/camera";
90+
```
91+
92+
Then you are ready to use it:
93+
94+
> Example 2: How to take a picture and to receive image asset
95+
96+
```JavaScript
97+
// JavaScript
98+
const { Image } = require("@nativescript/core");
99+
100+
camera.takePicture()
101+
.then(function (imageAsset) {
102+
console.log("Result is an image asset instance");
103+
var image = new Image();
104+
image.src = imageAsset;
105+
}).catch(function (err) {
106+
console.log("Error -> " + err.message);
107+
});
108+
```
109+
110+
```TypeScript
111+
// TypeScript
112+
import { Image } from "@nativescript/core";
113+
114+
camera.takePicture()
115+
.then((imageAsset) => {
116+
console.log("Result is an image asset instance");
117+
var image = new Image();
118+
image.src = imageAsset;
119+
}).catch((err) => {
120+
console.log("Error -> " + err.message);
121+
});
122+
```
123+
124+
The code in **Example 2** will start the native platform camera application. After taking the picture and tapping the button `Save` (Android) or `use image` (iOS), the promise will resolve the `then` part and image asset will be set as `src` of the `ui/image` control.
125+
126+
### Using the options to take memory efficient picture
127+
128+
**Example 2** shows how to take a picture using the NativeScript camera module. However, it takes a huge image (even mid-level devices has a 5MP camera, which results in a image 2580x2048, which in bitmap means approximately 15 MB). In many cases you don't need such a huge picture to show an image with 100x100 size, so taking a big picture is just a waste of memory. The camera takePicture() method accepts an optional parameter that could help in that case. With that optional parameter, you could set some properties like:
129+
130+
- **width**: The desired width of the picture (in device independent pixels).
131+
- **height**: The desired height of the picture (in device independent pixels).
132+
- **keepAspectRatio**: A boolean parameter that indicates if the aspect ratio should be kept.
133+
- **saveToGallery**: A boolean parameter that indicates if the original taken photo will be saved in "Photos" for Android and in "Camera Roll" in iOS
134+
- **allowsEditing**: (iOS Only) A boolean parameter that indicates if the camera "Retake" or "Use Photo" screen forces the user to crop camera picture to a square and optionally lets them zoom in.
135+
- **cameraFacing**: Start with either the "front" or "rear" (default) camera of the device. The current implementation doesn't work on all Android devices, in which case it falls back to the default behavior.
136+
137+
What does `device independent pixels` mean? The NativeScript layout mechanism uses device-independent pixels when measuring UI controls. This allows you to declare one layout and this layout will look similar to all devices (no matter the device's display resolution). In order to get a proper image quality for high resolution devices (like iPhone retina and Android Full HD), camera will return an image with bigger dimensions. For example, if we request an image that is 100x100, on iPhone 6 the actual image will be 200x200 (since its display density factor is 2 -> 100*2x100*2).
138+
Setting the `keepAspectRatio` property could result in a different than requested width or height. The camera will return an image with the correct aspect ratio but generally only one (from width and height) will be the same as requested; the other value will be calculated in order to preserve the aspect of the original image.
139+
140+
**Example 3** shows how to use the options parameter:
141+
142+
> Example 3: How to setup `width`, `height`, `keepAspectRatio` and `saveToGallery` properties for the camera module
143+
144+
```JavaScript
145+
// JavaScript
146+
147+
const options = {
148+
width: 300,
149+
height: 300,
150+
keepAspectRatio: false,
151+
saveToGallery: true
152+
};
153+
154+
camera.takePicture(options)
155+
.then(function (imageAsset) {
156+
console.log("Size: " + imageAsset.options.width + "x" + imageAsset.options.height);
157+
console.log("keepAspectRatio: " + imageAsset.options.keepAspectRatio);
158+
console.log("Photo saved in Photos/Gallery for Android or in Camera Roll for iOS");
159+
}).catch(function (err) {
160+
console.log("Error -> " + err.message);
161+
});
162+
```
163+
164+
```TypeScript
165+
// TypeScript
166+
import { Image } from "@nativescript/core";
167+
168+
const options = {
169+
width: 300,
170+
height: 300,
171+
keepAspectRatio: false,
172+
saveToGallery: true
173+
};
174+
175+
camera.takePicture(options)
176+
.then((imageAsset) => {
177+
console.log("Size: " + imageAsset.options.width + "x" + imageAsset.options.height);
178+
console.log("keepAspectRatio: " + imageAsset.options.keepAspectRatio);
179+
console.log("Photo saved in Photos/Gallery for Android or in Camera Roll for iOS");
180+
}).catch((err) => {
181+
console.log("Error -> " + err.message);
182+
});
183+
```
184+
185+
### Save a picture
186+
187+
To save a picture with the width & height that you have defined you must use the `imageAsset` and save it to the file system like so:
188+
189+
```TypeScript
190+
import { ImageSource, knownFolders, path } from '@nativescript/core';
191+
192+
const source = new ImageSource();
193+
194+
source.fromAsset(imageAsset)
195+
.then((imageSource: ImageSource) => {
196+
const folderPath: string = knownFolders.documents().path;
197+
const fileName: string = "test.jpg";
198+
const filePath: string = path.join(folderPath, fileName);
199+
const saved: boolean = imageSource.saveToFile(filePath, "jpg");
200+
201+
if (saved) {
202+
console.log("Gallery: " + this._dataItem.picture_url);
203+
console.log("Saved: " + filePath);
204+
console.log("Image saved successfully!");
205+
}
206+
});
207+
```
208+
209+
This could be used to create thumbnails for quick display within your application.
210+
211+
### Check if the device has available camera
212+
213+
The first thing that the developers should check if the device has an available camera.
214+
The method isAvaiable will return true if the camera hardware is ready to use or false if otherwise.
215+
216+
```
217+
const isAvailable = camera.isAvailable();
218+
```
12219

13-
Apache License Version 2.0
220+
> Note: This method will return false when used in iOS simulator (as the simulator does not have camera hardware)

packages/camera/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/camera",
3-
"version": "5.0.7",
3+
"version": "5.0.8",
44
"description": "Provides API for using device camera",
55
"main": "index",
66
"typings": "index.d.ts",

0 commit comments

Comments
 (0)