目前我正在使用Boost沙盒中的Boost.Process,并且在正确捕获标准输出方面遇到了问题;想知道是否有人可以帮我看看我可能做错了什么。
我正在尝试使用DCRAW(最新版本)从RAW相机图像中提取缩略图,并将它们捕获以转换为QT QImage。
进程启动函数:
namespace bf = ::boost::filesystem;
namespace bp = ::boost::process;
QImage DCRawInterface::convertRawImage(string path) {
// commandline: dcraw -e -c <srcfile> -> piped to stdout.
if ( bf::exists( path ) ) {
std::string exec = "bin\\dcraw.exe";
std::vector<std::string> args;
args.push_back("-v");
args.push_back("-c");
args.push_back("-e");
args.push_back(path);
bp::context ctx;
ctx.stdout_behavior = bp::capture_stream();
bp::child c = bp::launch(exec, args, ctx);
bp::pistream &is = c.get_stdout();
ofstream output("C:\\temp\\testcfk.jpg");
streamcopy(is, output);
}
return (NULL);
}
inline void streamcopy(std::istream& input, std::ostream& out) {
char buffer[4096];
int i = 0;
while (!input.eof() ) {
memset(buffer, 0, sizeof(buffer));
int bytes = input.readsome(buffer, sizeof buffer);
out.write(buffer, bytes);
i++;
}
}
调用转换器:
DCRawInterface DcRaw;
DcRaw.convertRawImage("test/CFK_2439.NEF");
目标是验证我能否将输入流复制到输出文件。当前,如果我注释掉以下行:
args.push_back("-c");
然后使用DCRAW将缩略图写入源目录,名称为CFK_2439.thumb.jpg,这证明进程是以正确的参数调用的。但问题在于没有正确连接输出管道。
顺便说一下:我正在Windows XP下使用Eclipse 3.5 / Latest MingW (GCC 4.4)进行测试。
[更新]
从调试中可以看出,通过流复制时,文件/管道已经关闭 - bytes = input.readsome(...)的值始终为0。
output
的ofstream
应该以二进制模式打开。另外:streamcopy
可以简化为out << input.rdbuf();
。 - Éric Malenfant