當前我正在使用Boost沙盒中的Boost.Process,並且遇到問題才能正確捕獲我的標準輸出;想知道是否有人可以給我第二雙眼球成爲我可能做錯的事。無法使用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的名字,這證明,我認爲這個過程是用正確的參數進行調用。沒有發生的事情正確地連接到輸出管道。
FWIW:我在Eclipse 3.5/Latest MingW(GCC 4.4)下的Windows XP上執行此測試。
[UPDATE]
從調試,它會出現由代碼到達複製視頻流的時間,文件/管已經關閉 - 字節= input.readsome(...)是從未任何值而不是0.
可能不是主要問題,但'output''ofstream'應該以二進制模式打開。另外:'streamcopy'可以簡化爲'out << input.rdbuf();' –
input.rdbuf()的良好調用。在我寫流媒體之前,我實際上正在使用它來查看引擎蓋下發生了什麼。 –