Kaynağa Gözat

initial commit

master
munir ishak 5 yıl önce
işleme
5d3104634e

+ 2
- 0
.gitignore Dosyayı Görüntüle

@@ -0,0 +1,2 @@
1
+
2
+.DS_Store

+ 30
- 0
README.md Dosyayı Görüntüle

@@ -0,0 +1,30 @@
1
+Base64ImageSaverPlugin
2
+============
3
+
4
+This plugin(based on devgeeks/Canvas2ImagePlugin) allows you to save BASE64 data to the iOS Photo Library, Android Gallery or WindowsPhone 8 Photo Album from your app.
5
+
6
+This plugin fork from solderzzc/Base64ImageSaverPlugin
7
+
8
+Usage:
9
+------
10
+
11
+Call the `window.Base64ImageSaverPlugin.saveImageDataToLibrary()` method using success and error callbacks and the id attribute or the element object of the canvas to save:
12
+
13
+### Example
14
+
15
+```javascript
16
+function onDeviceReady()
17
+{
18
+	window.Base64ImageSaverPlugin.saveImageDataToLibrary(
19
+        function(msg){
20
+            console.log(msg);
21
+        },
22
+        function(err){
23
+            console.log(err);
24
+        },
25
+        BASE64DATA
26
+    );
27
+}
28
+```
29
+
30
+

+ 43
- 0
package.json Dosyayı Görüntüle

@@ -0,0 +1,43 @@
1
+{
2
+  "name": "cordova-mirtech-plugin-base64saveimage",
3
+  "version": "0.0.1",
4
+  "description": "This plugin allows you to save the contents of an HTML canvas tag to the iOS Photo Library, or Android Gallery from your app.",
5
+  "cordova": {
6
+    "id": "com.mirfalahtech.base64saveimage",
7
+    "platforms": [
8
+      "ios",
9
+      "android",
10
+      "wp8"
11
+    ]
12
+  },
13
+  "repository": {
14
+    "type": "git",
15
+    "url": ""
16
+  },
17
+  "keywords": [
18
+    "image",
19
+    "base64",
20
+    "save",
21
+    "photo library",
22
+    "ecosystem:cordova",
23
+    "cordova-ios",
24
+    "cordova-android",
25
+    "cordova-wp8"
26
+  ],
27
+  "engines": [
28
+    {
29
+      "name": "cordova",
30
+      "version": ">=3.0.0"
31
+    }
32
+  ],
33
+  "author": {
34
+    "name": "Muhammad Munir",
35
+    "email": "munirishak@mirfalah.my",
36
+    "url": ""
37
+  },
38
+  "license": "",
39
+  "bugs": {
40
+    "url": ""
41
+  },
42
+  "homepage": ""
43
+}

+ 70
- 0
plugin.xml Dosyayı Görüntüle

@@ -0,0 +1,70 @@
1
+<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
2
+    xmlns:android="http://schemas.android.com/apk/res/android"
3
+    id="com.mirfalahtech.base64saveimage"
4
+    version="0.0.1">
5
+
6
+    <name>base64saveimage</name>
7
+
8
+    <engines>
9
+        <engine name="cordova" version=">=3.0.0" />
10
+    </engines>
11
+
12
+    <description>This plugin allows you to save Base64 Data to the iOS Photo Library, or Android Gallery from your app.</description>
13
+    <author>Muhammad Munir - munirishak@mirfalah.my</author>
14
+    <keywords>base64,png,save,canvas,image,photo library</keywords>
15
+
16
+    <license></license>
17
+
18
+    <js-module src="www/Base64ImageSaverPlugin.js" name="Base64ImageSaverPlugin">
19
+        <clobbers target="window.Base64ImageSaverPlugin" />
20
+    </js-module>
21
+
22
+    <!-- ios -->
23
+    <platform name="ios">
24
+        <config-file target="config.xml" parent="/*">
25
+            <feature name="Base64ImageSaverPlugin">
26
+                <param name="ios-package" value="Base64ImageSaverPlugin"/>
27
+                <param name="onload" value="true" />
28
+            </feature>
29
+        </config-file>
30
+
31
+        <header-file src="src/ios/Base64ImageSaverPlugin.h" />
32
+
33
+        <source-file src="src/ios/Base64ImageSaverPlugin.m"
34
+            compiler-flags="-fno-objc-arc" />
35
+    </platform>
36
+
37
+    <!-- android -->
38
+    <platform name="android">
39
+
40
+        <config-file target="AndroidManifest.xml" parent="/*">
41
+            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
42
+        </config-file>
43
+
44
+        <config-file target="res/xml/config.xml" parent="/*">
45
+          <feature name="Base64ImageSaverPlugin" >
46
+            <param name="android-package" value="org.solderzzc.Base64ImageSaverPlugin.Base64ImageSaverPlugin"/>
47
+          </feature>
48
+        </config-file>
49
+
50
+        <source-file src="src/android/Base64ImageSaverPlugin.java" 
51
+            target-dir="src/org/solderzzc/Base64ImageSaverPlugin" />
52
+
53
+    </platform>
54
+
55
+    <!-- wp8 -->
56
+    <platform name="wp8">
57
+        <config-file target="config.xml" parent="/*">
58
+            <feature name="Base64ImageSaverPlugin">
59
+                <param name="wp-package" value="Base64ImageSaverPlugin"/>
60
+                <param name="onload" value="true" />
61
+            </feature>
62
+        </config-file>
63
+        
64
+        <config-file target="Properties/WMAppManifest.xml" parent="/Deployment/App/Capabilities">
65
+            <Capability Name="ID_CAP_MEDIALIB_PHOTO" />
66
+        </config-file>
67
+
68
+        <source-file src="src/wp8/Base64ImageSaverPlugin.cs" />
69
+    </platform>
70
+</plugin>

