Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
niou-ns committed Jul 26, 2015
1 parent bc85ec3 commit dc81afb
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 107 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

# cordova-plugin-camera

[![Build Status](https://travis-ci.org/apache/cordova-plugin-camera.svg)](https://travis-ci.org/apache/cordova-plugin-camera)

This plugin defines a global `navigator.camera` object, which provides an API for taking pictures and for choosing images from
the system's image library.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-camera",
"version": "1.2.1-dev",
"version": "1.2.0",
"description": "Cordova Camera Plugin",
"cordova": {
"id": "cordova-plugin-camera",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.2.1-dev">
version="1.2.0">
<name>Camera</name>
<description>Cordova Camera Plugin</description>
<license>Apache 2.0</license>
Expand Down
10 changes: 2 additions & 8 deletions src/android/CameraLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ private void processResultFromCamera(int destType, Intent intent) throws IOExcep
}

//We don't support PNG, so let's not pretend we do
exif.createInFile(sourcePath);
exif.createInFile(getTempDirectoryPath() + "/.Pic.jpg");
exif.readExifData();
rotate = exif.getOrientation();

Expand Down Expand Up @@ -442,13 +442,7 @@ else if (destType == FILE_URI || destType == NATIVE_URI) {

this.callbackContext.success(uri.toString());
} else {
if(croppedUri != null) {
bitmap = getScaledBitmap(FileHelper.stripFileProtocol(croppedUri.toString()));
}
else
{
bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));
}
bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));

if (rotate != 0 && this.correctOrientation) {
bitmap = getRotatedBitmap(rotate, bitmap, exif);
Expand Down
29 changes: 7 additions & 22 deletions src/ios/CDVCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Licensed to the Apache Software Foundation (ASF) under one
#import "CDVCamera.h"
#import "CDVJpegHeaderWriter.h"
#import "UIImage+CropScaleOrientation.h"
#import <Cordova/NSData+Base64.h>
#import <ImageIO/CGImageProperties.h>
#import <AssetsLibrary/ALAssetRepresentation.h>
#import <AssetsLibrary/AssetsLibrary.h>
Expand All @@ -30,31 +31,16 @@ Licensed to the Apache Software Foundation (ASF) under one
#import <MobileCoreServices/UTCoreTypes.h>
#import <objc/message.h>

#ifndef __CORDOVA_4_0_0
#import <Cordova/NSData+Base64.h>
#endif

#define CDV_PHOTO_PREFIX @"cdv_photo_"

static NSSet* org_apache_cordova_validArrowDirections;

static NSString* toBase64(NSData* data) {
SEL s1 = NSSelectorFromString(@"cdv_base64EncodedString");
SEL s2 = NSSelectorFromString(@"base64EncodedString");
SEL s3 = NSSelectorFromString(@"base64EncodedStringWithOptions:");

if ([data respondsToSelector:s1]) {
NSString* (*func)(id, SEL) = (void *)[data methodForSelector:s1];
return func(data, s1);
} else if ([data respondsToSelector:s2]) {
NSString* (*func)(id, SEL) = (void *)[data methodForSelector:s2];
return func(data, s2);
} else if ([data respondsToSelector:s3]) {
NSString* (*func)(id, SEL, NSUInteger) = (void *)[data methodForSelector:s3];
return func(data, s3, 0);
} else {
return nil;
}
SEL realSel = [data respondsToSelector:s1] ? s1 : s2;
NSString* (*func)(id, SEL) = (void *)[data methodForSelector:realSel];
return func(data, realSel);
}

@implementation CDVPictureOptions
Expand Down Expand Up @@ -417,10 +403,9 @@ - (UIImage*)retrieveImage:(NSDictionary*)info options:(CDVPictureOptions*)option
} else {
scaledImage = [image imageByScalingNotCroppingForSize:options.targetSize];
}
} else if (options.allowsEditing && [info objectForKey:UIImagePickerControllerEditedImage]) {
scaledImage = [image imageCropWithoutScale:info];
}

} else if (options.allowsEditing && [info objectForKey:UIImagePickerControllerEditedImage]) {
scaledImage = [image imageCropWithoutScale:info];
}

