@nativescript/camera
Advanced tools
Comparing version 6.0.0 to 7.0.0
@@ -0,1 +1,2 @@ | ||
import * as permissions from '@nativescript-community/perms'; | ||
export declare function getAspectSafeDimensions(sourceWidth: any, sourceHeight: any, reqWidth: any, reqHeight: any): { | ||
@@ -5,1 +6,27 @@ width: number; | ||
}; | ||
export declare function mapError(e: any): PermissionResult; | ||
export declare function mapCameraPermissionStatus(permission: permissions.Result): PermissionResult; | ||
export declare function mapPhotoPermissionStatus(permission: permissions.Result): PermissionResult; | ||
export declare function combineCamerPhotoPermissions(cameraPermissions: PermissionResult, photoPermissions: PermissionResult): PermissionsResult; | ||
export declare enum Status { | ||
authorized = "authorized", | ||
denied = "denied", | ||
limited = "limited", | ||
restricted = "restricted", | ||
undetermined = "undetermined", | ||
never_ask_again = "never_ask_again", | ||
unknown = "unknown", | ||
error = "error" | ||
} | ||
export interface PermissionsResult { | ||
Success: boolean; | ||
Details: { | ||
Camera?: PermissionResult; | ||
Photo?: PermissionResult; | ||
}; | ||
} | ||
export interface PermissionResult { | ||
Success: boolean; | ||
Details: Status; | ||
Error?: any; | ||
} |
@@ -7,5 +7,58 @@ export function getAspectSafeDimensions(sourceWidth, sourceHeight, reqWidth, reqHeight) { | ||
width: Math.floor(sourceWidth / aspectCoef), | ||
height: Math.floor(sourceHeight / aspectCoef) | ||
height: Math.floor(sourceHeight / aspectCoef), | ||
}; | ||
} | ||
function mapStatus(result) { | ||
let status = Status.unknown; | ||
if (result && result.length > 1) { | ||
if (Object.keys(Status).findIndex((k) => k === result[0]) >= 0) { | ||
status = Status[result[0]]; | ||
} | ||
} | ||
return status; | ||
} | ||
export function mapError(e) { | ||
return { | ||
Success: false, | ||
Details: Status.error, | ||
Error: e, | ||
}; | ||
} | ||
export function mapCameraPermissionStatus(permission) { | ||
const status = mapStatus(permission); | ||
const result = { | ||
Success: status === Status.authorized, | ||
Details: status, | ||
}; | ||
return result; | ||
} | ||
export function mapPhotoPermissionStatus(permission) { | ||
const status = mapStatus(permission); | ||
const result = { | ||
Success: status === Status.authorized || status === Status.limited, | ||
Details: status, | ||
}; | ||
return result; | ||
} | ||
export function combineCamerPhotoPermissions(cameraPermissions, photoPermissions) { | ||
const result = { | ||
Success: cameraPermissions.Success && photoPermissions.Success, | ||
Details: { | ||
Camera: cameraPermissions, | ||
Photo: photoPermissions, | ||
}, | ||
}; | ||
return result; | ||
} | ||
export var Status; | ||
(function (Status) { | ||
Status["authorized"] = "authorized"; | ||
Status["denied"] = "denied"; | ||
Status["limited"] = "limited"; | ||
Status["restricted"] = "restricted"; | ||
Status["undetermined"] = "undetermined"; | ||
Status["never_ask_again"] = "never_ask_again"; | ||
Status["unknown"] = "unknown"; | ||
Status["error"] = "error"; | ||
})(Status || (Status = {})); | ||
//# sourceMappingURL=common.js.map |
import { CameraOptions } from '.'; | ||
import { PermissionResult, PermissionsResult } from './common'; | ||
export declare const takePicture: (options?: CameraOptions) => Promise<any>; | ||
export declare const isAvailable: () => boolean; | ||
export declare function requestPermissions(): Promise<boolean>; | ||
export declare function requestPhotosPermissions(): Promise<boolean>; | ||
export declare function requestCameraPermissions(): Promise<boolean>; | ||
export declare function requestPermissions(): Promise<PermissionsResult>; | ||
export declare function requestPhotosPermissions(): Promise<PermissionResult>; | ||
export declare function requestCameraPermissions(): Promise<PermissionResult>; |
import { Utils, Application, Device, Trace, ImageAsset } from '@nativescript/core'; | ||
import * as permissions from '@nativescript-community/perms'; | ||
import { combineCamerPhotoPermissions, mapCameraPermissionStatus, mapPhotoPermissionStatus, Status } from './common'; | ||
const REQUEST_IMAGE_CAPTURE = 3453; | ||
@@ -159,19 +160,19 @@ const useAndroidX = function () { | ||
export async function requestPermissions() { | ||
return requestCameraPermissions().then((cameraPermissions) => requestPhotosPermissions().then((photoPermissions) => combineCamerPhotoPermissions(cameraPermissions, photoPermissions))); | ||
} | ||
export async function requestPhotosPermissions() { | ||
if (api30()) { | ||
const hasPerm = await permissions.request('android.permission.CAMERA'); | ||
return hasPerm[1]; | ||
return { | ||
Success: true, | ||
Details: Status.authorized, | ||
}; | ||
} | ||
else { | ||
const hasPerm1 = await permissions.request('android.permission.WRITE_EXTERNAL_STORAGE'); | ||
const hasPerm2 = await permissions.request('android.permission.CAMERA'); | ||
return hasPerm1[1] && hasPerm2[1]; | ||
const hasPerm = await permissions.request('android.permission.WRITE_EXTERNAL_STORAGE'); | ||
return mapPhotoPermissionStatus(hasPerm); | ||
} | ||
} | ||
export async function requestPhotosPermissions() { | ||
const hasPerm = await permissions.request('android.permission.WRITE_EXTERNAL_STORAGE'); | ||
return hasPerm[1]; | ||
} | ||
export async function requestCameraPermissions() { | ||
const hasPerm = await permissions.request('android.permission.CAMERA'); | ||
return hasPerm[1]; | ||
return mapCameraPermissionStatus(hasPerm); | ||
} | ||
@@ -178,0 +179,0 @@ const createDateTimeStamp = function () { |
import { ImageAsset } from '@nativescript/core'; | ||
import { PermissionsResult, PermissionResult } from './common'; | ||
/** | ||
@@ -12,5 +12,5 @@ * Take a photo using the camera. | ||
*/ | ||
export function requestPermissions(): Promise<any>; | ||
export function requestCameraPermissions(): Promise<any>; | ||
export function requestPhotosPermissions(): Promise<any>; | ||
export function requestPermissions(): Promise<PermissionsResult>; | ||
export function requestCameraPermissions(): Promise<PermissionResult>; | ||
export function requestPhotosPermissions(): Promise<PermissionResult>; | ||
@@ -23,44 +23,43 @@ /** | ||
export interface CameraOptions { | ||
/** | ||
* 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). | ||
*/ | ||
width?: number; | ||
/** | ||
* 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). | ||
*/ | ||
width?: number; | ||
/** | ||
* 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). | ||
*/ | ||
height?: number; | ||
/** | ||
* 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). | ||
*/ | ||
height?: number; | ||
/** | ||
* Defines if camera picture aspect ratio should be kept during picture resizing. | ||
* This property could affect width or height return values. | ||
*/ | ||
keepAspectRatio?: boolean; | ||
/** | ||
* Defines if camera picture aspect ratio should be kept during picture resizing. | ||
* This property could affect width or height return values. | ||
*/ | ||
keepAspectRatio?: boolean; | ||
/** | ||
* Defines if camera picture should be copied to photo Gallery (Android) or Photos (iOS) | ||
*/ | ||
saveToGallery?: boolean; | ||
/** | ||
* Defines if camera picture should be copied to photo Gallery (Android) or Photos (iOS) | ||
*/ | ||
saveToGallery?: boolean; | ||
/** | ||
* iOS Only | ||
* Defines if camera "Retake" or "Use Photo" screen forces user to crop camera picture to a square and optionally lets them zoom in. | ||
*/ | ||
allowsEditing?: boolean; | ||
/** | ||
* iOS Only | ||
* Defines if camera "Retake" or "Use Photo" screen forces user to crop camera picture to a square and optionally lets them zoom in. | ||
*/ | ||
allowsEditing?: boolean; | ||
/** | ||
* The initial camera. Default "rear". | ||
* The current implementation doesn't work on all Android devices, in which case it falls back to the default behavior. | ||
*/ | ||
cameraFacing?: "front" | "rear"; | ||
/** | ||
* The initial camera. Default "rear". | ||
* The current implementation doesn't work on all Android devices, in which case it falls back to the default behavior. | ||
*/ | ||
cameraFacing?: 'front' | 'rear'; | ||
/** | ||
* (iOS Only) Specify a custom UIModalPresentationStyle (Defaults to UIModalPresentationStyle.FullScreen) | ||
*/ | ||
modalPresentationStyle?: number; | ||
/** | ||
* (iOS Only) Specify a custom UIModalPresentationStyle (Defaults to UIModalPresentationStyle.FullScreen) | ||
*/ | ||
modalPresentationStyle?: number; | ||
} |
import { CameraOptions } from '.'; | ||
import { PermissionResult, PermissionsResult } from './common'; | ||
export declare let takePicture: (options: CameraOptions) => Promise<any>; | ||
export declare let isAvailable: () => boolean; | ||
export declare let requestPermissions: () => Promise<unknown>; | ||
export declare let requestPhotosPermissions: () => Promise<void>; | ||
export declare let requestCameraPermissions: () => Promise<void>; | ||
export declare let requestPermissions: () => Promise<PermissionsResult>; | ||
export declare let requestPhotosPermissions: () => Promise<PermissionResult>; | ||
export declare let requestCameraPermissions: () => Promise<PermissionResult>; |
102
index.ios.js
@@ -1,2 +0,4 @@ | ||
import { Utils, ImageSource, ImageAsset, Trace, Frame } from '@nativescript/core'; | ||
import { Device, Frame, ImageAsset, ImageSource, Trace, Utils } from '@nativescript/core'; | ||
import * as permissions from '@nativescript-community/perms'; | ||
import { combineCamerPhotoPermissions, mapCameraPermissionStatus, mapError, mapPhotoPermissionStatus } from './common'; | ||
var UIImagePickerControllerDelegateImpl = /** @class */ (function (_super) { | ||
@@ -125,4 +127,10 @@ __extends(UIImagePickerControllerDelegateImpl, _super); | ||
} | ||
let authStatus = PHPhotoLibrary.authorizationStatus(); | ||
if (authStatus !== 3 /* PHAuthorizationStatus.Authorized */) { | ||
let authStatus; | ||
if (parseFloat(Device.osVersion) >= 14) { | ||
authStatus = PHPhotoLibrary.authorizationStatusForAccessLevel(2 /* PHAccessLevel.ReadWrite */); | ||
} | ||
else { | ||
authStatus = PHPhotoLibrary.authorizationStatus(); | ||
} | ||
if (authStatus !== 3 /* PHAuthorizationStatus.Authorized */ && authStatus !== 4 /* PHAuthorizationStatus.Limited */) { | ||
saveToGallery = false; | ||
@@ -173,86 +181,16 @@ } | ||
export let requestPermissions = function () { | ||
return new Promise(function (resolve, reject) { | ||
requestPhotosPermissions().then(() => { | ||
requestCameraPermissions().then(resolve, reject); | ||
}, reject); | ||
}); | ||
return requestCameraPermissions().then((cameraPermissions) => requestPhotosPermissions().then((photoPermissions) => combineCamerPhotoPermissions(cameraPermissions, photoPermissions))); | ||
}; | ||
export let requestPhotosPermissions = function () { | ||
return new Promise(function (resolve, reject) { | ||
let authStatus; | ||
if (Utils.SDK_VERSION >= 14) { | ||
authStatus = PHPhotoLibrary.authorizationStatusForAccessLevel(2 /* PHAccessLevel.ReadWrite */); | ||
} | ||
else { | ||
authStatus = PHPhotoLibrary.authorizationStatus(); | ||
} | ||
switch (authStatus) { | ||
case 0 /* PHAuthorizationStatus.NotDetermined */: { | ||
const handler = (auth) => { | ||
if (auth === 3 /* PHAuthorizationStatus.Authorized */) { | ||
if (Trace.isEnabled()) { | ||
Trace.write('Application can access photo library assets.', Trace.categories.Debug); | ||
} | ||
resolve(); | ||
} | ||
else { | ||
reject(); | ||
} | ||
}; | ||
if (Utils.SDK_VERSION >= 14) { | ||
PHPhotoLibrary.requestAuthorizationForAccessLevelHandler(2 /* PHAccessLevel.ReadWrite */, handler); | ||
} | ||
else { | ||
PHPhotoLibrary.requestAuthorization(handler); | ||
} | ||
break; | ||
} | ||
case 3 /* PHAuthorizationStatus.Authorized */: { | ||
if (Trace.isEnabled()) { | ||
Trace.write('Application can access photo library assets.', Trace.categories.Debug); | ||
} | ||
resolve(); | ||
break; | ||
} | ||
case 1 /* PHAuthorizationStatus.Restricted */: | ||
case 2 /* PHAuthorizationStatus.Denied */: { | ||
if (Trace.isEnabled()) { | ||
Trace.write('Application can not access photo library assets.', Trace.categories.Debug); | ||
} | ||
reject(); | ||
break; | ||
} | ||
} | ||
}); | ||
return permissions | ||
.request('photo') | ||
.then((photoPermissions) => mapPhotoPermissionStatus(photoPermissions)) | ||
.catch((e) => mapError(e)); | ||
}; | ||
export let requestCameraPermissions = function () { | ||
return new Promise(function (resolve, reject) { | ||
let cameraStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo); | ||
switch (cameraStatus) { | ||
case 0 /* AVAuthorizationStatus.NotDetermined */: { | ||
AVCaptureDevice.requestAccessForMediaTypeCompletionHandler(AVMediaTypeVideo, (granted) => { | ||
if (granted) { | ||
resolve(); | ||
} | ||
else { | ||
reject(); | ||
} | ||
}); | ||
break; | ||
} | ||
case 3 /* AVAuthorizationStatus.Authorized */: { | ||
resolve(); | ||
break; | ||
} | ||
case 1 /* AVAuthorizationStatus.Restricted */: | ||
case 2 /* AVAuthorizationStatus.Denied */: { | ||
if (Trace.isEnabled()) { | ||
Trace.write('Application can not access Camera assets.', Trace.categories.Debug); | ||
} | ||
reject(); | ||
break; | ||
} | ||
} | ||
}); | ||
return permissions | ||
.request('camera') | ||
.then((photoPermissions) => mapCameraPermissionStatus(photoPermissions)) | ||
.catch((e) => mapError(e)); | ||
}; | ||
//# sourceMappingURL=index.ios.js.map |
{ | ||
"name": "@nativescript/camera", | ||
"version": "6.0.0", | ||
"version": "7.0.0", | ||
"description": "Provides API for using device camera", | ||
@@ -39,3 +39,4 @@ "main": "index", | ||
"@nativescript-community/perms": "^2.3.1" | ||
} | ||
}, | ||
"types": "./index.d.d.ts" | ||
} |
@@ -21,2 +21,6 @@ # @nativescript/camera | ||
**Note: Version 7 contains breaking changes:** | ||
* New behavior on requesting permissions, detailed in [Request for user permissions](#request-for-user-permissions). | ||
## Installation | ||
@@ -58,13 +62,34 @@ | ||
requestPermissions().then( | ||
function success() { | ||
// permission request accepted or already granted | ||
// ... call camera.takePicture here ... | ||
}, | ||
function failure() { | ||
// permission request rejected | ||
// ... tell the user ... | ||
} | ||
); | ||
const perms = await camera.requestPermissions(); | ||
if (perms.Success) { | ||
// permission request accepted or already granted | ||
// ... call camera.takePicture here ... | ||
} else { | ||
// permission request rejected | ||
// ... tell the user ... | ||
const cameraPermissionSuccess = perms.Details.Camera.Success; | ||
const photoPermissionSuccess = perms.Details.Photo.Success | ||
} | ||
``` | ||
If specifying the `saveToGallery = false` option, you can call the `requestCameraPermissions` method. | ||
```TypeScript | ||
import { requestPermissions } from '@nativescript/camera'; | ||
const perms = await camera.requestCameraPermissions(); | ||
if (perms.Success) { | ||
// permission request accepted or already granted | ||
// ... call camera.takePicture here ... | ||
} else { | ||
// permission request rejected | ||
// ... tell the user ... | ||
} | ||
``` | ||
> **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. | ||
@@ -71,0 +96,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
71983
616
222