[C++] Creating and Saving New Photos Win32 Webcam Application
5 replies, posted
I am trying to make my webcam Win32 application save more than one photo at a time i.e. save one photo then another then another etc.
To do this, I am appending a string to an integer variable so that each new photo name can be unique and conform to the format of the second argument of CreateBMPFile. This would usually be a case of writing TEXT("string literal"), but I need to keep modifying the filename as each new photo is created.
[cpp]PBITMAPINFO pbi = CreateBitmapInfoStruct(hwnd, hbm);
int i = 1;
std::string image = "..\\..\\..\\..\\WebCam\\frame" + std::to_string(i) + ".bmp";
while (!exists(image)) {
image = "..\\..\\..\\..\\WebCam\\frame" + std::to_string(i) + ".bmp";
LPTSTR filename = (LPTSTR)image.c_str();
CreateBMPFile(hwnd, filename, pbi, hbm, hdcMem);
i++;
}[/cpp]
This compiles and executes, however, when I click the "Grab Frame" button and it attempts to save it, the application freezes. I've also tried using sprintf_s() with the same result of crashing the application.
I am using an exists() function to see whether the file exists in the system:
[cpp]inline bool exists(const std::string& name) {
struct stat buffer;
return (stat(name.c_str(), &buffer) == 0);
}[/cpp]
Please help!
#include <windows.h> and add a Sleep(10); and see if it freezes then
Ok, well this works, but it needs a good infinite loop statement:
[cpp]PBITMAPINFO pbi = CreateBitmapInfoStruct(hwnd, hbm);
int i = 1;
std::string image = "..\\..\\..\\..\\WebCam\\frame" + std::to_string(i) + ".bmp";
while (true) {
std::wstring temp = s2ws(image);
LPTSTR filename = (LPTSTR)temp.c_str();
CreateBMPFile(hwnd, filename, pbi, hbm, hdcMem);
image = "..\\..\\..\\..\\WebCam\\frame" + std::to_string(i+1) + ".bmp";
}[/cpp]
I realised the while (!exists()) would only check if the first assignment of image (i.e. one image file) existed and would just overwrite it.
How would I get it to save every photo as an incremental name? e.g. frame1, frame2, frame3 ...
Keep a counter variable, and increment every time a new image is saved.
[QUOTE=Adult;45307725]Keep a counter variable, and increment every time a new image is saved.[/QUOTE]
I realise this, but I do not know how to implement the counter. I've tried a for-loop but that causes it to freeze again. I've also tried just putting i++ during the while-loop, but that just causes the condition never to be true and thus, keep saving the same photo to incremental filenames.
change
[code]image = "..\\..\\..\\..\\WebCam\\frame" + std::to_string(i+1) + ".bmp";[/code]
to
[code]image = "..\\..\\..\\..\\WebCam\\frame" + std::to_string(++i) + ".bmp";[/code]
i++ returns i, then increments.
++i increments, then returns i.
Sorry, you need to Log In to post a reply to this thread.