@@ -582,7 +582,7 @@ function JPEGEncoder(quality) {
582
582
}
583
583
}
584
584
585
- this . encode = function ( image , quality , toBase64 ) // image data object
585
+ this . encode = function ( image , quality , toRaw ) // image data object
586
586
{
587
587
var time_start = new Date ( ) . getTime ( ) ;
588
588
@@ -679,12 +679,21 @@ function JPEGEncoder(quality) {
679
679
680
680
writeWord ( 0xFFD9 ) ; //EOI
681
681
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
684
691
byteout = [ ] ;
692
+
685
693
// benchmarking
686
694
var duration = new Date ( ) . getTime ( ) - time_start ;
687
695
console . log ( 'Encoding time: ' + duration + 'ms' ) ;
696
+
688
697
return data ;
689
698
}
690
699
@@ -695,7 +704,6 @@ function JPEGEncoder(quality) {
695
704
// benchmarking
696
705
var duration = new Date ( ) . getTime ( ) - time_start ;
697
706
console . log ( 'Encoding time: ' + duration + 'ms' ) ;
698
- //
699
707
700
708
return jpegDataUri
701
709
}
@@ -751,10 +759,36 @@ function example(quality){
751
759
var ctx = cvs.getContext("2d");
752
760
ctx.drawImage(theImg,0,0);
753
761
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
755
763
var jpegURI = encoder.encode(theImgData, quality);
756
764
var img = document.createElement('img');
757
765
img.src = jpegURI;
758
766
document.body.appendChild(img);
759
767
}
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