在OpenCV中使用Win32 OpenFile对话框出现问题 [英] Trouble using Win32 OpenFile dialog box in OpenCV
问题描述
亲爱的同事,
我试图使用对话框来选择要在OpenCV中使用的AVI文件的文件名. OpenCV函数称为:cvCaptureFromAVI
.要创建打开文件"对话框,我正在使用Windows函数:
GetOpenFileName
我怀疑我遇到的麻烦与整个项目都是用Unicode编写的事实有关,并且OpenCV函数期望使用const char *.如果我将AVI文件名硬编码为char *变量,而我没有打开对话框,则一切似乎都可以正常工作.但是,当我尝试使用该对话框时,cvCaptureFromAVI
函数将不起作用.换句话说,即使我不使用对话框中存储的文件名,也仍然无法使用AVI文件.仅仅打开对话框本身似乎就搞砸了.
这是有效的方法:
Dear Colleagues,
I''m attempting to use a dialog box to select the filename of an AVI file that I want to use in OpenCV. The OpenCV function is called: cvCaptureFromAVI
. To create the Open File dialog box, I am using the Windows function:
GetOpenFileName
I suspect that the trouble I am having has something to do with the fact that my whole project is written in Unicode, and the OpenCV function expects const char*. If I hard-code the AVI filename as a char* variable, and I don''t open the dialog box, everything seems to work. However, as soon as I try to use the dialog box, the cvCaptureFromAVI
function won''t work. In other words, even if I don''t use the filename stored from the dialog box, I still won''t be able to use my AVI file. Just opening the dialog box itself seems to screw it up.
Here is what works:
char filename[40] = "C:\\Users\\weylspinor\\Desktop\\test.avi";
CvCapture *G_movie_file = cvCaptureFromAVI(filename);
现在,如果我添加以下内容,它将不起作用. (请注意,我什至没有使用存储在OPENFILENAME结构中的文件名):
Now if I add the following, it doesnt work. (Note, I''m not even using the filename stored in the OPENFILENAME structure):
OPENFILENAME ofn; // common dialog box structure
char szFile[260] = ""; // buffer for file name
HANDLE hf; // file handle
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = h_main;
ofn.lpstrFile = LPWSTR(szFile);
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
ofn.lpstrFile = LPWSTR(szFile);
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = TEXT("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0");
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
// Display the Open dialog box.
if (GetOpenFileName(&ofn) == TRUE) {
hf = CreateFile(ofn.lpstrFile,
GENERIC_READ,
0,
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
}
char filename[40] = "C:\\Users\\weylspinor\\Desktop\\test.avi";
CvCapture *G_movie_file = cvCaptureFromAVI(filename);
我可以通过仅注释掉GetOpenFileName函数来使其重新工作.
I can get it to work again by just commenting out the GetOpenFileName function.
OPENFILENAME ofn; // common dialog box structure
char szFile[260] = ""; // buffer for file name
HANDLE hf; // file handle
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = h_main;
ofn.lpstrFile = LPWSTR(szFile);
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
ofn.lpstrFile = LPWSTR(szFile);
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = TEXT("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0");
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
// Display the Open dialog box.
/*if (GetOpenFileName(&ofn) == TRUE) {
hf = CreateFile(ofn.lpstrFile,
GENERIC_READ,
0,
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
}*/
char filename[40] = "C:\\Users\\weylspinor\\Desktop\\test.avi";
CvCapture *G_movie_file = cvCaptureFromAVI(filename);
如果有人可以指出我的错误,我将不胜感激.
干杯,
-J
I''d appreciate it if anyone can point me to my error.
Cheers,
-J
推荐答案
您需要通过 ^ ]转换器.
You need to convert the returned filename from Unicode to Ansi via one of the wcstomb()[^] converters.
这篇关于在OpenCV中使用Win32 OpenFile对话框出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!