Add NativeImage.toDataUrl()

This commit is contained in:
Cheng Zhao 2015-02-12 14:32:51 +08:00
parent 8e94856cc8
commit 353b485202
2 changed files with 14 additions and 0 deletions

View file

@ -10,6 +10,7 @@
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/native_mate_converters/gfx_converter.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "base/base64.h"
#include "base/files/file_util.h"
#include "base/strings/string_util.h"
#include "native_mate/dictionary.h"
@ -127,6 +128,7 @@ mate::ObjectTemplateBuilder NativeImage::GetObjectTemplateBuilder(
template_.Reset(isolate, mate::ObjectTemplateBuilder(isolate)
.SetMethod("toPng", &NativeImage::ToPNG)
.SetMethod("toJpeg", &NativeImage::ToJPEG)
.SetMethod("toDataUrl", &NativeImage::ToDataURL)
.SetMethod("isEmpty", &NativeImage::IsEmpty)
.SetMethod("getSize", &NativeImage::GetSize)
.Build());
@ -150,6 +152,15 @@ v8::Handle<v8::Value> NativeImage::ToJPEG(v8::Isolate* isolate, int quality) {
output.size());
}
std::string NativeImage::ToDataURL() {
scoped_refptr<base::RefCountedMemory> png = image_.As1xPNGBytes();
std::string data_url;
data_url.insert(data_url.end(), png->front(), png->front() + png->size());
base::Base64Encode(data_url, &data_url);
data_url.insert(0, "data:image/png;base64,");
return data_url;
}
bool NativeImage::IsEmpty() {
return image_.IsEmpty();
}

View file

@ -5,6 +5,8 @@
#ifndef ATOM_COMMON_API_ATOM_API_NATIVE_IMAGE_H_
#define ATOM_COMMON_API_ATOM_API_NATIVE_IMAGE_H_
#include <string>
#include "native_mate/handle.h"
#include "native_mate/wrappable.h"
#include "ui/gfx/image/image.h"
@ -58,6 +60,7 @@ class NativeImage : public mate::Wrappable {
v8::Handle<v8::Value> ToPNG(v8::Isolate* isolate);
v8::Handle<v8::Value> ToJPEG(v8::Isolate* isolate, int quality);
std::string ToDataURL();
bool IsEmpty();
gfx::Size GetSize();