Giter VIP home page Giter VIP logo

canvascameraplugin's Introduction

NPM Version NPM Downloads Codacy Badge

Cordova/Capacitor CanvasCamera plugin

Plugin's Purpose

The purpose of the plugin is to capture video to preview camera in a web page's canvas element. Allows to select front or back camera and to control the flash.


Table of Contents

  1. Working Demo
  2. Supported Platforms
  3. Dependencies
  4. Installation
  5. Using the plugin (JavaScript)
  6. Using the plugin (TypeScript)

Working Demo

Cordova

Capacitor


Supported Platforms

  • iOS
  • Android

Dependencies

  • Cordova : Cordova dependencies are managed with plugman. plugman will check all dependencies and install them if they are missing.

  • Capacitor : Capacitor dependencies are managed with npm. npm will check all dependencies and install them if they are missing.


Installation

Table of Contents

  1. Installation in a Cordova project
  2. Installation in a Capacitor project

Installation in a Cordova project

Adding the Plugin to your project

Through the Command-line Interface:

cordova plugin add com.virtuoworks.cordova-plugin-canvascamera
# Add the plugin to your platforms (Android and/or iOS)
cordova prepare

Removing the Plugin from your project

Through the Command-line Interface:

cordova plugin remove com.virtuoworks.cordova-plugin-canvascamera

Installation in a Capacitor project

Adding the Plugin to your project

Through the Command-line Interface:

npm install com.virtuoworks.cordova-plugin-canvascamera
# Build the Angular project using the plugin
npm run build
# Add the plugin to your platforms (Android and/or iOS)
npx cap sync

Removing the Plugin from your project

Through the Command-line Interface:

npm remove com.virtuoworks.cordova-plugin-canvascamera
# Remove the plugin from your platforms (Android and/or iOS)
npx cap sync

Using the plugin (JavaScript)

The plugin creates the object window.plugin.CanvasCamera with the following methods:

Plugin initialization

The plugin and its methods are not available before the deviceready event has been fired. Call initialize with a reference to the canvas object used to preview the video and a second, optional, reference to a thumbnail canvas.

document.addEventListener(
  'deviceready',
  function () {
    // Call the initialize() function with canvas element reference
    var objCanvas = document.getElementById('canvas');
    window.plugin.CanvasCamera.initialize(objCanvas);
    // window.plugin.CanvasCamera is now available
  },
  false
);

start

Start capturing video as images from camera to preview camera on web page.
capture callback function will be called with image data (image file url) each time the plugin takes an image for a frame.

window.plugin.CanvasCamera.start(options);

This function starts a video capturing session, then the plugin takes each frame as a JPEG image and gives its url to web page calling the capture callback function with the image url(s).
The capture callback function will draw the image inside a canvas element to display the video.

Example

var options = {
  cameraFacing: 'front',
};
window.plugin.CanvasCamera.start(options);

flashMode

Set flash mode for camera.

window.plugin.CanvasCamera.flashMode(true);

cameraPosition

Change input camera to 'front' or 'back' camera.

window.plugin.CanvasCamera.cameraPosition('front');

Options

Optional parameters to customize the settings.

{
    width: 352,
    height: 288,
    canvas: {
      width: 352,
      height: 288
    },
    capture: {
      width: 352,
      height: 288
    },
    fps: 30,
    use: 'file',
    flashMode: false,
    thumbnailRatio: 1/6,
    cameraFacing: 'front', // or 'back'
    onBeforeDraw: function(frame){
      // do something before drawing a frame
      // frame.image; // HTMLImageElement
      // frame.element; // HTMLCanvasElement
    },
    onAfterDraw: function(frame){
      // do something after drawing a frame
      // frame.image.src; // file path or base64 data URI
      // frame.element.toDataURL(); // requested base64 data URI
    }
}
  • width : Number, optional, default : 352, width in pixels of the video to capture and the output canvas width in pixels.

  • height : Number, optional, default : 288, height in pixels of the video to capture and the output canvas height in pixels.

  • capture.width : Number, optional, default : 352, width in pixels of the video to capture.

  • capture.height : Number, optional, default : 288, height in pixels of the video to capture.

  • canvas.width : Number, optional, default : 352, output canvas width in pixels.

  • canvas.height : Number, optional, default : 288, output canvas height in pixels.

  • fps : Number, optional, default : 30, desired number of frames per second.

  • cameraFacing : String, optional, default : 'front', 'front' or 'back'.

  • flashMode : Boolean, optional, default : false, a boolean to set flash mode on/off.

  • thumbnailRatio : Number, optional, default : 1/6, a ratio used to scale down the thumbnail.

  • use : String, optional, default : file, file to use files for rendering (lower CPU / higher storage) or data to use base64 jpg data for rendering (higher cpu / lower storage).

  • onBeforeDraw : Function, optional, default : null, callback executed before a frame has been drawn. frame contains the canvas element, the image element, the tracking data, ...

  • onAfterDraw : Function, optional, default : null, callback executed after a frame has been drawn. frame contains the canvas element, the image element, the tracking data, ...

