Coder Social home page Coder Social logo

googlechromelabs / imagecapture-polyfill Goto Github PK

View Code? Open in Web Editor NEW
182.0 23.0 32.0 76 KB

MediaStream ImageCapture polyfill. Take photos from the browser as easy as .takePhoto().then(processPhoto)

Home Page: https://googlechromelabs.github.io/imagecapture-polyfill/

License: Apache License 2.0

JavaScript 100.00%
imagecapture polyfill camera takephoto getusermedia

imagecapture-polyfill's Introduction

ImageCapture polyfill

Build Status Dependency Status devDependency Status

ImageCapture is a polyfill for the MediaStream Image Capture API.

Status

As of June 2017, the ImageCapture spec is relatively stable. Chrome supports the API starting with M59 (earlier versions require setting a flag) and Firefox has partial support behind a flag. See the ImageCapture browser support page for details.

Prior art

Prior to this API, in order to take a still picture from the device camera, two approaches have been used:

  1. Set the source of a <video> element to a stream obtained via navigator[.mediaDevices].getUserMedia, then use a 2D canvas context to drawImage from that video. The canvas can return a URL to be used as the src attribute of an <img> element, via .toDataURL('image/<format>'). (1, 2)
  2. Use the HTML Media Capture API, i.e. <input type="file" name="image" accept="image/*" capture>

Demo

The demo currently shows grabFrame() and takePhoto().

Quick start

yarn add image-capture

Or, with npm:

npm install --save image-capture

In your JS code:

let videoDevice;
let canvas = document.getElementById('canvas');
let photo = document.getElementById('photo');

navigator.mediaDevices.getUserMedia({video: true}).then(gotMedia).catch(failedToGetMedia);

function gotMedia(mediaStream) {
  // Extract video track.
  videoDevice = mediaStream.getVideoTracks()[0];
  // Check if this device supports a picture mode...
  let captureDevice = new ImageCapture(videoDevice);
  if (captureDevice) {
    captureDevice.takePhoto().then(processPhoto).catch(stopCamera);
    captureDevice.grabFrame().then(processFrame).catch(stopCamera);
  }
}

function processPhoto(blob) {
  photo.src = window.URL.createObjectURL(blob);
}

function processFrame(imageBitmap) {
  canvas.width = imageBitmap.width;
  canvas.height = imageBitmap.height;
  canvas.getContext('2d').drawImage(imageBitmap, 0, 0);
}

function stopCamera(error) {
  console.error(error);
  if (videoDevice) videoDevice.stop();  // turn off the camera
}

photo.addEventListener('load', function () {
  // After the image loads, discard the image object to release the memory
  window.URL.revokeObjectURL(this.src);
});

Methods

Start by constructing a new ImageCapture object:

let captureDevice;

navigator.mediaDevices.getUserMedia({video: true}).then(mediaStream => {
  captureDevice = new ImageCapture(mediaStream.getVideoTracks()[0]);
}).catch(...)

Please consult the spec for full detail on the methods.

constructor(videoStreamTrack)

Takes a video track and returns an ImageCapture object.

getPhotoCapabilities

TBD

setOptions

TBD

takePhoto

Capture the video stream into a Blob containing a single still image.

Returns a Promise that resolves to a Blob on success, or is rejected with DOMException on failure.

captureDevice.takePhoto().then(blob => {
  
}).catch(error => ...);

grabFrame

Gather data from the video stream into an ImageBitmap object. The width and height of the ImageBitmap object are derived from the constraints of the video stream track passed to the constructor.

Returns a Promise that resolves to an ImageBitmap on success, or is rejected with DOMException on failure.

captureDevice.grabFrame().then(imageBitmap => {
  
}).catch(error => ...);

Compatibility

The polyfill has been tested to work in current browsers:

  • Chrome 55+
  • Firefox 49+
  • Chrome 52+ for Android
  • Firefox 48+ for Android

For the widest compatibility, you can additionally load the WebRTC adapter. That will expand support to:

  • Chrome 53

