Permalink
Browse files

Merge pull request #1638 from FWeinb/capturePage-node-buffer

[Fix #480] capturePage using node buffer
  • Loading branch information...
2 parents fe02e16 + 85c4bf3 commit 7a9f25812a21a201c218d6f098f28e34a112da88 @rogerwang rogerwang committed Mar 3, 2014
Showing with 33 additions and 16 deletions.
  1. +30 −5 src/api/window_bindings.js
  2. +3 −11 src/browser/capture_page_helper.cc
View
@@ -431,18 +431,43 @@ Window.prototype.reloadDev = function() {
this.reload(3);
}
-Window.prototype.capturePage = function(callback, image_format) {
- if (image_format != 'jpeg' && image_format != 'png') {
- image_format = 'jpeg';
+var mime_types = {
+ 'jpeg' : 'image/jpeg',
+ 'png' : 'image/png'
+}
+
+Window.prototype.capturePage = function(callback, image_format_options) {
+ var options;
+
+ // Be compatible with the old api capturePage(callback, [format string])
+ if (typeof image_format_options == 'string' || image_format_options instanceof String) {
+ options = {
+ format : image_format_options
+ };
+ } else {
+ options = image_format_options || {};
+ }
+
+ if (options.format != 'jpeg' && options.format != 'png') {
+ options.format = 'jpeg';
}
if (typeof callback == 'function') {
this.once('__nw_capturepagedone', function(imgdata) {
- callback(imgdata);
+ switch(options.datatype){
+ case 'buffer' :
+ callback(new Buffer(imgdata, "base64"));
+ break;
+ case 'raw' :
+ callback(imgdata);
+ case 'datauri' :
+ default :
+ callback('data:' + mime_types[options.format] + ';base64,' + imgdata );
+ }
});
}
- CallObjectMethod(this, 'CapturePage', [image_format]);
+ CallObjectMethod(this, 'CapturePage', [options.format]);
};
Window.prototype.eval = function(frame, script) {
@@ -43,8 +43,6 @@ namespace capture_page_helper_constants {
const char kFormatValueJpeg[] = "jpeg";
const char kFormatValuePng[] = "png";
-const char kMimeTypeJpeg[] = "image/jpeg";
-const char kMimeTypePng[] = "image/png";
const int kDefaultQuality = 90;
@@ -110,7 +108,6 @@ void CapturePageHelper::SendResultFromBitmap(const SkBitmap& screen_capture) {
std::vector<unsigned char> data;
SkAutoLockPixels screen_capture_lock(screen_capture);
bool encoded = false;
- std::string mime_type;
switch (image_format_) {
case FORMAT_JPEG:
encoded = gfx::JPEGCodec::Encode(
@@ -120,15 +117,12 @@ void CapturePageHelper::SendResultFromBitmap(const SkBitmap& screen_capture) {
screen_capture.height(),
static_cast<int>(screen_capture.rowBytes()),
keys::kDefaultQuality,
- &data);
- mime_type = keys::kMimeTypeJpeg;
- break;
+ &data); break;
case FORMAT_PNG:
encoded = gfx::PNGCodec::EncodeBGRASkBitmap(
screen_capture,
true, // Discard transparency.
&data);
- mime_type = keys::kMimeTypePng;
break;
default:
NOTREACHED() << "Invalid image format.";
@@ -144,10 +138,8 @@ void CapturePageHelper::SendResultFromBitmap(const SkBitmap& screen_capture) {
reinterpret_cast<const char*>(vector_as_array(&data)), data.size());
base::Base64Encode(stream_as_string, &base64_result);
- base64_result.insert(0, base::StringPrintf("data:%s;base64,",
- mime_type.c_str()));
-
- shell_->SendEvent("__nw_capturepagedone", base64_result);
+
+ shell_->SendEvent("__nw_capturepagedone", base64_result );
}
void CapturePageHelper::OnSnapshot(const SkBitmap& bitmap) {

0 comments on commit 7a9f258

Please sign in to comment.