Skip to content

Instantly share code, notes, and snippets.

@jiin
Created July 9, 2015 14:05
Show Gist options
  • Save jiin/f1f7894272d70cafde33 to your computer and use it in GitHub Desktop.
Save jiin/f1f7894272d70cafde33 to your computer and use it in GitHub Desktop.
//
// CanvasCamera.js
// PhoneGap iOS Cordova Plugin to capture Camera streaming into a HTML5 Canvas or an IMG tag.
//
// Created by Diego Araos <[email protected]> on 12/29/12.
//
// MIT License
cordova.define("cordova/plugin/CanvasCamera", function(require, exports, module) {
var exec = require('cordova/exec');
var CanvasCamera = function(){
var _orientation = 'landscape';
var _obj = null;
var _context = null;
var _camImage = null;
var _x = 0;
var _y = 0;
var _width = 0;
var _height = 0;
};
CanvasCamera.prototype.initialize = function(obj) {
this._obj = obj;
var RADIUS = Math.PI / 180;
var _this = this;
this._context = obj.getContext("2d");
this._camImage = new Image();
this._camImage.onload = function() {
_this._context.clearRect(0, 0, (_this._obj.width - 60), _this._obj.height);
if (window.orientation == 90 || window.orientation == -90)
{
_this._context.save();
_this._context.translate((_this._obj.width - 60)/2, _this._obj.height/2);
_this._context.rotate((90 - window.orientation) * RADIUS);
_this._context.drawImage(_this._camImage, 0, 0, 352, 288, -(_this._obj.width - 60) / 2, -_this._obj.height / 2, (_this._obj.width - 60), _this._obj.height);
_this._context.restore();
}
else
{
_this._context.save();
_this._context.translate((_this._obj.width - 60)/2, _this._obj.height/2);
_this._context.rotate((90 - window.orientation) * RADIUS);
_this._context.drawImage(_this._camImage, 0, 0, 352, 288, -_this._obj.height / 2, -(_this._obj.width - 60) / 2, _this._obj.height, (_this._obj.width - 60));
_this._context.restore();
}
/*
GRID LINES
var unit_width = (_this._obj.width - 60) / 100;
var unit_height = _this._obj.height / 100;
_this._context.strokeStyle = '#FFF';
_this._context.beginPath();
_this._context.moveTo( unit_width * 33, 0);
_this._context.lineTo( unit_width * 33, _this._obj.height);
_this._context.stroke();
_this._context.beginPath();
_this._context.moveTo( unit_width * 66, 0);
_this._context.lineTo( unit_width * 66, _this._obj.height);
_this._context.stroke();
_this._context.beginPath();
_this._context.moveTo(0, unit_height * 33);
_this._context.lineTo((_this._obj.width - 60), unit_height * 33);
_this._context.stroke();
_this._context.beginPath();
_this._context.moveTo(0, unit_height * 66);
_this._context.lineTo((_this._obj.width - 60), unit_height * 66);
_this._context.stroke();
*/
_this._context.save();
_this._context.translate((_this._obj.width - 60) / 2, _this._obj.height / 2);
_this._context.beginPath();
_this._context.arc(0, 0, 120, 0, 2 * Math.PI, false);
_this._context.lineWidth = 2;
_this._context.strokeStyle = '#C22F3E';
_this._context.stroke();
_this._context.restore();
};
// register orientation change event
//window.addEventListener('orientationchange', this.doOrientationChange);
//this.doOrientationChange();
};
CanvasCamera.prototype.start = function(options) {
cordova.exec(false, false, "CanvasCamera", "startCapture", [options]);
};
CanvasCamera.prototype.capture = function(data) {
this._camImage.src = data;
};
CanvasCamera.prototype.setFlashMode = function(flashMode) {
cordova.exec(function(){}, function(){}, "CanvasCamera", "setFlashMode", [flashMode]);
};
CanvasCamera.prototype.setCameraPosition = function(cameraPosition) {
cordova.exec(function(){}, function(){}, "CanvasCamera", "setCameraPosition", [cameraPosition]);
};
CanvasCamera.prototype.doOrientationChange = function() {
switch(window.orientation)
{
case -90:
case 90:
this._orientation = 'landscape';
break;
default:
this._orientation = 'portrait';
break;
}
var windowWidth = window.innerWidth;
var windowHeight = this._obj.height;
var pixelRatio = window.devicePixelRatio || 1; /// get pixel ratio of device
this._obj.width = windowWidth;// * pixelRatio; /// resolution of canvas
this._obj.height = windowHeight;// * pixelRatio;
this._obj.style.width = windowWidth + 'px'; /// CSS size of canvas
this._obj.style.height = windowHeight + 'px';
this._x = 0;
this._y = 0;
this._width = windowWidth;
this._height = windowHeight;
};
CanvasCamera.prototype.takePicture = function(onsuccess, onfailure) {
cordova.exec(onsuccess, onfailure, "CanvasCamera", "captureImage", []);
};
var myplugin = new CanvasCamera();
module.exports = myplugin;
});
var CanvasCamera = cordova.require("cordova/plugin/CanvasCamera");
var DestinationType = {
DATA_URL : 0,
FILE_URI : 1
};
var PictureSourceType = {
PHOTOLIBRARY : 0,
CAMERA : 1,
SAVEDPHOTOALBUM : 2
};
var EncodingType = {
JPEG : 0,
PNG : 1
};
var CameraPosition = {
BACK : 0,
FRONT : 1
};
var CameraPosition = {
BACK : 1,
FRONT : 2
};
var FlashMode = {
OFF : 0,
ON : 1,
AUTO : 2
};
CanvasCamera.DestinationType = DestinationType;
CanvasCamera.PictureSourceType = PictureSourceType;
CanvasCamera.EncodingType = EncodingType;
CanvasCamera.CameraPosition = CameraPosition;
CanvasCamera.FlashMode = FlashMode;
module.exports = CanvasCamera;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment