OpenCV读取照片保存为MP4视频
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
#define VIDEONUM 10
int main(int argc, char **argv)
{
VideoWriter writer[10];
char video_path[256];
char img_path[256];
for (size_t i = 0; i < VIDEONUM; i++)
{
sprintf_s(video_path, "F:/video/%d.mp4", i);
sprintf_s(img_path, "F:/front_rgb_%d.png", i);
writer[i].open(string(video_path), CV_FOURCC('m', 'p', '4', 'v'), 15, Size(1920, 1080));
for (int j = 0; j < 1000; j++)
{
Mat src_img = imread(string(img_path));
putText(src_img, to_string(j), Point(960, 540), FONT_HERSHEY_COMPLEX, 2, Scalar(0, 0, 255), 2, 8, false);
writer[i] << src_img;
cout << i << " video, frame num = " << j << endl;
}
writer[i].release();
}
}
向指定进程窗口发送模拟按键指令
参考链接
sendCmd.h
#pragma once
// cnc_resetconnect.cc
#include <afxwin.h>
#include <iostream>
#include <atlstr.h>
#include <string>
#include <codecvt>
#include <TlHelp32.h>
#include <vector>
namespace sendkey {
using namespace std;
vector<HWND> vcHWnd;
bool g_bOnlyShowWnd;
CString g_sProcessTitle;
CString g_sPressKey;
std::vector<HWND> g_vcHandle;
bool g_bSend;
bool SetDataToBoard( const char* pDta, int iCount)
{
if (!pDta || iCount < 1)
{
return false;
}
bool bReturn = false;
if (OpenClipboard(NULL))
{
HGLOBAL lclipBuffer;
char * lpBuffer;
EmptyClipboard();
lclipBuffer = GlobalAlloc(GMEM_DDESHARE, iCount + 1);
lpBuffer = (char*)GlobalLock(lclipBuffer);
memcpy_s(lpBuffer, iCount + 1, pDta, iCount);
GlobalUnlock(lclipBuffer);
SetClipboardData(CF_TEXT, lclipBuffer);
CloseClipboard();
bReturn = true;
}
return bReturn;
}
string GetBoardData()
{
char*buffer = NULL;
string fromClipboard;
if (OpenClipboard(NULL))
{
HANDLE hData = GetClipboardData(CF_TEXT);
char * buffer = (char*)GlobalLock(hData);
if ( buffer )
fromClipboard = buffer;
GlobalUnlock(hData);
CloseClipboard();
}
return fromClipboard;
}
/*char* WCharToAsni(const wchar_t* pDta)
{
int iLen = wcslen(pDta);
int iReLen = WideCharToMultiByte(CP_ACP, 0, pDta, iLen, NULL, 0, NULL, NULL);
char* pReDta = (char*)malloc(iReLen + 1);
WideCharToMultiByte(CP_ACP, 0, pDta, iLen, pReDta, iReLen, 0, NULL);
pReDta[iReLen] = '\0';
return pReDta;
}
wchar_t* AsnitoWChar(const char* pDta)
{
int iLen = strlen(pDta);
int iReLen = MultiByteToWideChar(CP_ACP, 0, pDta, iLen, NULL, NULL);
wchar_t* pReDta = (wchar_t*)malloc((iReLen + 1) * sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, pDta, iLen, pReDta, iReLen);
pReDta[iReLen] = '\0';
return pReDta;
}*/
static void DivideStr(CString sInfo, CString sDiv, vector<CString>& sArr)
{
sArr.clear();
int iPos = sInfo.Find(sDiv);
while (iPos != -1)
{
CString sCur = sInfo.Left(iPos);
sArr.push_back(sCur);
CString sBuf = sInfo.Right(sInfo.GetLength() - iPos - sDiv.GetLength());
sInfo = sBuf;
iPos = sInfo.Find(sDiv);
}
if (!sInfo.IsEmpty())
{
sArr.push_back(sInfo);
}
}
#define SETDTA( EXP ) if( sCur == #EXP ){\
vcDta.push_back( VK_##EXP );}
static void SendMessage()
{
g_bSend = true;
vector<CString> sArr;
DivideStr(g_sPressKey, TEXT("+"), sArr);
int iSize = sArr.size();
std::vector<UINT> vcDta;
for (int i = 0; i < iSize; ++i)
{
CString sCur = sArr[i];
sCur.MakeUpper();
if (sCur == "CTRL")
{
vcDta.push_back(VK_CONTROL);
}
else if (sCur == "ALT")
{
vcDta.push_back(VK_MENU);
}
else if (sCur == "ESC")
{
vcDta.push_back(VK_ESCAPE);
}
else if (sCur == "CAPS")
{
vcDta.push_back(VK_CAPITAL);
}
else if (sCur == "PGUP")
{
vcDta.push_back(VK_PRIOR);
}
else if (sCur == "PGDN")
{
vcDta.push_back(VK_DOWN);
}
else if (sCur == "ENTER")
{
vcDta.push_back(VK_RETURN);
}
else SETDTA(HELP)
else SETDTA(INSERT)
else SETDTA(END)
else SETDTA(HOME)
else SETDTA(LEFT)
else SETDTA(RIGHT)
else SETDTA(UP)
else SETDTA(DOWN)
else SETDTA(TAB)
else SETDTA(BACK)
else SETDTA(CLEAR)
else SETDTA(RETURN)
else SETDTA(SPACE)
else SETDTA(PAUSE)
else SETDTA(SHIFT)
else SETDTA(DELETE)
else SETDTA(F1)
else SETDTA(F2)
else SETDTA(F3)
else SETDTA(F4)
else SETDTA(F5)
else SETDTA(F6)
else SETDTA(F7)
else SETDTA(F8)
else SETDTA(F9)
else SETDTA(F10)
else SETDTA(F11)
else SETDTA(F12)
else
{
int iCurSize = sCur.GetLength();
for (int iIndex = 0; iIndex < iCurSize; ++iIndex)
{
vcDta.push_back(sCur[iIndex]);
}
}
}
iSize = vcDta.size();
for (int i = 0; i < iSize; ++i)
{
if (vcDta[i] == VK_CONTROL)
{
Sleep(100);
}
keybd_event(vcDta[i], 0, 0, 0);
Sleep(20);
}
for (int i = (iSize - 1); i >= 0; --i)
{
keybd_event(vcDta[i], 0, KEYEVENTF_KEYUP, 0);
}
}
BOOL CALLBACK EnumChildWindowCallBack(HWND hWnd, LPARAM lParam)
{
DWORD dwPid = 0;
GetWindowThreadProcessId(hWnd, &dwPid); // 获得找到窗口所属的进程
if (dwPid == lParam) // 判断是否是目标进程的窗口
{
EnumChildWindows(hWnd, EnumChildWindowCallBack, lParam); // 递归查找子窗口
}
return TRUE;
}
BOOL CALLBACK EnumWindowCallBack(HWND hWnd, LPARAM lParam)
{
DWORD dwPid = 0;
GetWindowThreadProcessId(hWnd, &dwPid); // 获得找到窗口所属的进程
if (dwPid == lParam) // 判断是否是目标进程的窗口
{
//printf("0x%08X ", hWnd); // 输出窗口信息
CWnd* pWnd = CWnd::FromHandle(hWnd);
if (!pWnd)
{
return TRUE;
}
CString sTxt;
pWnd->GetWindowText(sTxt);
sTxt.MakeUpper();
if (!g_bOnlyShowWnd)
{
if (sTxt == g_sProcessTitle)
{
g_vcHandle.push_back(hWnd);
pWnd->ShowWindow(SW_NORMAL);
SetForegroundWindow(hWnd);
SendMessage();
return FALSE;
}
EnumChildWindows(hWnd, EnumChildWindowCallBack, lParam); // 继续查找子窗口
}
else{
/*RECT rt;
pWnd->GetWindowRect( &rt );*/
/*bool bShow = false;
if( rt.left >= 0 && rt.right > 0 && rt.top >= 0 && rt.bottom > 0 && ( rt.right - rt.left ) > 5 && ( rt.bottom - rt.top ) > 5 )
{
bShow = true;
}*/
if (!sTxt.IsEmpty())
{
/*char szRect[512]={0};
sprintf(szRect, "%s,%s,%s,%s", rt.left, rt.right, rt.top, rt.bottom);
char szDta[1024];
sprintf( szDta, "%s,%s,visible:%s, Enable:%s,",sTxt, szRect, pWnd->IsWindowVisible() ? "true":"false", pWnd->IsWindowEnabled()? "true":"false");
MessageBox( NULL, szDta, NULL, MB_OK );*/
}
char sInfo[1024] = { 0 };
if ( /*bShow && */pWnd->IsWindowEnabled() && pWnd->IsWindowVisible() && !sTxt.IsEmpty())
{
if (pWnd->IsIconic())
{
pWnd->ShowWindow(SW_NORMAL);
}
SetForegroundWindow(hWnd);
pWnd->SetActiveWindow();
SendMessage();
return FALSE;
}
}
}
return TRUE;
}
static bool SendPressKey(CString sProcessName, CString sProcessTitle, CString sPresskey, bool bOnlyShowWnd = false)
{
g_bSend = false;
g_vcHandle.clear();
g_sProcessTitle = sProcessTitle;
sProcessName.MakeUpper();
g_sProcessTitle.MakeUpper();
g_sPressKey = sPresskey;
g_bOnlyShowWnd = bOnlyShowWnd;
setlocale(LC_CTYPE, "chs");
DWORD qqPid = 0; // 进程id
PROCESSENTRY32 pe; // 进程信息
pe.dwSize = sizeof(PROCESSENTRY32);
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); // 进程快照
if (!Process32First(hSnapshot, &pe)) // 得到第一个进程的快照
{
return false;
}
CString sCur = pe.szExeFile;
do
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
break;
}
sCur = pe.szExeFile;
sCur.MakeUpper();
if (sCur.Find( sProcessName) == 0 /*StrCmp(sCur, sProcessName) == 0*/)
{
qqPid = pe.th32ProcessID;
EnumWindows(EnumWindowCallBack, (LPARAM)qqPid);
}
} while (Process32Next(hSnapshot, &pe)); // 遍历进程直到找打目标进程
return g_bSend;
}
} // namespace demo
main.cpp
#include <iosteam>
#include “sendCmd.h”
using namespace std;
int main() {
sendkey::SendPressKey("iexplore.exe", "", "CTRL+T+CTRL+A+www.baidu.com+ENTER", true);
return 0;
}