I've written a JAva program that uses JNI to call some C++ methods to retrieve webcam images using video for windows. The problem is that every time I retrieve an image, memory usage keeps going up. I'm still a noob to C++ so the problem may be something really simple. Here's my code:
Code:
HWND m_capWindow;
int connectedIndex;
LRESULT PASCAL callbackProc(HWND hWnd, LPVIDEOHDR lpVHdr);
LRESULT PASCAL callbackProc(HWND hWnd, LPVIDEOHDR lpVHdr)
{
return 0;
}
JNIEXPORT void JNICALL Java_client_desktop_dateclick_Test_connect
(JNIEnv *, jobject, jint index) {
m_capWindow = capCreateCaptureWindow ("", WS_CHILD, 0, 0, 0, 0, GetDesktopWindow(), 10);
//m_capWindow = capCreateCaptureWindow ("Capture", WS_CHILD | WS_VISIBLE, 50, 50, 480, 360, GetDesktopWindow(), 10);
// connect to the new driver
BOOL rval = capDriverConnect (m_capWindow, index);
capSetCallbackOnFrame(m_capWindow, callbackProc);
capPreview(m_capWindow, FALSE);
}
JNIEXPORT jbyteArray JNICALL Java_client_desktop_dateclick_Test_getImage
(JNIEnv *env, jobject) {
CString fileName;
int contentLength;
BYTE *frame;
capGrabFrame(m_capWindow);
//capGrabFrameNoStop(m_capWindow);
fileName = "capframe.bmp"; //and use this file...
capFileSaveDIB( m_capWindow, (LPCTSTR)fileName);
CFile jpegTemp;
// now read the resulting file into a buffer
jpegTemp.Open("capframe.bmp", CFile::modeRead);
contentLength = (UINT)jpegTemp.GetLength();
//allocate the buffer for the jpeg frame
frame = new BYTE[contentLength];
//now read in the jpeg file
jpegTemp.Read(frame, contentLength);
jpegTemp.Close();
jbyteArray ba = env->NewByteArray(contentLength);
env->SetByteArrayRegion(ba, 0, contentLength, (jbyte*) frame);
delete frame
return ba;
}
JNIEXPORT void JNICALL Java_client_desktop_dateclick_Test_disconnect
(JNIEnv *env, jobject) {
capDriverDisconnect(m_capWindow);
}