2

I have my pixel array of html5 canvas pixel array object. I want to convert it into an integer array and pass it to a C or C++ method to convert this array to bits and save it as a jpeg image

var intPixArray = new Array(canavsPixelArray.length);
var k = 0;
for(var i = 0; i <img.height; i++){

    for(var j = 0; j <img.width; j++){

       intPixArray[k] = canvaspixel[k];
       intPixArray[k+1] = canvaspixel[k+1];
       intPixArray[k+2] = canvaspixel[k+2];
       intPixArray[k+3] = canvaspixel[k+3];
       k+=4;
    }
}

This int pixel array I want to convert into bits in javascript or at C/C++ side to write it to a file.

How is this possible?

I am working on a webOS hybrid app.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
madiha.athar
  • 1,103
  • 3
  • 12
  • 25
  • 1
    As far as I know, you can save to jpeg straight from canvas. (Unless I'm wrong;) Is there a reason you need to pass the image to C before saving to jpeg? – Adrian Schmidt May 17 '11 at 06:12
  • yes because canvas.toDataURL isn't supported in webOS so i cant get the base64 encoded string of the canvas ... – madiha.athar May 17 '11 at 07:38
  • 4
    Standard C++ doesn't have any functions to save jpegs, you are going to need to find a library (hint: libjpeg) or code one up yourself (hint: don't do that) – Arelius May 19 '11 at 22:49

1 Answers1

0

Adrian is right, you can just use javascript to change it into a jpeg:

Capture HTML Canvas as gif/jpg/png/pdf?

var canvas = document.getElementById("mycanvas");
var context = canvas.getContext("2d");
var img     = canvas.toDataURL("image/jpeg");

You can then write it to the DOM

document.write('<img src="'+img+'"/>');

Or base64 encode it and pass it to your C++ if you really want.

Community
  • 1
  • 1
Phil
  • 1,110
  • 1
  • 9
  • 25