+ 126
- 0
src/android/Base64ImageSaverPlugin.java Dosyayı Görüntüle

@@ -0,0 +1,126 @@
1
+package org.solderzzc.Base64ImageSaverPlugin;
2
+
3
+import java.io.File;
4
+import java.io.FileOutputStream;
5
+import java.util.Calendar;
6
+
7
+import org.apache.cordova.CallbackContext;
8
+import org.apache.cordova.CordovaPlugin;
9
+
10
+import org.json.JSONArray;
11
+import org.json.JSONException;
12
+
13
+import android.content.Intent;
14
+import android.graphics.Bitmap;
15
+import android.graphics.BitmapFactory;
16
+import android.net.Uri;
17
+import android.os.Build;
18
+import android.os.Environment;
19
+import android.util.Base64;
20
+import android.util.Log;
21
+
22
+/**
23
+ * Base64ImageSaverPlugin.java
24
+ *
25
+ * Android implementation of the Base64ImageSaverPlugin for iOS.
26
+ * Inspirated by Joseph's "Save HTML5 Canvas Image to Gallery" plugin
27
+ * http://jbkflex.wordpress.com/2013/06/19/save-html5-canvas-image-to-gallery-phonegap-android-plugin/
28
+ *
29
+ * @author Vegard Løkken <vegard@headspin.no>
30
+ */
31
+public class Base64ImageSaverPlugin extends CordovaPlugin {
32
+	public static final String ACTION = "saveImageDataToLibrary";
33
+
34
+	@Override
35
+	public boolean execute(String action, JSONArray data,
36
+			CallbackContext callbackContext) throws JSONException {
37
+
38
+		if (action.equals(ACTION)) {
39
+
40
+			String base64 = data.optString(0);
41
+			if (base64.equals("")) // isEmpty() requires API level 9
42
+				callbackContext.error("Missing base64 string");
43
+			
44
+			// Create the bitmap from the base64 string
45
+			Log.d("Canvas2ImagePlugin", base64);
46
+			byte[] decodedString = Base64.decode(base64, Base64.DEFAULT);
47
+			Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
48
+			if (bmp == null) {
49
+				callbackContext.error("The image could not be decoded");
50
+			} else {
51
+				
52
+				// Save the image
53
+				File imageFile = savePhoto(bmp);
54
+				if (imageFile == null)
55
+					callbackContext.error("Error while saving image");
56
+				
57
+				// Update image gallery
58
+				scanPhoto(imageFile);
59
+				
60
+				callbackContext.success(imageFile.toString());
61
+			}
62
+			
63
+			return true;
64
+		} else {
65
+			return false;
66
+		}
67
+	}
68
+
69
+	private File savePhoto(Bitmap bmp) {
70
+		File retVal = null;
71
+		
72
+		try {
73
+			Calendar c = Calendar.getInstance();
74
+			String date = "" + c.get(Calendar.DAY_OF_MONTH)
75
+					+ c.get(Calendar.MONTH)
76
+					+ c.get(Calendar.YEAR)
77
+					+ c.get(Calendar.HOUR_OF_DAY)
78
+					+ c.get(Calendar.MINUTE)
79
+					+ c.get(Calendar.SECOND);
80
+
81
+			String deviceVersion = Build.VERSION.RELEASE;
82
+			Log.i("Base64ImageSaverPlugin", "Android version " + deviceVersion);
83
+			int check = deviceVersion.compareTo("2.3.3");
84
+
85
+			File folder;
86
+			/*
87
+			 * File path = Environment.getExternalStoragePublicDirectory(
88
+			 * Environment.DIRECTORY_PICTURES ); //this throws error in Android
89
+			 * 2.2
90
+			 */
91
+			if (check >= 1) {
92
+				folder = Environment
93
+					.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
94
+				
95
+				if(!folder.exists()) {
96
+					folder.mkdirs();
97
+				}
98
+			} else {
99
+				folder = Environment.getExternalStorageDirectory();
100
+			}
101
+			
102
+			File imageFile = new File(folder, "c2i_" + date.toString() + ".png");
103
+
104
+			FileOutputStream out = new FileOutputStream(imageFile);
105
+			bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
106
+			out.flush();
107
+			out.close();
108
+
109
+			retVal = imageFile;
110
+		} catch (Exception e) {
111
+			Log.e("Base64ImageSaverPlugin", "An exception occured while saving image: "
112
+					+ e.toString());
113
+		}
114
+		return retVal;
115
+	}
116
+	
117
+	/* Invoke the system's media scanner to add your photo to the Media Provider's database, 
118
+	 * making it available in the Android Gallery application and to other apps. */
119
+	private void scanPhoto(File imageFile)
120
+	{
121
+		Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
122
+	    Uri contentUri = Uri.fromFile(imageFile);
123
+	    mediaScanIntent.setData(contentUri);	      		  
124
+	    cordova.getActivity().sendBroadcast(mediaScanIntent);
125
+	} 
126
+}

