Compare commits

...

7 Commits

Author SHA1 Message Date
Joe Bowser
c2e0db2b86 CB-8909: Remove unused import from File 2015-04-24 14:24:47 -07:00
Murat Sutunc
8f07f562a2 CB-8404 typo fix cameraproxy.js
There was a missing comma on cameraproxy

github close #89
2015-04-22 14:39:40 -07:00
Murat Sutunc
1e8c836844 CB-8404 Rotate camera feed with device orientation
CR-1

CR-2
2015-04-21 10:56:04 -07:00
Vladimir Kotikov
b131021303 CB-8054 Support taking pictures from file for WP8 2015-04-21 15:22:40 +03:00
Murat Sutunc
0dabe94416 CB-8054 Support taking pictures from file for WP8
codereview

codereview 2
2015-04-16 14:19:01 -07:00
Murat Sutunc
fb8ce21711 CB-8405 Use z-index instead of z-order 2015-04-16 12:44:29 -07:00
Steve Gill
b67e4a09ed CB-8858 Incremented plugin version. 2015-04-15 16:00:21 -07:00
5 changed files with 142 additions and 34 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-camera",
"version": "1.0.0",
"version": "1.0.1-dev",
"description": "Cordova Camera Plugin",
"cordova": {
"id": "cordova-plugin-camera",

View File

@@ -22,7 +22,7 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:rim="http://www.blackberry.com/ns/widgets"
id="cordova-plugin-camera"
version="1.0.0">
version="1.0.1-dev">
<name>Camera</name>
<description>Cordova Camera Plugin</description>
<license>Apache 2.0</license>

View File

@@ -33,7 +33,6 @@ import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.LOG;
import org.apache.cordova.PluginResult;
import org.apache.cordova.file.FileUtils;
import org.json.JSONArray;
import org.json.JSONException;

View File

@@ -53,6 +53,9 @@ module.exports = {
}
};
// https://msdn.microsoft.com/en-us/library/windows/apps/ff462087(v=vs.105).aspx
var windowsVideoContainers = [".avi", ".flv", ".asx", ".asf", ".mov", ".mp4", ".mpg", ".rm", ".srt", ".swf", ".wmv", ".vob"];
var windowsPhoneVideoContainers = [".avi", ".3gp", ".3g2", ".wmv", ".3gp", ".3g2", ".mp4", ".m4v"];
// Resize method
function resizeImage(successCallback, errorCallback, file, targetWidth, targetHeight, encodingType) {
@@ -134,27 +137,99 @@ function resizeImageBase64(successCallback, errorCallback, file, targetWidth, ta
}, function(err) { errorCallback(err); });
}
function takePictureFromFile(successCallback, errorCallback, mediaType, destinationType, targetWidth, targetHeight, encodingType) {
// TODO: Add WP8.1 support
// WP8.1 doesn't allow to use of pickSingleFileAsync method
// see http://msdn.microsoft.com/en-us/library/windows/apps/br207852.aspx for details
// replacement of pickSingleFileAsync - pickSingleFileAndContinue method
// will take application to suspended state and this require additional logic to wake application up
if (navigator.appVersion.indexOf('Windows Phone 8.1') >= 0 ) {
errorCallback('Not supported');
return;
function takePictureFromFile(successCallback, errorCallback, args) {
// Detect Windows Phone
if (navigator.appVersion.indexOf('Windows Phone 8.1') >= 0) {
takePictureFromFileWP(successCallback, errorCallback, args);
} else {
takePictureFromFileWindows(successCallback, errorCallback, args);
}
}
function takePictureFromFileWP(successCallback, errorCallback, args) {
var mediaType = args[6],
destinationType = args[1],
targetWidth = args[3],
targetHeight = args[4],
encodingType = args[5];
/*
Need to add and remove an event listener to catch activation state
Using FileOpenPicker will suspend the app and it's required to catch the PickSingleFileAndContinue
https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn631755.aspx
*/
var filePickerActivationHandler = function(eventArgs) {
if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.pickFileContinuation) {
var file = eventArgs.files[0];
if (!file) {
errorCallback("User didn't choose a file.");
Windows.UI.WebUI.WebUIApplication.removeEventListener("activated", filePickerActivationHandler);
return;
}
if (destinationType == Camera.DestinationType.FILE_URI || destinationType == Camera.DestinationType.NATIVE_URI) {
if (targetHeight > 0 && targetWidth > 0) {
resizeImage(successCallback, errorCallback, file, targetWidth, targetHeight, encodingType);
}
else {
var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).done(function (storageFile) {
successCallback(URL.createObjectURL(storageFile));
}, function () {
errorCallback("Can't access localStorage folder.");
});
}
}
else {
if (targetHeight > 0 && targetWidth > 0) {
resizeImageBase64(successCallback, errorCallback, file, targetWidth, targetHeight);
} else {
Windows.Storage.FileIO.readBufferAsync(file).done(function (buffer) {
var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
successCallback(strBase64);
}, errorCallback);
}
}
Windows.UI.WebUI.WebUIApplication.removeEventListener("activated", filePickerActivationHandler);
}
};
var fileOpenPicker = new Windows.Storage.Pickers.FileOpenPicker();
fileOpenPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
if (mediaType == Camera.MediaType.PICTURE) {
fileOpenPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);
fileOpenPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
}
else if (mediaType == Camera.MediaType.VIDEO) {
fileOpenPicker.fileTypeFilter.replaceAll([".avi", ".flv", ".asx", ".asf", ".mov", ".mp4", ".mpg", ".rm", ".srt", ".swf", ".wmv", ".vob"]);
fileOpenPicker.fileTypeFilter.replaceAll(windowsPhoneVideoContainers);
fileOpenPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.videosLibrary;
}
else {
fileOpenPicker.fileTypeFilter.replaceAll(["*"]);
fileOpenPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
}
Windows.UI.WebUI.WebUIApplication.addEventListener("activated", filePickerActivationHandler);
fileOpenPicker.pickSingleFileAndContinue();
}
function takePictureFromFileWindows(successCallback, errorCallback, args) {
var mediaType = args[6],
destinationType = args[1],
targetWidth = args[3],
targetHeight = args[4],
encodingType = args[5];
var fileOpenPicker = new Windows.Storage.Pickers.FileOpenPicker();
if (mediaType == Camera.MediaType.PICTURE) {
fileOpenPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);
fileOpenPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
}
else if (mediaType == Camera.MediaType.VIDEO) {
fileOpenPicker.fileTypeFilter.replaceAll(windowsVideoContainers);
fileOpenPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.videosLibrary;
}
else {
fileOpenPicker.fileTypeFilter.replaceAll(["*"]);
fileOpenPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
}
fileOpenPicker.pickSingleFileAsync().done(function (file) {
@@ -173,7 +248,6 @@ function takePictureFromFile(successCallback, errorCallback, mediaType, destinat
}, function () {
errorCallback("Can't access localStorage folder.");
});
}
}
else {
@@ -215,18 +289,29 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {
captureSettings = null,
CaptureNS = Windows.Media.Capture;
function cameraPreviewOrientation(arg) {
// Rotate the cam since WP8.1 MediaCapture outputs video stream rotated 90° CCW
if (screen.msOrientation === "portrait-primary" || screen.msOrientation === "portrait-secondary") {
capture.setPreviewRotation(Windows.Media.Capture.VideoRotation.clockwise90Degrees);
} else if (screen.msOrientation === "landscape-secondary") {
capture.setPreviewRotation(Windows.Media.Capture.VideoRotation.clockwise180Degrees);
} else {
capture.setPreviewRotation(Windows.Media.Capture.VideoRotation.none);
}
}
var createCameraUI = function () {
// Create fullscreen preview
capturePreview = document.createElement("video");
// z-order style element for capturePreview and captureCancelButton elts
// is necessary to avoid overriding by another page elements, -1 sometimes is not enough
capturePreview.style.cssText = "position: fixed; left: 0; top: 0; width: 100%; height: 100%; z-order: 999";
capturePreview.style.cssText = "position: fixed; left: 0; top: 0; width: 100%; height: 100%; z-index: 999";
// Create cancel button
captureCancelButton = document.createElement("button");
captureCancelButton.innerText = "Cancel";
captureCancelButton.style.cssText = "position: fixed; right: 0; bottom: 0; display: block; margin: 20px; z-order: 1000";
captureCancelButton.style.cssText = "position: fixed; right: 0; bottom: 0; display: block; margin: 20px; z-index: 1000";
capture = new CaptureNS.MediaCapture();
@@ -253,9 +338,6 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {
});
capture.initializeAsync(captureSettings).done(function () {
// This is necessary since WP8.1 MediaCapture outputs video stream rotated 90 degrees CCW
// TODO: This can be not consistent across devices, need additional testing on various devices
capture.setPreviewRotation(Windows.Media.Capture.VideoRotation.clockwise90Degrees);
// msdn.microsoft.com/en-us/library/windows/apps/hh452807.aspx
capturePreview.msZoom = true;
capturePreview.src = URL.createObjectURL(capture);
@@ -266,6 +348,7 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {
document.body.appendChild(captureCancelButton);
// Bind events to controls
window.addEventListener('deviceorientation', cameraPreviewOrientation, false);
capturePreview.addEventListener('click', captureAction);
captureCancelButton.addEventListener('click', function () {
destroyCameraPreview();
@@ -279,6 +362,7 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {
};
var destroyCameraPreview = function () {
window.removeEventListener('deviceorientation', cameraPreviewOrientation, false);
capturePreview.pause();
capturePreview.src = null;
[capturePreview, captureCancelButton].forEach(function(elem) {
@@ -297,8 +381,7 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {
var encodingProperties,
fileName,
generateUniqueCollisionOption = Windows.Storage.CreationCollisionOption.generateUniqueName,
tempFolder = Windows.Storage.ApplicationData.current.temporaryFolder,
capturedFile;
tempFolder = Windows.Storage.ApplicationData.current.temporaryFolder;
if (encodingType == Camera.EncodingType.PNG) {
fileName = 'photo.png';
@@ -310,29 +393,55 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {
tempFolder.createFileAsync(fileName, generateUniqueCollisionOption)
.then(function(tempCapturedFile) {
capturedFile = tempCapturedFile;
return capture.capturePhotoToStorageFileAsync(encodingProperties, capturedFile);
return new WinJS.Promise(function (complete) {
var imgStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
capture.capturePhotoToStreamAsync(encodingProperties, imgStream)
.then(function() {
return Windows.Graphics.Imaging.BitmapDecoder.createAsync(imgStream);
})
.then(function(dec) {
return Windows.Graphics.Imaging.BitmapEncoder.createForTranscodingAsync(imgStream, dec);
})
.then(function(enc) {
// We need to rotate the photo 90° CW because by default wp8.1 takes 90° CCW rotated photos.
enc.bitmapTransform.rotation = Windows.Graphics.Imaging.BitmapRotation.clockwise90Degrees;
return enc.flushAsync();
})
.then(function() {
return tempCapturedFile.openAsync(Windows.Storage.FileAccessMode.readWrite);
})
.then(function(fileStream) {
return Windows.Storage.Streams.RandomAccessStream.copyAsync(imgStream, fileStream);
})
.done(function() {
imgStream.close();
complete(tempCapturedFile);
}, function() {
imgStream.close();
throw new Error("An error has occured while capturing the photo.");
});
})
})
.done(function() {
.done(function(capturedFile) {
destroyCameraPreview();
// success callback for capture operation
var success = function(capturedfile) {
var success = function(capturedPhoto) {
if (destinationType == Camera.DestinationType.FILE_URI || destinationType == Camera.DestinationType.NATIVE_URI) {
if (targetHeight > 0 && targetWidth > 0) {
resizeImage(successCallback, errorCallback, capturedFile, targetWidth, targetHeight, encodingType);
resizeImage(successCallback, errorCallback, capturedPhoto, targetWidth, targetHeight, encodingType);
} else {
capturedfile.copyAsync(Windows.Storage.ApplicationData.current.localFolder, capturedfile.name, generateUniqueCollisionOption).done(function(copiedfile) {
capturedPhoto.copyAsync(Windows.Storage.ApplicationData.current.localFolder, capturedPhoto.name, generateUniqueCollisionOption).done(function(copiedfile) {
successCallback("ms-appdata:///local/" + copiedfile.name);
}, errorCallback);
}
} else {
if (targetHeight > 0 && targetWidth > 0) {
resizeImageBase64(successCallback, errorCallback, capturedfile, targetWidth, targetHeight);
resizeImageBase64(successCallback, errorCallback, capturedPhoto, targetWidth, targetHeight);
} else {
Windows.Storage.FileIO.readBufferAsync(capturedfile).done(function(buffer) {
Windows.Storage.FileIO.readBufferAsync(capturedPhoto).done(function(buffer) {
var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
capturedfile.deleteAsync().done(function() {
capturedPhoto.deleteAsync().done(function() {
successCallback(strBase64);
}, function(err) {
errorCallback(err);
@@ -379,7 +488,7 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {
};
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
if (encodingType === Camera.EncodingType.PNG) {
savePicker.fileTypeChoices.insert("PNG", [".png"]);
} else {
@@ -475,7 +584,7 @@ function takePictureFromCameraWindows(successCallback, errorCallback, args) {
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
if (encodingType === Camera.EncodingType.PNG) {
savePicker.fileTypeChoices.insert("PNG", [".png"]);
savePicker.suggestedFileName = "photo.png";

View File

@@ -22,7 +22,7 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:rim="http://www.blackberry.com/ns/widgets"
id="cordova-plugin-camera-tests"
version="1.0.0">
version="1.0.1-dev">
<name>Cordova Camera Plugin Tests</name>
<license>Apache 2.0</license>