您的位置:首页 > 其它

读取本地图片并编码为base64格式

2013-04-23 12:03 351 查看
读取本地图片并编码为base64格式

bool PluginImp::GetImageFromLocal( std::string image_path )
{
HANDLE pfile;
pfile = ::CreateFile(base::SysUTF8ToWide(image_path).c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_READONLY, NULL); // 用这个函数比OpenFile好

if ( pfile == INVALID_HANDLE_VALUE)
{
CloseHandle(pfile); // 一定注意在函数退出之前对句柄进行释放。
return false;
}

DWORD png_size = GetFileSize(pfile, NULL);
char* png_data = new char[png_size+1]; // 最后一位为 '/0',C-Style 字符串的结束符。
DWORD readsize;
ReadFile(pfile, png_data, png_size, &readsize, NULL);

std::string data_base64(reinterpret_cast<char*>(&png_data[0]), png_size);
file_util::WriteFile(FilePath(std::wstring(L"d://capture.png")), data_base64.data(), data_base64.size());
base::Base64Encode(data_base64, &data_base64);

std::string cmd_str;
scoped_ptr<DictionaryValue> protocol(new DictionaryValue);
protocol->SetString("imageData", data_base64);
base::JSONWriter::Write(protocol.get(), false, &cmd_str);

hbPluginAPI::instance()->fire_onImageData(cmd_str);
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: