当前位置: 首页 > 软件库 > 手机/移动开发 > >

nativescript-camera

授权协议 Apache-2.0 License
开发语言 JavaScript TypeScript
所属分类 手机/移动开发
软件类型 开源软件
地区 不详
投 递 者 郜德容
操作系统 iOS
开源组织
适用人群 未知
 软件概览

NativeScript 7 or higher

If using 6 and below, see the following:

NativeScript Camera

Welcome to the nativescript-camera plugin for NativeScript framework

(Optional) Prerequisites / Requirements

Working with the camera plugin

Overview

Almost every mobile application needs the option to capture, save and share images.The NativeScript camera plugin was designed for the first two parts of the job (taking a picture and optionally saving to device storage).

Installation

Navigate to project folder and run NativeScript-CLI command

tns plugin add nativescript-camera

Plugin could be added as a standard npm dependency running command

npm install nativescript-camera --save 

Note: the --save flag will add the plugin as dependency in your package.json file

API

Methods

Method Description
takePicture(options?: CameraOptions) Take a photo using the camera with an optional parameter for setting different camera options.
requestPermissions() Request permission from the user for access to their saved photos as well as access to their camera. Returns a Promise.
requestCameraPermissions() Request permission from the user for access to their camera. Returns a Promise.
requestPhotosPermissions() Request permission from the user for access to their saved photos. Returns a Promise.
isAvailable() Is the device camera available to use.

CameraOptions

Property Default Platform Description
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).
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).
keepAspectRatio true Both Defines if camera picture aspect ratio should be kept during picture resizing. This property could affect width or height return values.
saveToGallery true Both Defines if camera picture should be copied to photo Gallery (Android) or Photos (iOS)
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.
cameraFacing rear Both The initial camera facing. Use 'front' for selfies.

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.

Usage

Requesting permissions

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.

