Titanium Desktop save canvas to png file

227 views
Skip to first unread message

PM

unread,
Jun 12, 2012, 9:04:32 AM6/12/12
to titanium-desk...@googlegroups.com
Can some one tell me if it is at least possible to achieve this goal?
I've been searching at http://developer.appcelerator.com/question but really no luck : (

thanks
Peter

Paco Zarate

unread,
Jun 12, 2012, 12:56:01 PM6/12/12
to titanium-desk...@googlegroups.com

PM

unread,
Jun 12, 2012, 1:09:47 PM6/12/12
to titanium-desk...@googlegroups.com
Yes, but the way I used it just ends up with the image replacing the entire index.html.

Dan Tamas

unread,
Jun 12, 2012, 1:13:10 PM6/12/12
to titanium-desk...@googlegroups.com
Skip this step
document.write('<img src="'+img+'"/>');

PM

unread,
Jun 12, 2012, 1:57:01 PM6/12/12
to titanium-desk...@googlegroups.com
@dan tamas


Dan do you mean:


var img                = canvas.toDataURL ("image/png");
var mySampleFile = Titanium.Filesystem.getFile ( Titanium.Filesystem.getDesktopDirectory(), '');
var newFile           = Titanium.Filesystem.getFile ( mySampleFile,'sample.png' );
     newFile.write ( img );

Dan Tamas

unread,
Jun 12, 2012, 2:04:29 PM6/12/12
to titanium-desk...@googlegroups.com
Yap, something like this.
You might have to decode from base64 first.

PM

unread,
Jun 12, 2012, 2:08:30 PM6/12/12
to titanium-desk...@googlegroups.com
Gives me a windows warning for corrupted file:
I've tried many ways even with Ti.Utils.base64decode(img)

Paco Zarate

unread,
Jun 12, 2012, 5:03:12 PM6/12/12
to titanium-desk...@googlegroups.com
When you try to show it on screen, does it work?

db

unread,
Jun 12, 2012, 6:41:40 PM6/12/12
to Titanium Desktop Transition
Peter,

This is how I did it in the past:


function saveImage() {
var desktop = Ti.Filesystem.getDesktopDirectory();
var filename = (new Date()).getTime() + ".png";
var drawing = document.getElementById('drawing');
var baseImg = drawing.toDataURL("image/png").replace("data:image/
png;base64,", "");
writeBase64Asset(desktop + "/" + filename, baseImg);
return desktop + "/" + filename;
}

the function "writeBase64Asset" is a php command. Here is the code for
it:

function writeBase64Asset($path, $data) {
return file_put_contents($path, base64_decode($data));
}

Add it to a file (e.g. base64write.php) and make sure you include the
following code:
Titanium.include("php/base64write.php");


@davidbankier

PM

unread,
Jun 14, 2012, 12:01:13 PM6/14/12
to titanium-desk...@googlegroups.com
@ davidbankier

Can't seem to include the PHP file neither by the Ti.include nor the <script> tag :(

PM

unread,
Jun 14, 2012, 12:02:20 PM6/14/12
to titanium-desk...@googlegroups.com
@Paco Zarate

do you mean applying the data from canvas to a source of an image tag?

Paco Zarate

unread,
Jun 14, 2012, 1:26:20 PM6/14/12
to titanium-desk...@googlegroups.com
Yeap Peter, my suggestion was oriented to check the base64 handling. but the guys that have worked out the solution do process it as in the samples. so i think is better to try to make the php working.

Which version are you using?

PM

unread,
Jun 14, 2012, 1:40:27 PM6/14/12
to titanium-desk...@googlegroups.com
Titanium Studio, build: 2.0.1.201204132053
jenkins-titanium-rcp-master-185 (origin/master)

Titanium SDK: 1.2.0.RC4

PM

unread,
Jun 14, 2012, 3:12:36 PM6/14/12
to titanium-desk...@googlegroups.com
Would it be possible to get from the canvas.getImageData () the pixel data then pass it through an Uint8Array so that in the end we can pass it to a Titanium.Blob
Reply all
Reply to author
Forward
0 new messages