diff --git a/src/android/Canvas2ImagePlugin.java b/src/android/Canvas2ImagePlugin.java index ac3f1ba..f002e0d 100644 --- a/src/android/Canvas2ImagePlugin.java +++ b/src/android/Canvas2ImagePlugin.java @@ -19,6 +19,10 @@ import android.util.Base64; import android.util.Log; + +import java.io.StringBufferInputStream; +import android.util.Base64InputStream; + /** * Canvas2ImagePlugin.java * @@ -32,33 +36,49 @@ public class Canvas2ImagePlugin extends CordovaPlugin { public static final String ACTION = "saveImageDataToLibrary"; @Override - public boolean execute(String action, JSONArray data, - CallbackContext callbackContext) throws JSONException { + public boolean execute(final String action, final JSONArray data, + final CallbackContext callbackContext) throws JSONException { if (action.equals(ACTION)) { - String base64 = data.optString(0); - if (base64.equals("")) // isEmpty() requires API level 9 - callbackContext.error("Missing base64 string"); - - // Create the bitmap from the base64 string - Log.d("Canvas2ImagePlugin", base64); - byte[] decodedString = Base64.decode(base64, Base64.DEFAULT); - Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); - if (bmp == null) { - callbackContext.error("The image could not be decoded"); - } else { - - // Save the image - File imageFile = savePhoto(bmp); - if (imageFile == null) - callbackContext.error("Error while saving image"); - - // Update image gallery - scanPhoto(imageFile); - - callbackContext.success(imageFile.toString()); - } + cordova.getThreadPool().execute(new Runnable() { + public void run() { + + StringBufferInputStream inStream = null; + // Anonymous block to allow GC collection asap if necessary. + { + String base64 = data.optString(0); + if (base64.equals("")) { + // isEmpty() requires API level 9 + callbackContext.error("Missing base64 string"); + return; + } + inStream = new StringBufferInputStream(base64); + } + + Base64InputStream b64in = new Base64InputStream(inStream, 0); + + // Create the bitmap from the base64 string + //Log.d("Canvas2ImagePlugin ", Integer.toString(base64.length())); + //byte[] decodedString = Base64.decode(base64, Base64.DEFAULT); + //Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); + Bitmap bmp = BitmapFactory.decodeStream(b64in); + if (bmp == null) { + callbackContext.error("The image could not be decoded"); + } else { + + // Save the image + File imageFile = savePhoto(bmp); + if (imageFile == null) + callbackContext.error("Error while saving image"); + + // Update image gallery + scanPhoto(imageFile); + + callbackContext.success(imageFile.toString()); + } + } + }); return true; } else { diff --git a/src/ios/Canvas2ImagePlugin.h b/src/ios/Canvas2ImagePlugin.h index ef7bc56..4d74bfa 100644 --- a/src/ios/Canvas2ImagePlugin.h +++ b/src/ios/Canvas2ImagePlugin.h @@ -19,4 +19,6 @@ - (void)saveImageDataToLibrary:(CDVInvokedUrlCommand*)command; +- (void)saveImageBinaryDataToLibrary:(CDVInvokedUrlCommand*)command; + @end diff --git a/src/ios/Canvas2ImagePlugin.m b/src/ios/Canvas2ImagePlugin.m index 734ee00..f566618 100644 --- a/src/ios/Canvas2ImagePlugin.m +++ b/src/ios/Canvas2ImagePlugin.m @@ -13,22 +13,27 @@ @implementation Canvas2ImagePlugin @synthesize callbackId; -//-(CDVPlugin*) initWithWebView:(UIWebView*)theWebView -//{ -// self = (Canvas2ImagePlugin*)[super initWithWebView:theWebView]; -// return self; -//} - - (void)saveImageDataToLibrary:(CDVInvokedUrlCommand*)command { self.callbackId = command.callbackId; - NSData* imageData = [NSData dataFromBase64String:[command.arguments objectAtIndex:0]]; - - UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease]; - UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); + NSData* imageData = [[NSData alloc] initWithBase64EncodedString:[command.arguments objectAtIndex:0] options:0]; + + + UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease]; + UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); } +- (void)saveImageBinaryDataToLibrary:(CDVInvokedUrlCommand*)command +{ + self.callbackId = command.callbackId; + NSData* imageData = [[command.arguments objectAtIndex:0] dataUsingEncoding:NSUTF8StringEncoding]; + + UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease]; + UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); + +} + - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { // Was there an error? @@ -36,21 +41,21 @@ - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error context { // Show error message... NSLog(@"ERROR: %@",error); - CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:error.description]; - [self.webView stringByEvaluatingJavaScriptFromString:[result toErrorCallbackString: self.callbackId]]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId]; } else // No errors { // Show message image successfully saved NSLog(@"IMAGE SAVED!"); - CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:@"Image saved"]; - [self.webView stringByEvaluatingJavaScriptFromString:[result toSuccessCallbackString: self.callbackId]]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId]; } } - (void)dealloc { - [callbackId release]; + [callbackId release]; [super dealloc]; }