Skip to content

Is possible send a new argument for custom name of file canvas #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -28,14 +31,19 @@ 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);
},
function(err){
console.log(err);
},
document.getElementById('myCanvas')
canvasString,
canvasName
);
}
```
Expand Down
65 changes: 60 additions & 5 deletions src/android/Canvas2ImagePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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");

Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}
10 changes: 5 additions & 5 deletions www/Canvas2ImagePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@

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");
}
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]);
}
}
};
Expand Down