You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by sh...@apache.org on 2013/03/23 01:05:18 UTC

[08/13] ios commit: [CB-1285] added exif binary string splice operation to camera jpeg case, removed auto header

[CB-1285] added exif binary string splice operation to camera jpeg case, removed auto header


Project: http://git-wip-us.apache.org/repos/asf/cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-ios/commit/ac0cb7c6
Tree: http://git-wip-us.apache.org/repos/asf/cordova-ios/tree/ac0cb7c6
Diff: http://git-wip-us.apache.org/repos/asf/cordova-ios/diff/ac0cb7c6

Branch: refs/heads/master
Commit: ac0cb7c610098a7f56f6c33fc88ddf2eb65d8d1d
Parents: 9e64894
Author: lorinbeer <lo...@adobe.com>
Authored: Fri Mar 22 12:26:50 2013 -0700
Committer: lorinbeer <lo...@adobe.com>
Committed: Fri Mar 22 12:26:50 2013 -0700

----------------------------------------------------------------------
 CordovaLib/Classes/CDVCamera.m |   68 +++++++++++++++++++++++++++-------
 CordovaLib/Classes/CDVExif.h   |    9 +----
 2 files changed, 55 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/ac0cb7c6/CordovaLib/Classes/CDVCamera.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVCamera.m b/CordovaLib/Classes/CDVCamera.m
index 1f05333..bba339d 100644
--- a/CordovaLib/Classes/CDVCamera.m
+++ b/CordovaLib/Classes/CDVCamera.m
@@ -18,6 +18,7 @@
  */
 
 #import "CDVCamera.h"
+#import "CDVJpegHeaderWriter.h"
 #import "NSArray+Comparisons.h"
 #import "NSData+Base64.h"
 #import "NSDictionary+Extensions.h"
@@ -84,6 +85,12 @@ static NSSet* org_apache_cordova_validArrowDirections;
         return;
     }
 
+    NSNumber* cameraDirection = [arguments objectAtIndex:11];
+    UIImagePickerControllerCameraDevice cameraDevice = UIImagePickerControllerCameraDeviceRear; // default
+    if (cameraDirection != nil) {
+        cameraDevice = (UIImagePickerControllerSourceType)[cameraDirection intValue];
+    }
+
     bool allowEdit = [[arguments objectAtIndex:7] boolValue];
     NSNumber* targetWidth = [arguments objectAtIndex:3];
     NSNumber* targetHeight = [arguments objectAtIndex:4];
@@ -107,6 +114,7 @@ static NSSet* org_apache_cordova_validArrowDirections;
 
     cameraPicker.delegate = self;
     cameraPicker.sourceType = sourceType;
+    cameraPicker.cameraDevice = cameraDevice;
     cameraPicker.allowsEditing = allowEdit; // THIS IS ALL IT TAKES FOR CROPPING - jm
     cameraPicker.callbackId = callbackId;
     cameraPicker.targetSize = targetSize;
