Return-Path: X-Original-To: apmail-cordova-commits-archive@www.apache.org Delivered-To: apmail-cordova-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 01E5411A26 for ; Fri, 5 Sep 2014 18:08:57 +0000 (UTC) Received: (qmail 29768 invoked by uid 500); 5 Sep 2014 18:08:56 -0000 Delivered-To: apmail-cordova-commits-archive@cordova.apache.org Received: (qmail 29730 invoked by uid 500); 5 Sep 2014 18:08:56 -0000 Mailing-List: contact commits-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cordova.apache.org Delivered-To: mailing list commits@cordova.apache.org Received: (qmail 29713 invoked by uid 99); 5 Sep 2014 18:08:56 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 05 Sep 2014 18:08:56 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 6C122A0AA9D; Fri, 5 Sep 2014 18:08:56 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: anis@apache.org To: commits@cordova.apache.org Date: Fri, 05 Sep 2014 18:08:56 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [01/11] git commit: Added support for the browser Repository: cordova-plugin-camera Updated Branches: refs/heads/master af566264e -> 1ffb14d76 Added support for the browser Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/commit/b2403c60 Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/tree/b2403c60 Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/diff/b2403c60 Branch: refs/heads/master Commit: b2403c607652d2db21d778b39102e57652f813a8 Parents: d53a777 Author: Suraj Pindoria Authored: Fri Aug 29 15:14:41 2014 -0700 Committer: Suraj Pindoria Committed: Fri Aug 29 15:14:41 2014 -0700 ---------------------------------------------------------------------- plugin.xml | 13 +++++ src/browser/CameraProxy.js | 104 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/blob/b2403c60/plugin.xml ---------------------------------------------------------------------- diff --git a/plugin.xml b/plugin.xml index 1bb7b57..9f3bfad 100644 --- a/plugin.xml +++ b/plugin.xml @@ -218,4 +218,17 @@ + + + + + + + + + + + + + http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/blob/b2403c60/src/browser/CameraProxy.js ---------------------------------------------------------------------- diff --git a/src/browser/CameraProxy.js b/src/browser/CameraProxy.js new file mode 100644 index 0000000..5756c48 --- /dev/null +++ b/src/browser/CameraProxy.js @@ -0,0 +1,104 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +function takePicture(success, error, opts) { + if (opts && opts[2] === 1) { + capture(success); + } else { + var input = document.createElement('input'); + input.type = 'file'; + input.name = 'files[]'; + + input.onchange = function(inputEvent) { + var canvas = document.createElement('canvas'); + + var reader = new FileReader(); + reader.onload = function(readerEvent) { + input.parentNode.removeChild(input); + + return success(readerEvent.target.result.replace('data:image/png;base64,', '')); + } + + reader.readAsDataURL(inputEvent.target.files[0]); + }; + + document.body.appendChild(input); + } +} + +function capture(success) { + var localMediaStream; + + var video = document.createElement('video'); + var button = document.createElement('button'); + + video.width = 320; + video.height = 240; + button.innerHTML = 'Capture!'; + + button.onclick = function() { + // create a canvas and capture a frame from video stream + var canvas = document.createElement('canvas'); + canvas.getContext('2d').drawImage(video, 0, 0, 320, 240); + + // convert image stored in canvas to base64 encoded image + var imageData = canvas.toDataURL('img/png'); + imageData = imageData.replace('data:image/png;base64,', ''); + + // stop video stream, remove video and button + localMediaStream.stop(); + video.parentNode.removeChild(video); + button.parentNode.removeChild(button); + + return success(imageData); + } + + document.body.appendChild(video); + document.body.appendChild(button); + + navigator.getUserMedia = navigator.getUserMedia || + navigator.webkitGetUserMedia || + navigator.mozGetUserMedia || + navigator.msGetUserMedia; + + var successCallback = function(stream) { + localMediaStream = stream; + video.src = window.URL.createObjectURL(localMediaStream); + video.play(); + } + + var errorCallback = function(e) { + console.log('Error: ' + e); + } + + if (navigator.getUserMedia) { + navigator.getUserMedia({video: true, audio: true}, successCallback, errorCallback); + } else { + alert('Browser does not support camera :('); + } +} + +module.exports = { + takePicture: takePicture, + cleanup: function(){} +}; + +require("cordova/exec/proxy").add("Camera",module.exports);