Usage

Full size video only

let fullsizeCanvasElement = document.getElementById('fullsize-canvas');

CanvasCamera.initialize(fullsizeCanvasElement);

let options: CanvasCamera.CanvasCameraOptions = {
  cameraFacing: 'back',
  onAfterDraw: function (frame) {
    // do something with each frame
    // frame.image.src; // file path or base64 data URI
    // frame.element.toDataURL(); // requested base64 data URI
  },
};

CanvasCamera.start(options);

With thumbnail video

let fullsizeCanvasElement = document.getElementById('fullsize-canvas');
let thumbnailCanvasElement = document.getElementById('thumbnail-canvas');

CanvasCamera.initialize(fullsizeCanvasElement, thumbnailCanvasElement);

let options: CanvasCamera.CanvasCameraOptions = {
  cameraFacing: 'front',
  fps: 15,
  thumbnailRatio: 1 / 6,
  onAfterDraw: function (frame) {
    // do something with each frame of the fullsize canvas element only
    // frame.image.src; // file path or base64 data URI
    // frame.element.toDataURL(); // requested base64 data URI
  },
};

CanvasCamera.start(options);

Using the plugin (TypeScript)

TypeScript/Angular ≥ 2 support

CanvasCamera plugin ≥1.2.1 (current, typescript version)

The plugin was entirely re-written in typescript and the type definition file CanvasCamera.d.ts included in the plugin.

For exemple, in your typescript file :

import { CanvasCameraUserOptions } from 'com.virtuoworks.cordova-plugin-canvascamera';

To import the type for the options object.

CanvasCamera plugin ≤1.2.0 (prior to typescript version)

The CanvasCamera 1.2.0 plugin type definition has been added to the DefinitelyTyped repository (see commit here) thanks to a benevolent contributor.

If you wish to install the type definition file :

npm install --save @types/cordova-plugin-canvascamera

You can check this NPM page for more informations about this type definition.

The plugin creates the object window.plugin.CanvasCamera with the following methods:

Important :

This TypeScript documentation is intended for CanvasCamera ≥1.2.1 with the provided CanvasCamera.d.ts.

The CanvasCamera ≥1.2.1 plugin might not be compatible with the definitely typed CanvasCamera type definition.

The plugin creates a global CanvasCamera variable. Importing any type from com.virtuoworks.cordova-plugin-canvascamera augments the global namespace with this new variable.

No need to import CanvasCamera type from the plugin to use its implementation.

From now on, we assume that we are in an Angular component

Plugin options

Optional parameters to customize the settings.

import { Component, AfterViewInit } from '@angular/core';
import {
    CanvasCameraUserOptions,
    CanvasCameraUseImageAs,
    CanvasCameraCameraFacing,
    CanvasCameraFrame
} from 'com.virtuoworks.cordova-plugin-canvascamera';

// ...
export class SomeAngularComponent implements AfterViewInit {

  // ...

  private use: CanvasCameraUseImageAs = 'file';
  private position: CanvasCameraCameraFacing = 'back';

