Skip to content

Commit 7a9f258

Browse files
committed
Merge pull request nwjs#1638 from FWeinb/capturePage-node-buffer
[Fix nwjs#480] capturePage using node buffer
2 parents fe02e16 + 85c4bf3 commit 7a9f258

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

src/api/window_bindings.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,18 +431,43 @@ Window.prototype.reloadDev = function() {
431431
this.reload(3);
432432
}
433433

434-
Window.prototype.capturePage = function(callback, image_format) {
435-
if (image_format != 'jpeg' && image_format != 'png') {
436-
image_format = 'jpeg';
434+
var mime_types = {
435+
'jpeg' : 'image/jpeg',
436+
'png' : 'image/png'
437+
}
438+
439+
Window.prototype.capturePage = function(callback, image_format_options) {
440+
var options;
441+
442+
// Be compatible with the old api capturePage(callback, [format string])
443+
if (typeof image_format_options == 'string' || image_format_options instanceof String) {
444+
options = {
445+
format : image_format_options
446+
};
447+
} else {
448+
options = image_format_options || {};
449+
}
450+
451+
if (options.format != 'jpeg' && options.format != 'png') {
452+
options.format = 'jpeg';
437453
}
438454

439455
if (typeof callback == 'function') {
440456
this.once('__nw_capturepagedone', function(imgdata) {
441-
callback(imgdata);
457+
switch(options.datatype){
458+
case 'buffer' :
459+
callback(new Buffer(imgdata, "base64"));
460+
break;
461+
case 'raw' :
462+
callback(imgdata);
463+
case 'datauri' :
464+
default :
465+
callback('data:' + mime_types[options.format] + ';base64,' + imgdata );
466+
}
442467
});
443468
}
444469

445-
CallObjectMethod(this, 'CapturePage', [image_format]);
470+
CallObjectMethod(this, 'CapturePage', [options.format]);
446471
};
447472

448473
Window.prototype.eval = function(frame, script) {

src/browser/capture_page_helper.cc

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ namespace capture_page_helper_constants {
4343

4444
const char kFormatValueJpeg[] = "jpeg";
4545
const char kFormatValuePng[] = "png";
46-
const char kMimeTypeJpeg[] = "image/jpeg";
47-
const char kMimeTypePng[] = "image/png";
4846

4947
const int kDefaultQuality = 90;
5048

@@ -110,7 +108,6 @@ void CapturePageHelper::SendResultFromBitmap(const SkBitmap& screen_capture) {
110108
std::vector<unsigned char> data;
111109
SkAutoLockPixels screen_capture_lock(screen_capture);
112110
bool encoded = false;
113-
std::string mime_type;
114111
switch (image_format_) {
115112
case FORMAT_JPEG:
116113
encoded = gfx::JPEGCodec::Encode(
@@ -120,15 +117,12 @@ void CapturePageHelper::SendResultFromBitmap(const SkBitmap& screen_capture) {
120117
screen_capture.height(),
121118
static_cast<int>(screen_capture.rowBytes()),
122119
keys::kDefaultQuality,
123-
&data);
124-
mime_type = keys::kMimeTypeJpeg;
125-
break;
120+
&data); break;
126121
case FORMAT_PNG:
127122
encoded = gfx::PNGCodec::EncodeBGRASkBitmap(
128123
screen_capture,
129124
true, // Discard transparency.
130125
&data);
131-
mime_type = keys::kMimeTypePng;
132126
break;
133127
default:
134128
NOTREACHED() << "Invalid image format.";
@@ -144,10 +138,8 @@ void CapturePageHelper::SendResultFromBitmap(const SkBitmap& screen_capture) {
144138
reinterpret_cast<const char*>(vector_as_array(&data)), data.size());
145139

146140
base::Base64Encode(stream_as_string, &base64_result);
147-
base64_result.insert(0, base::StringPrintf("data:%s;base64,",
148-
mime_type.c_str()));
149-
150-
shell_->SendEvent("__nw_capturepagedone", base64_result);
141+
142+
shell_->SendEvent("__nw_capturepagedone", base64_result );
151143
}
152144

153145
void CapturePageHelper::OnSnapshot(const SkBitmap& bitmap) {

0 commit comments

Comments
 (0)