For older browsers that don't support navigator.getUserMedia, you can additionally load Addy Osmani's shim with optional fallback to Flash - getUserMedia.js. Alternatively, the getUserMedia wrapper normalizes error handling and gives an error-first API with cross-browser support.

Development

yarn
yarn run dev

npm

npm install
npm run dev

To make your server accessible outside of localhost, run npm/yarn run lt.

Before committing, make sure you pass yarn/npm run lint without errors, and run yarn/npm run docs to generate the demo.

imagecapture-polyfill's People

Contributors

andsouto avatar artoria2e5 avatar dandv avatar fluorescenthallucinogen avatar sinedooo 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

imagecapture-polyfill's Issues

Can't find variable: ImageCapture on Safari iOS 14

Hello,

I've been trying to use ImageCapture-polyfill and everything works fine on Android.
On iOS I'm encountering this error:

Unhandled Promise Rejection: ReferenceError: Can't find variable: ImageCapture

This is my code where ImageCapture is called:

var handleSuccess = function(stream) {
        player.srcObject = stream;
        const track = stream.getVideoTracks()[0];
        track.applyConstraints({advanced : [{focusMode: "continuous"}]});
        const capabilities = track.getCapabilities();
        const settings = track.getSettings();
        let {width, height} = stream.getTracks()[0].getSettings();
        console.log(`${width}x${height}`); 
        console.log(capabilities)
        console.log(settings)
        imageCapture = new ImageCapture(track);
    };

I've tested on two iPhones:

  • iPhone SE 2020, Safari, iOS 14.4
  • iPhone X, Safari, iOS 14.3

Are there any suggestions available for a solution or workaround?
Thank you

Consider providing UMD modules

Hi, since this is a polyfill I think that it makes sense to provide a UMD module under the lib folder to the file can be loaded as a script in a webpage without transpiling.

Currently if someone downloads the project when its included in a webpage like this

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<script src="node_modules/image-capture/lib/imagecapture.js"></script>
</head>

<body>
</body>

</html>

the following error is thrown:

Uncaught SyntaxError: Unexpected token export

While the package.json gives 2 different I think that the lib version should be published like an UMD module, while the src can be kept as an esnext module.

Making this change is as easy as updating the package.json file and force babel to use umd modules.

"babel": {
    "presets": [
      [
        "es2015",
        {
          "modules":  "umd"
        }
      ]
    ]
  }

Regards

Uncaught SyntaxError: Unexpected token 'export'

I'm trying to install the imagecapture.js into my existing project but I get the following (I guess) when including the file:

<SCRIPT type='text/JavaScript' SRC='..../javascript/imagecapture.js'></SCRIPT>

The browser console shows Uncaught SyntaxError: Unexpected token 'export' ....pointing at the line that reads... export let ImageCapture = window.ImageCapture;

Polyfill doesn't work

Steps to reproduce:

  • Use a browser that doesn't support MediaStream Image Capture API.
  • Use the polyfill.

Expected results without using the polyfill:

'ImageCapture' in window // returns false

Expected results with using the polyfill:

'ImageCapture' in window // returns true

Actual results with using the polyfill:

'ImageCapture' in window // returns false

Does this polyfill strip all metadata of an image?

I am trying to get focal length of the webcam/selfie cam from the image taken using ImageCapture but in browsers where this is not supported and polyfill is being used I am seeing no metadata. But for chrome where ImageCapture is available natively, I can see the metadata.
Is there any option which will help me preserve the metadata?

DOMException: setOptions failed on takePhoto()

Using your own Quick Start example in Chrome Version 64.0.3282.186 (Official Build) (64-bit), the error DOMException: setOptions failed is thrown when takePhoto() is called:

function gotMedia(mediaStream) {
  // Extract video track.
  videoDevice = mediaStream.getVideoTracks()[0];
  // Check if this device supports a picture mode...
  let captureDevice = new ImageCapture(videoDevice);
  if (captureDevice) {
    captureDevice.takePhoto().then(processPhoto).catch(stopCamera);
    captureDevice.grabFrame().then(processFrame).catch(stopCamera);
  }
}

Minified version

Please add minified version (lib/imagecapture.min.js) to the package.

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.