  private options: CanvasCameraUserOptions = {
    width: 320,
    height: 240,
    canvas: {
      width: 320,
      height: 240,
    },
    capture: {
      width: 320,
      height: 240,
    },
    use: use,
    fps: 30,
    flashMode: this.flash,
    hasThumbnail: true,
    thumbnailRatio: 1 / 6,
    cameraFacing: this.position,
    onBeforeDraw: <CanvasCameraFrame>(frame) => {
      // do something before drawing a frame
      // frame.image; // HTMLImageElement
      // frame.element; // HTMLCanvasElement
    },
    onAfterDraw: <CanvasCameraFrame>(frame) =>{
      // do something after drawing a frame
      // frame.image.src; // file path or base64 data URI
      // frame.element.toDataURL(); // requested base64 data URI
    }
  };

  //...

}
  • width : Number, optional, default : 352, width in pixels of the video to capture and the output canvas width in pixels.

  • height : Number, optional, default : 288, height in pixels of the video to capture and the output canvas height in pixels.

  • capture.width : Number, optional, default : 352, width in pixels of the video to capture.

  • capture.height : Number, optional, default : 288, height in pixels of the video to capture.

  • canvas.width : Number, optional, default : 352, output canvas width in pixels.

  • canvas.height : Number, optional, default : 288, output canvas height in pixels.

  • fps : Number, optional, default : 30, desired number of frames per second.

  • cameraFacing : String, optional, default : 'front', 'front' or 'back'.

  • flashMode : Boolean, optional, default : false, a boolean to set flash mode on/off.

  • thumbnailRatio : Number, optional, default : 1/6, a ratio used to scale down the thumbnail.

  • use : String, optional, default : file, file to use files for rendering (lower CPU / higher storage) or data to use base64 jpg data for rendering (higher cpu / lower storage).

  • onBeforeDraw : Function, optional, default : null, callback executed before a frame has been drawn. frame contains the canvas element, the image element, the tracking data, ...

  • onAfterDraw : Function, optional, default : null, callback executed after a frame has been drawn. frame contains the canvas element, the image element, ...

Plugin initialization

Call initialize with a reference to the canvas object used to preview the video and a second, optional, referencing a thumbnail canvas.

import { Component, ViewChild, AfterViewInit, ElementRef } from '@angular/core';
import {
    CanvasCameraUserOptions,
    CanvasCameraUseImageAs,
    CanvasCameraCameraFacing,
    CanvasCameraFrame
} from 'com.virtuoworks.cordova-plugin-canvascamera';

// ...
export class SomeAngularComponent implements AfterViewInit {

  // ...

  // Referencing a template HTMLCanvasElement (<canvas #fullsize/>)
  @ViewChild('fullsize') fullsizeCanvas: ElementRef;
  // (Optional) Referencing a template HTMLCanvasElement (<canvas #thumbnail/>)
  @ViewChild('thumbnail') thumbnailCanvas: ElementRef;

  ngAfterViewInit() {
    CanvasCamera.initialize({
      fullsize: this.fullsizeCanvas.nativeElement,
      thumbnail: this.thumbnailCanvas.nativeElement, // optional
    });
  }

  // ...

}

start

Start capturing video as images from camera to preview camera on web page.
capture callback function will be called with image data (image file url) each time the plugin takes an image for a frame.

import { Component, ViewChild, AfterViewInit, ElementRef } from '@angular/core';
import {
    CanvasCameraUserOptions,
    CanvasCameraUseImageAs,
    CanvasCameraCameraFacing,
    CanvasCameraFrame
} from 'com.virtuoworks.cordova-plugin-canvascamera';

// ...
export class SomeAngularComponent implements AfterViewInit {

  private options: CanvasCameraUserOptions = {}

  // ...

  start() {
    if (CanvasCamera) {
      CanvasCamera.start(
        this.options,
        (error) => {
          console.log('[CanvasCamera start]', 'error', error);
        },
        (data) => {
          console.log('[CanvasCamera start]', 'data', data);
        }
      );
    }
  }

  // ...

}

This function starts a video capturing session, then the plugin takes each frame as a JPEG image and gives its url to web page calling the capture callback function with the image url(s).
The capture callback function will draw the image inside a canvas element to display the video.

flashMode

Set flash mode for camera.

import { Component, ViewChild, AfterViewInit, ElementRef } from '@angular/core';
import {
    CanvasCameraUserOptions,
    CanvasCameraUseImageAs,
    CanvasCameraCameraFacing,
    CanvasCameraFrame
} from 'com.virtuoworks.cordova-plugin-canvascamera';

