mrmBot-Matrix/natives/jpeg.cc

36 lines
889 B
C++
Raw Normal View History

#include <Magick++.h>
#include <napi.h>
#include <list>
using namespace std;
using namespace Magick;
Napi::Value Jpeg(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
try {
Napi::Object obj = info[0].As<Napi::Object>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
Blob blob;
Image image;
image.read(path);
image.quality(1);
image.magick("JPEG");
image.write(&blob);
Napi::Object result = Napi::Object::New(env);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
blob.length()));
result.Set("type", "jpg");
return result;
} catch (Napi::Error const &err) {
throw err;
} catch (std::exception const &err) {
throw Napi::Error::New(env, err.what());
} catch (...) {
throw Napi::Error::New(env, "Unknown error");
}
}