return (scaledImage == nil ? image : scaledImage);
}
Expand Down
109 changes: 38 additions & 71 deletions src/windows/CameraProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,7 @@ function takePictureFromFileWindows(successCallback, errorCallback, args) {
else {
var storageFolder = getAppData().localFolder;
file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).done(function (storageFile) {
if(destinationType == Camera.DestinationType.NATIVE_URI) {
successCallback("ms-appdata:///local/" + storageFile.name);
}
else {
successCallback(URL.createObjectURL(storageFile));
}
successCallback(URL.createObjectURL(storageFile));
}, function () {
errorCallback("Can't access localStorage folder.");
});
Expand Down Expand Up @@ -312,32 +307,24 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {
saveToPhotoAlbum = args[9],
cameraDirection = args[11],
capturePreview = null,
captureTakePhotoButton = null,
captureCancelButton = null,
capture = null,
captureSettings = null,
CaptureNS = Windows.Media.Capture,
sensor = null;

var createCameraUI = function () {
// create style for take and cancel buttons
var buttonStyle = "width:45%;padding: 10px 16px;font-size: 18px;line-height: 1.3333333;color: #333;background-color: #fff;border-color: #ccc; border: 1px solid transparent;border-radius: 6px; display: block; margin: 20px; z-index: 1000;border-color: #adadad;";

// Create fullscreen preview
// z-order style element for capturePreview and captureCancelButton elts
// is necessary to avoid overriding by another page elements, -1 sometimes is not enough
capturePreview = document.createElement("video");
capturePreview.style.cssText = "position: fixed; left: 0; top: 0; width: 100%; height: 100%; z-index: 999;";

// Create capture button
captureTakePhotoButton = document.createElement("button");
captureTakePhotoButton.innerText = "Take";
captureTakePhotoButton.style.cssText = buttonStyle + "position: fixed; left: 0; bottom: 0; margin: 20px; z-index: 1000";
// 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-index: 999";

// Create cancel button
captureCancelButton = document.createElement("button");
captureCancelButton.innerText = "Cancel";
captureCancelButton.style.cssText = buttonStyle + "position: fixed; right: 0; bottom: 0; margin: 20px; z-index: 1000";
captureCancelButton.style.cssText = "position: fixed; right: 0; bottom: 0; display: block; margin: 20px; z-index: 1000";

capture = new CaptureNS.MediaCapture();

Expand Down Expand Up @@ -368,22 +355,6 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {

return capture.initializeAsync(captureSettings);
}).then(function () {

// create focus control if available
var VideoDeviceController = capture.videoDeviceController;
var FocusControl = VideoDeviceController.focusControl;

if (FocusControl.supported == true) {
capturePreview.addEventListener('click', function () {

var preset = Windows.Media.Devices.FocusPreset.autoNormal;

FocusControl.setPresetAsync(preset).done(function () {

});
});
}

// msdn.microsoft.com/en-us/library/windows/apps/hh452807.aspx
capturePreview.msZoom = true;
capturePreview.src = URL.createObjectURL(capture);
Expand All @@ -394,12 +365,10 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {
if (sensor !== null) {
sensor.addEventListener("orientationchanged", onOrientationChange);
}

// add click events to take and cancel buttons
captureTakePhotoButton.addEventListener('click', captureAction);
capturePreview.addEventListener('click', captureAction);
captureCancelButton.addEventListener('click', function () {
destroyCameraPreview();
errorCallback('no image selected');
errorCallback('Cancelled');
}, false);

// Change default orientation
Expand All @@ -419,9 +388,8 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {
return;
}

// add elements to body
// Insert preview frame and controls into page
document.body.appendChild(capturePreview);
document.body.appendChild(captureTakePhotoButton);
document.body.appendChild(captureCancelButton);

if (aspectRatios.indexOf(DEFAULT_ASPECT_RATIO) > -1) {
Expand All @@ -442,8 +410,7 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {
}
capturePreview.pause();
capturePreview.src = null;
// remove elements from wrapper
[capturePreview, captureTakePhotoButton, captureCancelButton].forEach(function (elem) {
[capturePreview, captureCancelButton].forEach(function(elem) {
if (elem /* && elem in document.body.childNodes */) {
document.body.removeChild(elem);
}
Expand Down Expand Up @@ -474,33 +441,33 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {
var photoStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
var finalStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
capture.capturePhotoToStreamAsync(encodingProperties, photoStream)
.then(function() {
return Windows.Graphics.Imaging.BitmapDecoder.createAsync(photoStream);
})
.then(function(dec) {
finalStream.size = 0; // BitmapEncoder requires the output stream to be empty
return Windows.Graphics.Imaging.BitmapEncoder.createForTranscodingAsync(finalStream, dec);
})
.then(function(enc) {
// We need to rotate the photo wrt sensor orientation
enc.bitmapTransform.rotation = orientationToRotation(sensor.getCurrentOrientation());
return enc.flushAsync();
})
.then(function() {
return tempCapturedFile.openAsync(Windows.Storage.FileAccessMode.readWrite);
})
.then(function(fileStream) {
return Windows.Storage.Streams.RandomAccessStream.copyAsync(finalStream, fileStream);
})
.done(function() {
photoStream.close();
finalStream.close();
complete(tempCapturedFile);
}, function() {
photoStream.close();
finalStream.close();
throw new Error("An error has occured while capturing the photo.");
});
.then(function() {
return Windows.Graphics.Imaging.BitmapDecoder.createAsync(photoStream);
})
.then(function(dec) {
finalStream.size = 0; // BitmapEncoder requires the output stream to be empty
return Windows.Graphics.Imaging.BitmapEncoder.createForTranscodingAsync(finalStream, dec);
})
.then(function(enc) {
// We need to rotate the photo wrt sensor orientation
enc.bitmapTransform.rotation = orientationToRotation(sensor.getCurrentOrientation());
return enc.flushAsync();
})
.then(function() {
return tempCapturedFile.openAsync(Windows.Storage.FileAccessMode.readWrite);
})
.then(function(fileStream) {
return Windows.Storage.Streams.RandomAccessStream.copyAsync(finalStream, fileStream);
})
.done(function() {
photoStream.close();
finalStream.close();
complete(tempCapturedFile);
}, function() {
photoStream.close();
finalStream.close();
throw new Error("An error has occured while capturing the photo.");
});
});
})
.done(function(capturedFile) {
Expand All @@ -513,8 +480,8 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {
saveToPhotoAlbum: saveToPhotoAlbum
}, successCallback, errorCallback);
}, function(err) {
destroyCameraPreview();
errorCallback(err);
destroyCameraPreview();
errorCallback(err);
});
};

Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions tests/ios/CDVCameraTest/CDVCameraLibTests/CameraTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ - (void) testProcessImage
pictureOptions.encodingType = EncodingTypePNG;

resultData = [self.plugin processImage:originalImage info:@{} options:pictureOptions];
XCTAssertEqualObjects([resultData base64EncodedStringWithOptions:0], [originalImageDataPNG base64EncodedStringWithOptions:0]);
XCTAssertEqualObjects([resultData base64EncodedString], [originalImageDataPNG base64EncodedString]);

// Original, JPEG, full quality

Expand All @@ -487,7 +487,7 @@ - (void) testProcessImage
pictureOptions.encodingType = EncodingTypeJPEG;

resultData = [self.plugin processImage:originalImage info:@{} options:pictureOptions];
XCTAssertEqualObjects([resultData base64EncodedStringWithOptions:0], [originalImageDataJPEG base64EncodedStringWithOptions:0]);
XCTAssertEqualObjects([resultData base64EncodedString], [originalImageDataJPEG base64EncodedString]);

// Original, JPEG, with quality value

Expand All @@ -500,7 +500,7 @@ - (void) testProcessImage

NSData* originalImageDataJPEGWithQuality = UIImageJPEGRepresentation(originalImage, [pictureOptions.quality floatValue]/ 100.f);
resultData = [self.plugin processImage:originalImage info:@{} options:pictureOptions];
XCTAssertEqualObjects([resultData base64EncodedStringWithOptions:0], [originalImageDataJPEGWithQuality base64EncodedStringWithOptions:0]);
XCTAssertEqualObjects([resultData base64EncodedString], [originalImageDataJPEGWithQuality base64EncodedString]);

// TODO: usesGeolocation is not tested
}
Expand Down
2 changes: 1 addition & 1 deletion tests/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.2.1-dev">
version="1.2.0">
<name>Cordova Camera Plugin Tests</name>
<license>Apache 2.0</license>

Expand Down

0 comments on commit dc81afb

Please sign in to comment.