// ...
export class SomeAngularComponent implements AfterViewInit {

  private flash = true;

  // ...

  flashMode() {
    if (CanvasCamera) {
      CanvasCamera.flashMode(
        this.flash,
        (error) => {
          console.log('[CanvasCamera start]', 'error', error);
        },
        (data) => {
          console.log('[CanvasCamera start]', 'data', data);
        }
      );
    }
  }

  // ...

}

cameraPosition

Change input camera to 'front' or 'back' camera.

import { Component, ViewChild, AfterViewInit, ElementRef } from '@angular/core';
import {
    CanvasCameraUserOptions,
    CanvasCameraUseImageAs,
    CanvasCameraCameraFacing,
    CanvasCameraFrame
} from 'com.virtuoworks.cordova-plugin-canvascamera';

// ...
export class SomeAngularComponent implements AfterViewInit {

  private position: CanvasCameraCameraFacing = 'back';

  // ...

  cameraPosition() {
    if (CanvasCamera) {
      CanvasCamera.cameraPosition(
        this.position,
        (error) => {
          console.log('[CanvasCamera cameraPosition]', error);
        },
        (data: CanvasCameraData) => {
          console.log('[CanvasCamera cameraPosition]', 'data', data);
        }
      );
    }
  }

  // ...

}

Contributing

  1. Fork it
  2. Install development dependencies with npm i
  3. Create your feature branch (git checkout -b my-new-feature)
  4. src/browser/src/CanvasCamera.ts is the source typescript file
  5. npm run build or npm run watch to build a new www/CanvasCamera.js output file
  6. Commit your changes (git commit -am 'Added some feature')
  7. Push to the branch (git push origin my-new-feature)
  8. Create new Pull Request

License

This software is released under the MIT License.

canvascameraplugin's People

Contributors

asennikov avatar beeramtheman avatar chaitanyakhurana100 avatar colin227 avatar dekwilde avatar donaldp24 avatar iwilliams avatar sami-radi avatar sebastien-guillon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

canvascameraplugin's Issues

installation on ionic 3

Plugin doesn't support this project's cordova-android version. cordova-android: 7.1.4, failed version requirement:
<6.3.0

cant install

while trying to use the demo i cant install the plugin through cordova command line
i get this error
Error: Failed to fetch plugin https://github.com/dekwilde/CanvasCameraPlugin.git via registry.
Probably this is either a connection problem, or plugin spec is incorrect.
Check your connection and plugin name/version/URL.
please if u can update to solve this issue
regards

How to Record Video?

Is there a way to Record Videos on both Android / IOS platform and get file URI to upload that video to server???

cordova-android: 7.0.0

Hi all.

In cordova-android: 7.0.0 it is not possible to install 'cordova-plugin-compat' anymore. Seems, that it is obsolete and its functions are covered by the new engine. Unfortunately, CanvasCameraPlugin depends on it, so it seems not to be possible to update apps to cordova-android: 7.0.0.

onAfterDraw not called in v1.0.8

after updating to version 1.0.8 onAfterDraw is not called anymore, please check in your environment, I will reproduce if needed.

Any suggestions on how to install this on a Meteor app?

I have a meteor app that I'm trying to capture Video and display it in a canvas tag that runs on iOS. I can't figure out how to install this plugin.

What I have done is to create the borilerplate meteor example and add the plugin. Here are my steps in meteor:

$ meteor create --example angular2-boilerplate
$ meteor add-platform ios
$ meteor add cordova:com.virtuoworks.cordova-plugin-canvascamera@https://github.com/VirtuoWorks/CanvasCameraPlugin#b634fe23fb058ef3182cf0f37db13183937ad47f

Sow when I run the build command I get the following error:
=> Errors prevented startup:

