Save HTML5 Canvas Image to Gallery – Phonegap Android plugin

Update

Readers of this post have raised issues with the plugin. I have to personally check for the issues but while using the plugin if you find any issues, you might want to debug it in ADT. I will come up with a working plugin soon. Thanks.

Sometime back I worked on a Phonegap Android plugin that helps to save an HTML5 Canvas Image to the device’s gallery. Well, the title of this post may be misleading but note that when you save an image, you actually have to save it on the SD Card or the device’s memory. The Gallery is just an app that shows the collection of images from various locations on the SD Card. So, there’s nothing like saving an image directly to the Gallery.
I had this working with Phonegap 2.2.0 (the version that I used). Newer versions of Phonegap/Cordova are available and things might have changed a bit, specially in the way custom plugins are written. Hence, if you are using newer versions of Phonegap you should have a look at the official documentation before proceeding.
Alright, here is what I did. I tried to save an image using the FileWriter – Phonegap API, but as it turned out, it can save only text data on the device’s memory. So, only way to do it was write a Phonegap plugin, pass the Canvas details from JavaScript interface to the Java side and let the Java class save the Canvas image on the SD Card.
Note that I am not a Java developer, and there may be better ways of writing this plugin.

How to install the plugin?
You need to get hold of two files :
1) The Java class – SavePhotoPlugin.java
2) The JavaScript interface – SavePhotoPlugin.js

Read More »

Advertisements

Getting CSS3 Transformations Matrix values for Android/Firefox

Quick snippet of code that will return the CSS3 Transformations Matrix values for Firefox browsers. We make use of the window.getComputedStyle() method for retrieving the style values for our element which has been moved using CSS3 Transformations.

function getMatrix() {
var style = window.getComputedStyle(your_element, null);
var moz_transform = style.getPropertyValue("-moz-transform") || style.getPropertyValue("transform");
if(moz_transform){
var values = moz_transform.split('(')[1];
values = values.split(')')[0];
values = values.split(',');
var a = values[4];
return a;
}

An example to follow…