使用 Node.JS 创建 JPG 文件



在我的节点.js服务器中,我正在从另一台服务器下载文件。下载的文件是用 Base64 编码两次的 JPG 图像数据,这意味着我必须对其进行 2 次解码。给定的是我的代码。

var base64DecodedFileData = new Buffer(file_data, 'base64').toString('binary');
var tmp = base64DecodedFileData.split("base64,");
var base64DecodedFileData = new Buffer(tmp[1], 'base64').toString('binary');                                                                                                           
var file = fs.createWriteStream(file_path, stream_options);
file.write(base64DecodedFileData);
file.end();

我知道我的图像数据在我第一次解码时是有效的(我已经通过第二次解码来验证在线 base64 解码器中的数据,并且我得到了正确的图像),但是当我第二次解码它并创建一个文件时这些数据。我没有获得有效的 JPG 文件。

已将其与实际图像进行了比较,两个文件的开头和结尾似乎都很好,但是我构建的文件中有些不对劲。构造文件的大小也比原始文件大。

PS:我在第二次解码前进行拆分,因为第一次解码后的数据以

data:; base64, DATASTART

任何想法。法鲁克·阿尔沙德。

我已经解决了我的问题。问题似乎出在节点的解码中.js所以我写了一个C++插件来完成这项工作。这是代码。我几乎可以肯定,如果我们只对图像文件进行一次编码,问题仍然存在。

.js文件

ModUtils.generateImageFromData(file_data, file_path);

c++ 插件:这使用 base64 C++编码器/解码器来自http://www.adp-gmbh.ch/cpp/common/base64.html

#define BUILDING_NODE_EXTENSION
#include <node.h>
#include <iostream>
#include <fstream>
#include "base64.h"
using namespace std;
using namespace v8;
static const std::string decoding_prefix = 
"data:;base64,";
// --------------------------------------------------------
//  Decode the image data and save it as image
// --------------------------------------------------------
Handle<Value> GenerateImageFromData(const Arguments& args) {
HandleScope scope;
// FIXME: Improve argument checking here.
// FIXME: Add error handling here.
if ( args.Length() < 2) return v8::Undefined();
Handle<Value> fileDataArg = args[0];
Handle<Value> filePathArg = args[1];
String::Utf8Value encodedData(fileDataArg);
String::Utf8Value filePath(filePathArg);
std::string std_FilePath = std::string(*filePath);
// We have received image data which is encoded with Base64 two times
// so we have to decode it twice.
std::string decoderParam = std::string(*encodedData);
std::string decodedString = base64_decode(decoderParam);
// After first decoding the data will also contains a encoding prefix like 
    // data:;base64,
// We have to remove this prefix to get actual encoded image data.
std::string second_pass = decodedString.substr(decoding_prefix.length(),     (decodedString.length() - decoding_prefix.length()));
std::string imageData = base64_decode(second_pass);
// Write image to file
ofstream image;
image.open(std_FilePath.c_str());
image << imageData;
image.close();
return scope.Close(String::New(" "));
//return scope.Close(decoded);
}
void Init(Handle<Object> target) {
// Register all functions here
target->Set(String::NewSymbol("generateImageFromData"),
    FunctionTemplate::New(GenerateImageFromData)->GetFunction());
}
NODE_MODULE(modutils, Init);

希望它能对其他人有所帮助。

最新更新