+ 22
- 0
src/ios/Base64ImageSaverPlugin.h Dosyayı Görüntüle

@@ -0,0 +1,22 @@
1
+//
2
+//  Base64ImageSaverPlugin.h
3
+//  Base64ImageSaverPlugin PhoneGap/Cordova plugin
4
+//
5
+//  Created by Tommy-Carlos Williams on 29/03/12.
6
+//  Copyright (c) 2012 Tommy-Carlos Williams. All rights reserved.
7
+//	MIT Licensed
8
+//
9
+
10
+
11
+#import <Cordova/CDVPlugin.h>
12
+
13
+@interface Base64ImageSaverPlugin : CDVPlugin
14
+{
15
+	NSString* callbackId;
16
+}
17
+
18
+@property (nonatomic, copy) NSString* callbackId;
19
+
20
+- (void)saveImageDataToLibrary:(CDVInvokedUrlCommand*)command;
21
+
22
+@end

+ 60
- 0
src/ios/Base64ImageSaverPlugin.m Dosyayı Görüntüle

@@ -0,0 +1,60 @@
1
+//
2
+//  Base64ImageSaverPlugin.m
3
+//  Base64ImageSaverPlugin PhoneGap/Cordova plugin
4
+//
5
+//  Created by Tommy-Carlos Williams on 29/03/12.
6
+//  Copyright (c) 2012 Tommy-Carlos Williams. All rights reserved.
7
+//  MIT Licensed
8
+//
9
+
10
+#import "Base64ImageSaverPlugin.h"
11
+#import <Cordova/CDV.h>
12
+
13
+@implementation Base64ImageSaverPlugin
14
+@synthesize callbackId;
15
+
16
+//-(CDVPlugin*) initWithWebView:(UIWebView*)theWebView
17
+//{
18
+//    self = (Base64ImageSaverPlugin*)[super initWithWebView:theWebView];
19
+//    return self;
20
+//}
21
+
22
+- (void)saveImageDataToLibrary:(CDVInvokedUrlCommand*)command
23
+{
24
+    self.callbackId = command.callbackId;
25
+    NSData *imageData = [[NSData alloc] initWithBase64EncodedString:[command.arguments objectAtIndex:0] options:nil];
26
+    
27
+    UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease];    
28
+    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
29
+    
30
+}
31
+
32
+- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
33
+{
34
+    // Was there an error?
35
+    if (error != NULL)
36
+    {
37
+        // Show error message...
38
+        NSLog(@"ERROR: %@",error);
39
+        CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:error.description];
40
+//      [self.webView stringByEvaluatingJavaScriptFromString:[result toErrorCallbackString: self.callbackId]];
41
+        [self.commandDelegate sendPluginResult:result callbackId: self.callbackId];
42
+    }
43
+    else  // No errors
44
+    {
45
+        // Show message image successfully saved
46
+        NSLog(@"IMAGE SAVED!");
47
+        CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:@"gallery"];
48
+//      [self.webView stringByEvaluatingJavaScriptFromString:[result toSuccessCallbackString: self.callbackId]];
49
+        [self.commandDelegate sendPluginResult:result callbackId: self.callbackId];
50
+    }
51
+}
52
+
53
+- (void)dealloc
54
+{   
55
+    [callbackId release];
56
+    [super dealloc];
57
+}
58
+
59
+
60
+@end

