npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nativescript-camera-ins

v3.2.2

Published

Provides API for using device camera

Downloads

11

Readme

Welcome to the nativescript-camera plugin for NativeScript framework

Prerequisites

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

Requesting permissions

Newer API levels of Android and iOS versions are requiring explicit permissions in order the application to have access to the camera and to be able to save photos to the device. Once the user has granted permissions the camera module can be used.

camera.requestPermissions();

Note: Older versions won't be affected by the usage of the requestPermissions method.

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

var camera = require("nativescript-camera");
import * as camera from "nativescript-camera";

Then you are ready to use it:

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

var imageModule = require("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);
    });
import { Image } from "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 taken photo will be saved in "Photos" for Android and in "Camera Roll" in iOS
  • 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

var imageModule = require("ui/image");

var 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);
    });
import { Image } from "ui/image";

var 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);
    });

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.

var isAvailable = camera.isAvailable(); 

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