diff --git a/README.md b/README.md index 022f8ab..385e79b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ Canvas2ImagePlugin ============ +Added "canvasName" parameter to custom name of image (only for android) * +Modified for to send string base64 canvas in place canvas element * + This plugin allows you to save the contents of an HTML canvas tag to the iOS Photo Library, Android Gallery or WindowsPhone 8 Photo Album from your app. See an example project using it here: [https://github.com/devgeeks/Canvas2ImageDemo](https://github.com/devgeeks/Canvas2ImageDemo) - note: this app does not work in wp8. @@ -28,6 +31,10 @@ Call the `window.canvas2ImagePlugin.saveImageDataToLibrary()` method using succe ```javascript function onDeviceReady() { + var myCanvas = document.getElementById('myCanvas'); + var canvasString = myCanvas.toDataURL(); + var canvasName = "nameOfFile"; + window.canvas2ImagePlugin.saveImageDataToLibrary( function(msg){ console.log(msg); @@ -35,7 +42,8 @@ function onDeviceReady() function(err){ console.log(err); }, - document.getElementById('myCanvas') + canvasString, + canvasName ); } ``` diff --git a/src/android/Canvas2ImagePlugin.java b/src/android/Canvas2ImagePlugin.java index ac3f1ba..dca3898 100644 --- a/src/android/Canvas2ImagePlugin.java +++ b/src/android/Canvas2ImagePlugin.java @@ -36,8 +36,10 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException { if (action.equals(ACTION)) { - + String base64 = data.optString(0); + String customName = normalizeString(data.optString(1)); + if (base64.equals("")) // isEmpty() requires API level 9 callbackContext.error("Missing base64 string"); @@ -50,7 +52,7 @@ public boolean execute(String action, JSONArray data, } else { // Save the image - File imageFile = savePhoto(bmp); + File imageFile = savePhoto(bmp, customName); if (imageFile == null) callbackContext.error("Error while saving image"); @@ -66,7 +68,7 @@ public boolean execute(String action, JSONArray data, } } - private File savePhoto(Bitmap bmp) { + private File savePhoto(Bitmap bmp, String customName) { File retVal = null; try { @@ -99,7 +101,14 @@ private File savePhoto(Bitmap bmp) { folder = Environment.getExternalStorageDirectory(); } - File imageFile = new File(folder, "c2i_" + date.toString() + ".png"); + if(customName.equals("")){ + customName = "c2i_" + date.toString() + ".png"; + }else{ + customName = customName + ".png"; + } + + + File imageFile = new File(folder, customName); FileOutputStream out = new FileOutputStream(imageFile); bmp.compress(Bitmap.CompressFormat.PNG, 100, out); @@ -123,4 +132,50 @@ private void scanPhoto(File imageFile) mediaScanIntent.setData(contentUri); cordova.getActivity().sendBroadcast(mediaScanIntent); } -} + private String normalizeString(String str) { + + String[][] caracteresAcento = { + {"Á", "A"}, {"á", "a"}, + {"É", "E"}, {"é", "e"}, + {"Í", "I"}, {"í", "i"}, + {"Ó", "O"}, {"ó", "o"}, + {"Ú", "U"}, {"ú", "u"}, + {"À", "A"}, {"à", "a"}, + {"È", "E"}, {"è", "e"}, + {"Ì", "I"}, {"ì", "i"}, + {"Ò", "O"}, {"ò", "o"}, + {"Ù", "U"}, {"ù", "u"}, + {"Â", "A"}, {"â", "a"}, + {"Ê", "E"}, {"ê", "e"}, + {"Î", "I"}, {"î", "i"}, + {"Ô", "O"}, {"ô", "o"}, + {"Û", "U"}, {"û", "u"}, + {"Ä", "A"}, {"ä", "a"}, + {"Ë", "E"}, {"ë", "e"}, + {"Ï", "I"}, {"ï", "i"}, + {"Ö", "O"}, {"ö", "o"}, + {"Ü", "U"}, {"ü", "u"}, + {"Ã", "A"}, {"ã", "a"}, + {"Õ", "O"}, {"õ", "o"}, + {"Ç", "C"}, {"ç", "c"}, + }; + + for (int i = 0; i < caracteresAcento.length; i++) { + str = str.replaceAll(caracteresAcento[i][0], caracteresAcento[i][1]); + } + + /** Remove special chars "" **/ + String[] caracteresEspeciais = {"\\.", ",", "-", ":", "\\(", "\\)", "ª", "\\|", "\\\\", "°"}; + + for (int i = 0; i < caracteresEspeciais.length; i++) { + str = str.replaceAll(caracteresEspeciais[i], ""); + } + + /** Remove white spaces **/ + str = str.replaceAll("^\\s+",""); + str = str.replaceAll("\\s+$",""); + str = str.replaceAll("\\s+",""); + + return str; + } +} \ No newline at end of file diff --git a/www/Canvas2ImagePlugin.js b/www/Canvas2ImagePlugin.js index e46b6db..a5ab52e 100644 --- a/www/Canvas2ImagePlugin.js +++ b/www/Canvas2ImagePlugin.js @@ -9,7 +9,7 @@ module.exports = { - saveImageDataToLibrary:function(successCallback, failureCallback, canvasId) { + saveImageDataToLibrary:function(successCallback, failureCallback, canvasString, customName) { // successCallback required if (typeof successCallback != "function") { console.log("Canvas2ImagePlugin Error: successCallback is not a function"); @@ -17,10 +17,10 @@ else if (typeof failureCallback != "function") { console.log("Canvas2ImagePlugin Error: failureCallback is not a function"); } - else { - var canvas = (typeof canvasId === "string") ? document.getElementById(canvasId) : canvasId; - var imageData = canvas.toDataURL().replace(/data:image\/png;base64,/,''); - return cordova.exec(successCallback, failureCallback, "Canvas2ImagePlugin","saveImageDataToLibrary",[imageData]); + else { + var imageData = canvasString.replace(/data:image\/png;base64,/,''); + var imageName = String(customName); + return cordova.exec(successCallback, failureCallback, "Canvas2ImagePlugin","saveImageDataToLibrary",[imageData, imageName]); } } };