While installing Cordova plugins:
error: Meteor no longer supports installing Cordova plugins from arbitrary tarball URLs. You can either add a plugin from a Git URL with a SHA reference, or from a
local path. (Attempting to install from https://github.com/VirtuoWorks/CanvasCameraPlugin#b634fe23fb058ef3182cf0f37db13183937ad47f.)

=> Your application has errors. Waiting for file change.

Mechanism for actual image capture?

Is there a recommended mechanism for actual file capture, or must we simply "scrape" the canvas with a toDataURL()? Attempting to use the frame object is proving unfruitful. Since it's only available in a Cordova context, and I can't JSON.stringify it without error, I'm unable to verify what it contains or see how to use it. Please advise.

Image data in capture resolution

Hello,

i there any proved/suggested way how to get image data in resolution specified by capture options?

As of following line of code

fullsizeData = getResizedAndRotatedImage(fullsizeData, mCanvasWidth, mCanvasHeight, displayOrientation);

Fullsized data are shrinked to canvas size into resulting data/file.

Canvas data are only for preview, but i need bigger resolution for live data processing.

I found two ways how to do it:

  • set canvas size bigger and resize it by css (works, but wasting some resources due to fps updates)
  • modify java code to add original sized picture data into some result

Is there any better way how to do it?

Thank you

declares <uses-feature> for non-existing hardware feature; prevents listing on Google Play

Hello,

Today I tried to release an app which uses this plugin and when uploading to the Google Play store, the app shows a "Supported Devices" list of ZERO.

Working with LiveChat support at Goolge Play store, they alerted me to an issue with the android.hardware.camera.setParameters feature. Upon researching, I found that this plugin injects into my AndroidManifest.xml.

It appears there is no such Hardware feature, at least I don't see it listed on this reference page: https://developer.android.com/guide/topics/manifest/uses-feature-element.html#features-reference

By requiring a feature that no devices have, Google Play excludes the app from all devices.

The app will still sideload fine onto a test device over USB, which is why I did not catch this issue during testing. The test device does not apparently care whether this manifest element exists, but Google Play store does.

Potential Memory Leak Issue

First of all, thanks for your work on this plugin. 🙏🏻

Having said that, I think I have stumbled upon a memory leak problem. When I use data mode (on iOS), the page memory is always increasing (as seen in the Safari Web Inspector) and, eventually, the application crashes. To try and troubleshoot this problem, I installed the cordova-plugin-memory-warning-ios-android plugin and I do receive memorywarning events repeatedly until the crash.

To alleviate the problem, I tried calling frame.recycle() in the onAfterDraw callback but it does not change the situation. By the way, frame.image and frame.element both return undefined in my setup.

When I switch to file mode, I do receive the occasional memorywarning event but the page memory usually goes back down and it does not crash. However, file mode causes me another problem which I reported in #29.

Anyway, if it was possible to free a frame's memory when we are done with it, I would be a very hapy man. Thanks in advance for your help.

P.S. If it helps, here are the options I'm using:

{

    canvas: {
      width: 1136,
      height: 640
    },

    capture: {
      width: 1280,
      height: 2272
    },

    hasThumbnail: true,
    thumbnailRatio: 1/4,

    cameraFacing: "back",

}

Not working on iOS 10

Hi, i'm getting

2017-05-30 00:33:55.721560+0200 Projector[15249:6507291] Apache Cordova native platform version 4.4.0 is starting.
2017-05-30 00:33:55.722156+0200 Projector[15249:6507291] Multi-tasking -> Device: YES, App: YES
2017-05-30 00:33:55.728249+0200 Projector[15249:6507291] 

Started backup to iCloud! Please be careful.
Your application might be rejected by Apple if you store too much data.
For more information please read "iOS Data Storage Guidelines" at:
https://developer.apple.com/icloud/documentation/data-storage/
To disable web storage backup to iCloud, set the BackupWebStorage preference to "local" in the Cordova config.xml file
2017-05-30 00:33:55.792209+0200 Projector[15249:6507291] Using UIWebView
2017-05-30 00:33:55.793591+0200 Projector[15249:6507291] [CDVTimer][handleopenurl] 0.065982ms
2017-05-30 00:33:55.794908+0200 Projector[15249:6507291] [CDVTimer][intentandnavigationfilter] 1.269996ms
2017-05-30 00:33:55.794985+0200 Projector[15249:6507291] [CDVTimer][gesturehandler] 0.043988ms
2017-05-30 00:33:55.795060+0200 Projector[15249:6507291] [CDVTimer][canvascamera] 0.051975ms
2017-05-30 00:33:55.795081+0200 Projector[15249:6507291] [CDVTimer][TotalPluginStartup] 1.574039ms
2017-05-30 00:33:55.839457+0200 Projector[15249:6507332] libMobileGestalt MobileGestaltSupport.m:153: pid 15249 (Projector) does not have sandbox access for frZQaeyWLUvLjeuEK43hmg and IS NOT appropriately entitled
2017-05-30 00:33:55.839506+0200 Projector[15249:6507332] libMobileGestalt MobileGestalt.c:550: no access to InverseDeviceID (see <rdar://problem/11744455>)
2017-05-30 00:33:56.083797+0200 Projector[15249:6507291] Resetting plugins due to page load.
2017-05-30 00:33:56.122436+0200 Projector[15249:6507291] Finished load of: file:///var/containers/Bundle/Application/EFAAF37D-E0B9-4DFC-B954-1607221C0F82/Projector.app/www/index.html

in xCode.
I'm using cordova 7.0.1.

How to apply a green screen to the canvas?

I found the function to apply green screen to webcam but I am confused about applying it to this plugin. How to apply a green screen to the canvas?

function draw() {
    var frame = readFrame();

    if (frame) {
      replaceGreen(frame.data);
      context.putImageData(frame, 0, 0);
    }

    // Wait for the next frame.
    requestAnimationFrame(draw);
  }

  function readFrame() {
    try {
      context.drawImage(video, 0, 0, width, height);
    } catch (e) {
      // The video may not be ready, yet.
      return null;
    }

    return context.getImageData(0, 0, width, height);
  }

  function replaceGreen(data) {
    var len = data.length;

    for (var i = 0, j = 0; j < len; i++, j += 4) {
      // Convert from RGB to HSL...
      var hsl = rgb2hsl(data[j], data[j + 1], data[j + 2]);
      var h = hsl[0], s = hsl[1], l = hsl[2];

      // ... and check if we have a somewhat green pixel.
	  //if (h >= 90 && h <= 160 && s >= 25 && s <= 90 && l >= 20 && l <= 75) {
	  
      if (h >= 65 && h <= 185 && s >= 0 && s <= 115 && l >= 0 && l <= 100) {
        data[j + 3] = 0;
      }
    }
  }

  function rgb2hsl(r, g, b) {
    r /= 255; g /= 255; b /= 255;

    var min = Math.min(r, g, b);
    var max = Math.max(r, g, b);
    var delta = max - min;
    var h, s, l;

    if (max == min) {
      h = 0;
    } else if (r == max) {
      h = (g - b) / delta;
    } else if (g == max) {
      h = 2 + (b - r) / delta;
    } else if (b == max) {
      h = 4 + (r - g) / delta;
    }

    h = Math.min(h * 60, 360);

    if (h < 0) {
      h += 360;
    }

    l = (min + max) / 2;

    if (max == min) {
      s = 0;
    } else if (l <= 0.5) {
      s = delta / (max + min);
    } else {
      s = delta / (2 - max - min);
    }

    return [h, s * 100, l * 100];
  }

Enable ability to set orientation

Here is a little proposal: it would be really cool if we could force the orientation used by the camera (portrait or landscape). Currently, there does not seem to be a way to do that.

By the way, I tried doing it with cordova-plugin-screen-orientation but, even though the reported orientation changes, the behaviour of CanvasCameraPlugin does not.

I guess the most logical way would be for CanvasCameraPlugin to simply follow window.screen.orientation. This way it would allow us to set it through the screen orientation plugin (or other) without adding bloat to CanvasCameraPlugin.

Thanks for your work on this plugin!

Poor performance on Android

The performance is poor running on a modern android smartphone.
When I set the resolution to 1280x720, the fps drops below 30 (lags).
If I set use: 'data' it's even worse.
Is there any way to speed things up or is it a canvas limitation?

Proposal: Horizontal and vertical flipping of the output ...

Most FrontCams provide a horizontally mirrored image. Since I want to record this picture, I have to mirror the mirroring :)

The most efficient way is in CanvasCamera.js.

My proposal:
Expand the options by Booleans flipH and flipV.
Replace CanvasCamera.Renderer.prototype.draw with:

        ```
CanvasCamera.Renderer.prototype.draw = function (frame) {
            this.canvasCamera.dispatch ('beforeframerendering', this, frame);
            if (frame) {
                if (this.canvasCamera.options.flipH === true || this.canvasCamera.options.flipV === true) {
                    var scaleH = this.canvasCamera.options.flipH? -1: 1; // Set horizontal scale to -1 if flip horizontal
                    var scaleV = this.canvasCamera.options.flipV? -1: 1; // Set verical scale to -1 if flip vertical
                    var posX = this.canvasCamera.options.flipH? frame.dWidth * -1: 0; // set x position to -100% if flip horizontal
                    var posY = this.canvasCamera.options.flipV? frame.dHeight * -1: 0; // Set y position to -100% if flip vertical
                    this.context.save (); // save the current state
                    this.context.scale (scaleH, scaleV); // Set scale to flip the image
                    this.context.drawImage (frame.image, frame.sx, frame.sy, frame.sWidth, frame.sHeight, posX, posY, frame.dWidth, frame.dHeight); // draw the image
                    this.context.restore (); // Restore the last saved state
                }
                else {
                    this.context.drawImage (frame.image, frame.sx, frame.sy, frame.sWidth, frame.sHeight, frame.dx, frame.dy, frame.dWidth, frame.dHeight);
                }
            }
            this.canvasCamera.dispatch ('afterframerendering', this, frame);
            return this;
        };

Failed to load resource error

I have a project that uses the CanvasCameraPlugin. With a vast majority of frames, it works fine. But, I keep getting this error randomly in the console:

[Error] Failed to load resource: The requested URL was not found on this server. (f2-canvascamera.jpg, line 0)

In fact, it's not so random. It happens shortly after I start the camera (after 1 or 2 frames), then it happens randomly and then one last time when I stop the camera (each time). But, as I said, most frames are loaded normally (I can see them in the Resources tab of the Safari Inspector):

image

These are my options:

{fps: 2, use: "file", cameraFacing: "front"}

I'm not using onCapture, onBeforeDraw or onAfterDraw.

It's as though the images are being prematurely deleted from the device. Perhaps it's something else but I'm running out of ideas. Do you think this can be a bug with the library? Or maybe you would know what I could check to try and solve this?

Thanks a lot!

fps not work

hi,
i have this code
document.addEventListener('deviceready', function () {
var objCanvas = document.getElementById('canvas');
window.plugin.CanvasCamera.initialize(objCanvas);
var cameraOptions = {
width: window.screen.width,
height: window.screen.height,
canvas: {
width: window.screen.width,
height: window.screen.height
},
capture: {
width: window.screen.width,
height: window.screen.height
},
fps: 1,
use: 'data',
flashMode: false,
thumbnailRatio: 1/6,
cameraFacing: 'front',
onBeforeDraw: function(frame){

      },
      onAfterDraw: function(frame){
        //save in list
      }
    };
    window.plugin.CanvasCamera.start($scope.cameraOptions, function(error) {
      
    }, function(data) {
      
    });
}, true);

the camera is ready in canvas, but capture 30 frames/sec. My array contains after 2 seconds about 60 frames. But i have set 1 fps and i suppose max 2 frames after 2 seconds.

Typescript types not working

Hello i need run on ionic with angular but typescript types not working.
`

import * as CanvasCamera from 'cordova-plugin-canvascamera';
`
return this error : File '~/node_modules/@types/cordova-plugin-canvascamera/index.d.ts' is not a module.

Is there any example of the "onAfterDraw"?

I am not sure what data included with the "frame" in the "onAfterDraw" return.

Is there any example on that? Would like to get the image data, is there any image data included in the "frame"

Thank you

Add crossOrigin = 'anonymous' to initialize method in CanvasCamera.js

When you want to copy the canvas inside a cordova app (e.g. in Ionic), you can't copy the canvas because of cross origin problems ("The canvas has been tainted by cross-origin data ").

The fix is only one line inside CanvasCamera.js:

    CanvasCamera.Renderer.prototype.initialize = function() {
        if (this.element) {
            this.context = this.element.getContext('2d');

            this.image = new Image();
            this.image.crossOrigin = 'anonymous'; // <-- this line will fix the problem

            this.image.addEventListener('load', function(event) {
               [...]

Not working on Android

Hi

Thanks for the great plugin. It worked fine on iOS , but is not working on Android Device. Tested on Nexus 6p Nougat. Following was shown in logcat when tested with demo app.

screen shot 2017-06-05 at 7 43 07 pm

Angular 2 support

What about convert the JavaScript in project to TypeScript? For Angular 2 support?

Performance problem when using with Angular

The plugin successively transfers an image to a canvas. In an Angular app, this causes Angular Change Detection to be triggered on every "drawImage" (216 in CanvasCamera.js) and every "clearRect" (206 in CanvasCamera.js) and elsewhere ... up to 60 times a second, which could make the app really slow and a phone melts

In Angular, change detection can be bypassed by zone.runOutSideAngular (...) when drawing on a canvas. But this is not possible in the context of the plugin, since there is no zone.

Solution 1: An Angular version of the plugin that uses runOutSideAngular :)

Solution 2: Optionally provide a wrapper callback that is defined by the app and performs all drawing operations in the context of the wrapper.

Some other ideas to tackle this problem wtihout changing the plugin?

Best Regards

Martin

Plugin not working on cordova-ios 6.1.0

Terrific plugin. Using on cordova-ios 5.1.0. Now I'm upgrading to cordova-ios 6.1.0 in order to get rid of UIWebView and start using WKWebView to get my apps aproved on AppStore. Sadly, app compiles fine but video is not shown on canvas.

Camerastream freezes (stops) when device camera is covered up.

While experimenting using your plugin I noticed a strange behaviour. When I put down my device camera down during the stream and pick it up again, the process freezes.

const options = {
      cameraPosition:'back',
      use:'data',
      onAfterDraw: function(){
        if(!_this.videoReady){
          _this.set('videoReady',true);
        }
        else{
          if(!_this.get('positioned')&&_this.get('streaming')){
          _this.placeIndicators(parseInt(video.style.width), parseInt(video.style.height));
        }
        }
      }
    };
    const canvas = document.getElementById('testCanvas');
    window.plugin.CanvasCamera.initialize(canvas);
    window.plugin.CanvasCamera.start(options);

Testing with an emberJS based cordova app on IOS 12 safari wkwebview.

setOnAfterDraw is not a function...

Hi there.
The error occurs in my project and in your demo:
As soon as I add an empty function to the options I get this error.
I used your sample, i.e.
...
onAfterDraw: function (frame) {
// do something with each frame
}
...

"TypeError: this.canvas.fullsize.setOnAfterDraw is not a function
at CanvasCamera.setRenderingPresets (file:///android_asset/www/plugins/com.virtuoworks.cordova-plugin-canvascamera/www/CanvasCamera.js:492:34)
at CanvasCamera.start (file:///android_asset/www/plugins/com.virtuoworks.cordova-plugin-canvascamera/www/CanvasCamera.js:377:10)

Not running under iOS and Ionic when using "use:'file'"

II ran into problems with an Ionic app and the plugin: when running under android the world is fine, but under iOS and using "use:'file'" I get the console-error:
[Error] Not allowed to load local resource: file:///var/mobile/Containers/Data/Application/CFD416B7-AD48-48A9-9511-AA10E19F92B7/Documents/tmp/f16-canvascamera.jpg?seed=1264145267490218
(anonyme Funktion) (CanvasCamera.js:200)
(anonyme Funktion)
runTask (polyfills.js:3:10845)
invokeTask (polyfills.js:3:16802)
n (polyfills.js:3)

I copied your (working) cordova-example into the blank Ionic starter app and got the same problem.
All whitelisting tricks and a full open security policy don't help.

Using "use:'data'" works is a workaround, but melting down my poor 5S :)

Not working on Android 13

Not working on android 13, gives permission denied error.
I can see a PR for this issue but its not avaibale NPM.

Odd behaviour when using XCode 15 and iOS 17

There are some weird odd behaviour since I updated to iOS 17 and started building with XCode 15.

If the iPad is in landscape, the camera is flipped upside down.
If the iPad is in portrait, the camera is flipped 90 degrees.

However, if I change the iPad orientation, things go back to normal and stays that way even if I close the app.

Any idea what could be causing this odd behaviour?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.