camera.requestPermissions().then(
    function success() {
        // permission request accepted or already granted 
        // ... call camera.takePicture here ...
    }, 
    function failure() {
        // 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.

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 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:

<key>NSCameraUsageDescription</key>
<string>enter your camera permission request text here</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>enter your photo library permission request text here</string>

Using the camera module to take a picture

Using the camera module is relatively simple.However, there are some points that need a little bit more explanation.

In order to use the camera module, just require it, as shown in Example 1:

Example 1: Require camera module in the application

// JavaScript
const camera = require("nativescript-camera");
// TypeScript
import * as camera from "nativescript-camera";

Then you are ready to use it:

Example 2: How to take a picture and to receive image asset

// JavaScript
const imageModule = require("tns-core-modules/ui/image");

camera.takePicture()   
    .then(function (imageAsset) {
        console.log("Result is an image asset instance");
        var image = new imageModule.Image();
        image.src = imageAsset;
    }).catch(function (err) {
        console.log("Error -> " + err.message);
    });
// TypeScript
import { Image } from "tns-core-modules/ui/image";

camera.takePicture()
    .then((imageAsset) => {
        console.log("Result is an image asset instance");
        var image = new Image();
        image.src = imageAsset;
    }).catch((err) => {
        console.log("Error -> " + err.message);
    });

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.

Using the options to take memory efficient picture

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:

  • width: The desired width of the picture (in device independent pixels).
  • height: The desired height of the picture (in device independent pixels).
  • keepAspectRatio: A boolean parameter that indicates if the aspect ratio should be kept.
  • saveToGallery: A boolean parameter that indicates if the original taken photo will be saved in "Photos" for Android and in "Camera Roll" in iOS
  • 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.
  • 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.

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 -> 1002x1002).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.

Example 3 shows how to use the options parameter:

Example 3: How to setup width, height, keepAspectRatio and saveToGallery properties for the camera module

// JavaScript
const imageModule = require("tns-core-modules/ui/image");

const options = {
    width: 300,
    height: 300,
    keepAspectRatio: false,
    saveToGallery: true
};

camera.takePicture(options)   
    .then(function (imageAsset) {
        console.log("Size: " + imageAsset.options.width + "x" + imageAsset.options.height);
        console.log("keepAspectRatio: " + imageAsset.options.keepAspectRatio);
        console.log("Photo saved in Photos/Gallery for Android or in Camera Roll for iOS");
    }).catch(function (err) {
        console.log("Error -> " + err.message);
    });
// TypeScript
import { Image } from "tns-core-modules/ui/image";

const options = {
    width: 300,
    height: 300,
    keepAspectRatio: false,
    saveToGallery: true
};

camera.takePicture(options)
    .then((imageAsset) => {
        console.log("Size: " + imageAsset.options.width + "x" + imageAsset.options.height);
        console.log("keepAspectRatio: " + imageAsset.options.keepAspectRatio);
        console.log("Photo saved in Photos/Gallery for Android or in Camera Roll for iOS");
    }).catch((err) => {
        console.log("Error -> " + err.message);
    });

Save a picture

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:

const source = new ImageSource();

source.fromAsset(imageAsset)
    .then((imageSource: ImageSource) => {
        const folderPath: string = knownFolders.documents().path;
        const fileName: string = "test.jpg";
        const filePath: string = path.join(folderPath, fileName);
        const saved: boolean = imageSource.saveToFile(filePath, "jpg");

        if (saved) {
            console.log("Gallery: " + this._dataItem.picture_url);
            console.log("Saved: " + filePath);
            console.log("Image saved successfully!");
        }
    });

This could be used to create thumbnails for quick display within your application.

Check if the device has available camera

The first thing that the developers should check if the device has an available camera.The method isAvaiable will return true if the camera hardware is ready to use or false if otherwise.

const isAvailable = camera.isAvailable(); 

Note: This method will return false when used in iOS simulator (as the simulator does not have camera hardware)

Contribute

We love PRs! Check out the contributing guidelines. If you want to contribute, but you are not sure where to start - look for issues labeled help wanted.

Get Help

Please, use github issues strictly for reporting bugs or requesting features. For general questions and support, check out Stack Overflow or ask our experts in NativeScript community Slack channel.

  • OpenCV Android Studio 在Android Studio工程中使用Native的方式集成OpenCV 为什么要使用Native方式集成OpenCV 如果我们要处理的图片计算量不大,或者对处理速度不关注的时候,我们完全可以采用Java的来调用OpenCV。采用Java来调用OpenCV的集成方法非常简单,具体集成方法可以参考我的这个教程:https://panxsoft.codin

  • 简单聊两句 学习Vue+Cordova打包编译App,首先你要安装Cordova与vue,在这里本人就不说明了,自行看文档与搜索相关资料。 Cordova中文官网地址 Vue中文官网地址 第一步:首先在vue代码中加入cordova-plugin-camera的调用方法 navigator.camera.getPicture是调用camera的方法,他会附带三个参数,cameraSuccess是提

  • camera 摄像头拍照/摄像 Camera模块管理设备的摄像头,可用于拍照、摄像操作,通过plus.camera获取摄像头管理对象。 方法: getCamera: 获取摄像头管理对象 对象: Camera: 摄像头对象 CameraOption: JSON对象,调用摄像头的参数 PopPosition: JSON对象,弹出拍照或摄像界面指示位置 回调方法: CameraSuccessCallba

  • 转载于:https://blog.csdn.net/u010127332/article/details/83622209 React Native从开发环境到入门练手,再到跑几个开源demo的整个过程中,遇到了不少问题,以下是对报错现象及解决方法的记录: Mac上运行iOS项目 问题1: npm ERR! Unexpected end of JSON input while parsing ne

  • camera 摄像头拍照/摄像 Camera模块管理设备的摄像头,可用于拍照、摄像操作,通过plus.camera获取摄像头管理对象。 方法: getCamera: 获取摄像头管理对象 对象: Camera: 摄像头对象 CameraOption: JSON对象,调用摄像头的参数 PopPosition: JSON对象,弹出拍照或摄像界面指示位置 回调方法: CameraSuccessCallba

  •     安卓Unity3d 可以使用ReadPixels从当前Render Target读取图像,音频可以从AudioClip读取,具体调用GetData接口,读取到的可能是float类型,有些音频编码器可能需要sint16格式,这需要做一个转换。 读图像先用StartCoroutine创建协程,等待WaitForEndOfFrame, 之后调用ReadPixels就好,调用之前要创建一个和Cam

 相关资料
  • NativeScript 可以使用 Javascript,CSS, XML 创建真正的 Native 跨平台应用,支持 iOS Android,NativeScript 将您的跨平台代码翻译成目标平台的代码。 UI 使用 XML 描述,CSS 样式,在编译时将 UI 转化成本地原生代码,最终得到正在的 Native 原生应用。 Telerik 公开了用于创建安卓、iOS和Windows Unive

  • NativeScript Yoonit Camera The most advanced and complete NativeScript Camera plugin ◻ Fully iOS and Android integration ◻ VueJS Plugin ◻ Modern Android Camera API [Camera X](https://developer.android

  • NativeScript Command-Line Interface The NativeScript CLI lets you create, build, and deploy NativeScript-based apps on iOS and Android devices. Get it using: npm install -g nativescript What is Native

  • NativeScript-Snackbar �� �� �� NativeScript plugin for Material Design SnackBar component. Installation: NativeScript 7+:tns plugin add @nstudio/nativescript-snackbar NativeScript version prior to 7:t

  • Nativescript-Ripple This plugin aims to bring a native (or close to native) ripple implementation on Android and iOS. The android version uses a RippleDrawable and conserves the previous background, a

  • NativeScript-FloatingActionButton NativeScript plugin for Material Design Floating Action Button UI component. Installation Nativescript 7+: ns plugin add @nstudio/nativescript-floatingactionbutton Na