+ 49
- 0
src/wp8/Base64ImageSaverPlugin.cs Dosyayı Görüntüle

@@ -0,0 +1,49 @@
1
+using Microsoft.Xna.Framework.Media;
2
+using System;
3
+using System.IO;
4
+using System.Text;
5
+using WPCordovaClassLib.Cordova;
6
+using WPCordovaClassLib.Cordova.Commands;
7
+using WPCordovaClassLib.Cordova.JSON;
8
+
9
+public class Base64ImageSaverPlugin : BaseCommand
10
+{
11
+    public Base64ImageSaverPlugin()
12
+	{
13
+	}
14
+
15
+    public void saveImageDataToLibrary(string jsonArgs)
16
+    {
17
+        try
18
+        {
19
+            var options = JsonHelper.Deserialize<string[]>(jsonArgs);
20
+
21
+            string imageData = options[0];
22
+            byte[] imageBytes = Convert.FromBase64String(imageData);
23
+
24
+            using (var imageStream = new MemoryStream(imageBytes))
25
+            {
26
+                imageStream.Seek(0, SeekOrigin.Begin);
27
+
28
+                string fileName = String.Format("c2i_{0:yyyyMMdd_HHmmss}", DateTime.Now);
29
+                var library = new MediaLibrary();
30
+                var picture = library.SavePicture(fileName, imageStream);
31
+
32
+                if (picture.Name.Contains(fileName))
33
+                {
34
+                    DispatchCommandResult(new PluginResult(PluginResult.Status.OK, 
35
+                        "Image saved: " + picture.Name));
36
+                }
37
+                else
38
+                {
39
+                    DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, 
40
+                        "Failed to save image: " + picture.Name));
41
+                }
42
+            }
43
+        }
44
+        catch (Exception ex)
45
+        {
46
+            DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, ex.Message));
47
+        }
48
+    }
49
+}

+ 30
- 0
www/Base64ImageSaverPlugin.js Dosyayı Görüntüle

@@ -0,0 +1,30 @@
1
+//
2
+//  Base64ImageSaverPlugin.js
3
+//  Base64ImageSaverPlugin PhoneGap/Cordova plugin
4
+//
5
+//  Created by Tommy-Carlos Williams on 29/03/12.
6
+//  Modified by Simba Zhang on 09/10/2015
7
+//  Copyright (c) 2012 Tommy-Carlos Williams. All rights reserved.
8
+//  MIT Licensed
9
+//
10
+
11
+  module.exports = {
12
+    
13
+    saveImageDataToLibrary:function(successCallback, failureCallback, base64Data) {
14
+        // successCallback required
15
+        if (typeof successCallback != "function") {
16
+            console.log("Base64ImageSaverPlugin Error: successCallback is not a function");
17
+        }
18
+        else if (typeof failureCallback != "function") {
19
+            console.log("Base64ImageSaverPlugin Error: failureCallback is not a function");
20
+        }
21
+        else if (typeof base64Data != "string") {
22
+            console.log("Base64ImageSaverPlugin Error: base64Data is not a string");
23
+        }
24
+        else {
25
+            var imageData = base64Data.replace(/data:image\/png;base64,/,'');
26
+            return cordova.exec(successCallback, failureCallback, "Base64ImageSaverPlugin","saveImageDataToLibrary",[imageData]);
27
+        }
28
+    }
29
+  };
30
+  

Loading…
İptal
Kaydet