0.2.2 • Published 4 years ago

@zwyssigly/image-capture v0.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

WARNING

This Package is under early development and will have breaking changes and bugs !!!

Description

image-capture is a client library to process captured images before upload.

Features

  • get an image from either the file system or directly from camera
  • down scale to image to maximum width and heigth
  • crop the image to an aspect ratio with two differnt modes: contain, cover
  • rotate and flip the image before upload
  • fix orientation from exif tag before upload

Example

import { capturePicture } from '../index';

window.result = null;
window.onload = () => {
  document.getElementById("btn").onclick = () => {
    capturePicture({
      output: 'dataUrl',
      cropColor: '#FF0000',
      maxWidth: 512,
      maxHeight: 512,
      cropRatio: [1, 1],
      cropBehavior: 'contain',
      capture: 'environment',
      debug: true
    }).then(apply);
  };
  document.getElementById("cw").onclick = () => {
    window.result.rotateClockwise().then(apply);
  }
  document.getElementById("ccw").onclick = () => {
    window.result.rotateCounterClockwise().then(apply);
  }
  document.getElementById("fh").onclick = () => {
    window.result.flipHorizontally().then(apply);
  }
  document.getElementById("fv").onclick = () => {
    window.result.flipVertically().then(apply);
  }
}

function apply(result) {
  console.log(result);
  window.result = result;
  document.getElementById('img').src = result.data;
}

Methods

SignatureDescription
capturePicture(options: Options): Promise<PictureResult>open the file dialog or camera and process the return file with the given options
processPicture(file: Blob, options: Options): Promise<PictureResult>in case you already got a file instance

Options

OptionTypeDefaultDescription
capturefalse⎮'environment'⎮'user'falsewhen set, it will open the camera instead of a file dialog on smart phones
maxWidthfalse⎮intfalseimages is scaled down to fit within the given width
maxHeightfalse⎮intfalseimages is scaled down to fit within the given height
maxHeightfalse⎮intfalseimages is scaled down to fit within the given height
orientation'meta'⎮1-8'meta'either get orientation from exif or uses the given orientation. (1 means no rotation or flipping)
cropRatiofalse⎮[int, int]falsewhen set, it will enforce given ratio and crop the picture to fit
cropFit'cover'⎮'contain''cover'defines how the images is fit into the new aspect ratio
cropColorstring'#000000'defines the background color in case cropFit is set to 'contain'
mimeType'keep'⎮string'image/jpeg''keep' keeps the original format
quality0-10.75image quality
output'blob'⎮'dataUrl''blob'how the processed image is returned
debugbool⎮Functionfalsewhen true logs debug information to console otherwise it will use the given Function

PictureResult

SignatureDescription
original: Bloboriginal file
data: Blob⎮stringprocessed image either as blob or as data url
options: Optionsoptions used to process the image
rotateClockwise(): Promise<PictureResult>processes the image again with different orientation
rotateCounterClockwise(): Promise<PictureResult>processes the image again with different orientation
flipHorizontally(): Promise<PictureResult>processes the image again with different orientation
flipVertically(): Promise<PictureResult>processes the image again with different orientation