@@ -124,26 +132,18 @@ static NSSet* org_apache_cordova_validArrowDirections;
     cameraPicker.returnType = ([arguments objectAtIndex:1]) ? [[arguments objectAtIndex:1] intValue] : DestinationTypeFileUri;
 
     if (sourceType == UIImagePickerControllerSourceTypeCamera) {
-        // We only allow taking pictures (no video) in this API.
+        // we only allow taking pictures (no video) in this api
         cameraPicker.mediaTypes = [NSArray arrayWithObjects:(NSString*)kUTTypeImage, nil];
-
-        // We can only set the camera device if we're actually using the camera.
-        NSNumber* cameraDirection = [arguments objectAtIndex:11];
-        UIImagePickerControllerCameraDevice cameraDevice = UIImagePickerControllerCameraDeviceRear; // default
-        if (cameraDirection != nil) {
-            cameraDevice = (UIImagePickerControllerSourceType)[cameraDirection intValue];
-        }
-        cameraPicker.cameraDevice = cameraDevice;
     } else if (mediaType == MediaTypeAll) {
         cameraPicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
     } else {
-        NSArray* mediaArray = [NSArray arrayWithObjects:(NSString*)(mediaType == MediaTypeVideo ? kUTTypeMovie : kUTTypeImage), nil];
+        NSArray* mediaArray = [NSArray arrayWithObjects:(NSString*)(mediaType == MediaTypeVideo ? kUTTypeMovie:kUTTypeImage), nil];
         cameraPicker.mediaTypes = mediaArray;
     }
 
     if ([self popoverSupported] && (sourceType != UIImagePickerControllerSourceTypeCamera)) {
         if (cameraPicker.popoverController == nil) {
-            cameraPicker.popoverController = [[NSClassFromString(@"UIPopoverController")alloc] initWithContentViewController:cameraPicker];
+            cameraPicker.popoverController = [[NSClassFromString (@"UIPopoverController")alloc] initWithContentViewController:cameraPicker];
         }
         NSDictionary* options = [command.arguments objectAtIndex:10 withDefault:nil];
         [self displayPopover:options];
@@ -299,12 +299,52 @@ static NSSet* org_apache_cordova_validArrowDirections;
                 data = UIImagePNGRepresentation(scaledImage == nil ? image : scaledImage);
             } else {
                 data = UIImageJPEGRepresentation(scaledImage == nil ? image : scaledImage, cameraPicker.quality / 100.0f);
+                
+                CDVJpegHeaderWriter * exifWriter = [[CDVJpegHeaderWriter alloc] init];
+                
+                NSString * headerstring = [exifWriter createExifAPP1: [info objectForKey:@"UIImagePickerControllerMediaMetadata"]];
+                NSMutableData * exifdata = [NSMutableData dataWithCapacity: [headerstring length]/2];
+                int idx;
+                for (idx = 0; idx+1 < [headerstring length]; idx+=2) {
+                    NSRange range = NSMakeRange(idx, 2);
+                    NSString* hexStr = [headerstring substringWithRange:range];
+                    NSScanner* scanner = [NSScanner scannerWithString:hexStr];
+                    unsigned int intValue;
+                    [scanner scanHexInt:&intValue];
+                    [exifdata appendBytes:&intValue length:1];
+                }
+               
+                NSMutableData * buffer = [NSMutableData dataWithLength: 4];
+                NSMutableData * ddata = [NSMutableData dataWithCapacity: [data length]];
+                NSMakeRange(0,4);
+                int loc = 0;
+                //         CDVImageHeaderWriter * me = [[CDVImageHeaderWriter alloc] init];
+                
+                // read the jpeg data until we encounter the app1==0xFFE1 marker
+                while (loc+1 < [data length]) {
+                    NSData * blag = [data subdataWithRange: NSMakeRange(loc,2)];
+                    if( [[blag description] isEqualToString : @"<ffe1>"]) {
+                        // read the next
+                        NSString * the = [exifWriter hexStringFromData:[data subdataWithRange: NSMakeRange(loc+2,2)]];
+                        NSNumber * app1width = [exifWriter numericFromHexString:the];
+                        
+                        NSData * blag = [data subdataWithRange: NSMakeRange(loc,[app1width intValue])];
+                        [ddata appendData:exifdata];
+                        // advance our loc marker past app1
+                        loc += [app1width intValue] + 2;
+                        
+                    } else {
+                        [ddata appendData:blag];
+                        loc += 2;
+                    }
+                }
+                data = ddata;
             }
 
             if (cameraPicker.returnType == DestinationTypeFileUri) {
                 // write to temp directory and return URI
                 // get the temp directory path
-                NSString* docsPath = [NSTemporaryDirectory()stringByStandardizingPath];
+                NSString* docsPath = [NSTemporaryDirectory ()stringByStandardizingPath];
                 NSError* err = nil;
                 NSFileManager* fileMgr = [[NSFileManager alloc] init]; // recommended by apple (vs [NSFileManager defaultManager]) to be threadsafe
                 // generate unique file name
@@ -429,7 +469,7 @@ static NSSet* org_apache_cordova_validArrowDirections;
             rotation_radians = 0.0;
             break;
 
-        case UIImageOrientationDown:
+        case UIImageOrientationDown :
             rotation_radians = M_PI; // don't be scared of radians, if you're reading this, you're good at math
             break;
 
@@ -528,7 +568,7 @@ static NSSet* org_apache_cordova_validArrowDirections;
     // first parameter an image
     [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
     [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"upload\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
-    [postBody appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding : NSUTF8StringEncoding]];
+    [postBody appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
     [postBody appendData:imageData];
 
     //	// second parameter information

http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/ac0cb7c6/CordovaLib/Classes/CDVExif.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVExif.h b/CordovaLib/Classes/CDVExif.h
index 27240c8..3e8adbd 100644
--- a/CordovaLib/Classes/CDVExif.h
+++ b/CordovaLib/Classes/CDVExif.h
@@ -17,14 +17,6 @@
  under the License.
  */
 
-//
-//  ExifData.h
-//  Declares exif format codes and static data
-//
-//  Created by Lorin Beer on 2012-10-04.
-//
-//
-
 #ifndef CordovaLib_ExifData_h
 #define CordovaLib_ExifData_h
 
@@ -47,4 +39,5 @@ typedef enum exifDataTypes {
 // maps integer code for exif data types to width in bytes
 static const int DataTypeToWidth[] = {1,1,2,4,8,1,1,2,4,8,4,8};
 
+static const int RECURSE_HORIZON = 8;
 #endif