Skip to content

Commit 36a9e62

Browse files
Niklas IstenesNiklas Istenes
Niklas Istenes
authored and
Niklas Istenes
committed
added sample for using raw data, changes method signature and convention.
1 parent e7ec063 commit 36a9e62

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

jpeg_encoder_basic.js

+40-6
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ function JPEGEncoder(quality) {
582582
}
583583
}
584584

585-
this.encode = function(image,quality,toBase64) // image data object
585+
this.encode = function(image,quality,toRaw) // image data object
586586
{
587587
var time_start = new Date().getTime();
588588

@@ -679,12 +679,21 @@ function JPEGEncoder(quality) {
679679

680680
writeWord(0xFFD9); //EOI
681681

682-
if(!toBase64) {
683-
var data = byteout.slice(0);
682+
if(toRaw) {
683+
var len = byteout.length;
684+
var data = new Uint8Array(len);
685+
686+
for (var i=0; i<len; i++ ) {
687+
data[i] = byteout[i].charCodeAt();
688+
}
689+
690+
//cleanup
684691
byteout = [];
692+
685693
// benchmarking
686694
var duration = new Date().getTime() - time_start;
687695
console.log('Encoding time: '+ duration + 'ms');
696+
688697
return data;
689698
}
690699

@@ -695,7 +704,6 @@ function JPEGEncoder(quality) {
695704
// benchmarking
696705
var duration = new Date().getTime() - time_start;
697706
console.log('Encoding time: '+ duration + 'ms');
698-
//
699707

700708
return jpegDataUri
701709
}
@@ -751,10 +759,36 @@ function example(quality){
751759
var ctx = cvs.getContext("2d");
752760
ctx.drawImage(theImg,0,0);
753761
var theImgData = (ctx.getImageData(0, 0, cvs.width, cvs.height));
754-
// Encode the image and get a URI back
762+
// Encode the image and get a URI back, toRaw is false by default
755763
var jpegURI = encoder.encode(theImgData, quality);
756764
var img = document.createElement('img');
757765
img.src = jpegURI;
758766
document.body.appendChild(img);
759767
}
760-
*/
768+
769+
Example usage for getting back raw data and transforming it to a blob.
770+
Raw data is useful when trying to send an image over XHR or Websocket,
771+
it uses around 30% less bytes then a Base64 encoded string. It can
772+
also be useful if you want to save the image to disk using a FileWriter.
773+
774+
NOTE: The browser you are using must support Blobs
775+
function example(quality){
776+
// Pass in an existing image from the page
777+
var theImg = document.getElementById('testimage');
778+
// Use a canvas to extract the raw image data
779+
var cvs = document.createElement('canvas');
780+
cvs.width = theImg.width;
781+
cvs.height = theImg.height;
782+
var ctx = cvs.getContext("2d");
783+
ctx.drawImage(theImg,0,0);
784+
var theImgData = (ctx.getImageData(0, 0, cvs.width, cvs.height));
785+
// Encode the image and get a URI back, set toRaw to true
786+
var rawData = encoder.encode(theImgData, quality, true);
787+
788+
blob = new Blob([rawData.buffer], {type: 'image/jpeg'});
789+
var jpegURI = URL.createObjectURL(blob);
790+
791+
var img = document.createElement('img');
792+
img.src = jpegURI;
793+
document.body.appendChild(img);
794+
}*/

0 commit comments

Comments
 (0)