API JNA e Threads - FORZEN WINDOW
914633Feb 1 2012 — edited Feb 1 2012I'm creating one aplication JAVA DESKTOP for open the WEBCAM USB or NATIVE.
I'm using JNA for open library native windows avicap.dll
My system operation is Windows 7
I have 3 interfaces:
One have name MapHWND
package interf;
import com.sun.jna.FromNativeContext;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIFunctionMapper;
import com.sun.jna.win32.W32APITypeMapper;
import java.util.HashMap;
import java.util.Map;
public interface MapHWND extends StdCallLibrary{
Map UNICODE_OPTIONS = new HashMap() {
{
put(OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE);
put(OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.UNICODE);
}
};
Map ASCII_OPTIONS = new HashMap() {
{
put(OPTION_TYPE_MAPPER, W32APITypeMapper.ASCII);
put(OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.ASCII);
}
};
Map DEFAULT_OPTIONS = Boolean.getBoolean("w32.ascii") ? ASCII_OPTIONS
: UNICODE_OPTIONS;
public static class HANDLE extends PointerType {
@Override
public Object fromNative(Object nativeValue, FromNativeContext context) {
Object o = super.fromNative(nativeValue, context);
if (INVALID_HANDLE_VALUE.equals(o)) {
return INVALID_HANDLE_VALUE;
}
return o;
}
}
public static class HWND extends HANDLE {
}
HANDLE INVALID_HANDLE_VALUE = new HANDLE() {
{
super.setPointer(Pointer.createConstant(-1));
}
@Override
public void setPointer(Pointer p) {
throw new UnsupportedOperationException("Immutable reference");
}
};
}
Other have name Avicap32:
package interf;
import com.sun.jna.win32.StdCallLibrary;
public interface Avicap32 extends StdCallLibrary, MapHWND{
//Métodos
public int capCreateCaptureWindowA(
String lpszWindowName, //Create a function od capture windows
int dwStye, //style of window
int x, //..
int y, //..
int nWidth, //..
int nHeight, //position and size of window
int hWndParent, /identify of father window
int nID); //window ID
//********************************* Constantes ********************************
/******************************************************************************/
public static final short WM_USER = 1024;
public static final int WM_CAP_START = WM_USER;
public static int WS_CHILD = 0x40000000;
public static int WS_VISIBLE = 0x10000000;
public static int SWP_NOZORDER = 0x4;
public static int HWND_BOTTOM = 1;
public static int SWP_NOMOVE = 0x2;
// Defines start of the message range
public static final int WM_CAP_GET_CAPSTREAMPTR = (WM_CAP_START+ 1);
public static final int WM_CAP_SET_CALLBACK_ERROR = (WM_CAP_START+ 2);
public static final int WM_CAP_SET_CALLBACK_STATUS = (WM_CAP_START+ 3);
public static final int WM_CAP_SET_CALLBACK_YIELD = (WM_CAP_START+ 4);
public static final int WM_CAP_SET_CALLBACK_FRAME = (WM_CAP_START+ 5);
public static final int WM_CAP_SET_CALLBACK_VIDEOSTREAM = (WM_CAP_START+ 6);
public static final int WM_CAP_SET_CALLBACK_WAVESTREAM = (WM_CAP_START+ 7);
public static final int WM_CAP_GET_USER_DATA = (WM_CAP_START+ 8);
public static final int WM_CAP_SET_USER_DATA = (WM_CAP_START+ 9);
public static final int WM_CAP_DRIVER_CONNECT = 0x40a;//(WM_CAP_START+ 10);
public static final int WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START+ 11);
public static final int WM_CAP_DRIVER_GET_NAME = (WM_CAP_START+ 12);
public static final int WM_CAP_DRIVER_GET_VERSION = (WM_CAP_START+ 13);
public static final int WM_CAP_DRIVER_GET_CAPS = (WM_CAP_START+ 14);
public static final int WM_CAP_FILE_SET_CAPTURE_FILE = (WM_CAP_START+ 20);
public static final int WM_CAP_FILE_GET_CAPTURE_FILE = (WM_CAP_START+ 21);
public static final int WM_CAP_FILE_ALLOCATE = (WM_CAP_START+ 22);
public static final int WM_CAP_FILE_SAVEAS = (WM_CAP_START+ 23);
public static final int WM_CAP_FILE_SET_INFOCHUNK = (WM_CAP_START+ 24);
public static final int WM_CAP_FILE_SAVEDIB = (WM_CAP_START+ 25);
public static final int WM_CAP_EDIT_COPY = (WM_CAP_START+ 30);
public static final int WM_CAP_SET_AUDIOFORMAT = (WM_CAP_START+ 35);
public static final int WM_CAP_GET_AUDIOFORMAT = (WM_CAP_START+ 36);
public static final int WM_CAP_DLG_VIDEOFORMAT = (WM_CAP_START+ 41);
public static final int WM_CAP_DLG_VIDEOSOURCE = (WM_CAP_START+ 42);//usb
public static final int WM_CAP_DLG_VIDEODISPLAY = (WM_CAP_START+ 43);
public static final int WM_CAP_GET_VIDEOFORMAT = (WM_CAP_START+ 44);
public static final int WM_CAP_SET_VIDEOFORMAT = (WM_CAP_START+ 45);
public static final int WM_CAP_DLG_VIDEOCOMPRESSION = (WM_CAP_START+ 46);
public static final int WM_CAP_SET_PREVIEW = 0x432;//(WM_CAP_START+ 50);
public static final int WM_CAP_SET_OVERLAY = (WM_CAP_START+ 51);
public static final int WM_CAP_SET_PREVIEWRATE = 0x434;//(WM_CAP_START+ 52);
public static final int WM_CAP_SET_SCALE = 0x435;//(WM_CAP_START+ 53);
public static final int WM_CAP_GET_STATUS = (WM_CAP_START+ 54);
public static final int WM_CAP_SET_SCROLL = (WM_CAP_START+ 55);
public static final int WM_CAP_GRAB_FRAME = (WM_CAP_START+ 60);
public static final int WM_CAP_GRAB_FRAME_NOSTOP = (WM_CAP_START+ 61);
public static final int WM_CAP_SEQUENCE = (WM_CAP_START+ 62);
public static final int WM_CAP_SEQUENCE_NOFILE = (WM_CAP_START+ 63);
public static final int WM_CAP_SET_SEQUENCE_SETUP = (WM_CAP_START+ 64);
public static final int WM_CAP_GET_SEQUENCE_SETUP = (WM_CAP_START+ 65);
public static final int WM_CAP_SET_MCI_DEVICE = (WM_CAP_START+ 66);
public static final int WM_CAP_GET_MCI_DEVICE = (WM_CAP_START+ 67);
public static final int WM_CAP_STOP = (WM_CAP_START+ 68);
public static final int WM_CAP_ABORT = (WM_CAP_START+ 69);
public static final int WM_CAP_SINGLE_FRAME_OPEN = (WM_CAP_START+ 70);
public static final int WM_CAP_SINGLE_FRAME_CLOSE = (WM_CAP_START+ 71);
public static final int WM_CAP_SINGLE_FRAME = (WM_CAP_START+ 72);
public static final int WM_CAP_PAL_OPEN = (WM_CAP_START+ 80);
public static final int WM_CAP_PAL_SAVE = (WM_CAP_START+ 81);
public static final int WM_CAP_PAL_PASTE = (WM_CAP_START+ 82);
public static final int WM_CAP_PAL_AUTOCREATE = (WM_CAP_START+ 83);
public static final int WM_CAP_PAL_MANUALCREATE = (WM_CAP_START+ 84);
// Following added post VFW 1.1
public static final int WM_CAP_SET_CALLBACK_CAPCONTROL = (WM_CAP_START+ 90);
// Defines end of the message range
public static final int WM_CAP_END = WM_CAP_SET_CALLBACK_CAPCONTROL;
public static final int IDS_CAP_BEGIN = 300; //(* "Capture Start" *)
public static final int IDS_CAP_END = 301; //(* "Capture End" *)
public static final int IDS_CAP_INFO = 401; //(* "%s" *)
public static final int IDS_CAP_OUTOFMEM = 402; //(* "Out of memory" *)
public static final int IDS_CAP_FILEEXISTS = 403; //(* "File '%s' exists -- overwrite it?" *)
public static final int IDS_CAP_ERRORPALOPEN = 404; //(* "Error opening palette '%s'" *)
public static final int IDS_CAP_ERRORPALSAVE = 405; //(* "Error saving palette '%s'" *)
public static final int IDS_CAP_ERRORDIBSAVE = 406; //(* "Error saving frame '%s'" *)
public static final int IDS_CAP_DEFAVIEXT = 407; //(* "avi" *)
public static final int IDS_CAP_DEFPALEXT = 408; //(* "pal" *)
public static final int IDS_CAP_CANTOPEN = 409; //(* "Cannot open '%s'" *)
public static final int IDS_CAP_SEQ_MSGSTART = 410; //(* "Select OK to start capture\nof video sequence\nto %s." *)
public static final int IDS_CAP_SEQ_MSGSTOP = 411; //(* "Hit ESCAPE or click to end capture" *)
public static final int IDS_CAP_VIDEDITERR = 412; //(* "An error occurred while trying to run VidEdit." *)
public static final int IDS_CAP_READONLYFILE = 413; //(* "The file '%s' is a read-only file." *)
public static final int IDS_CAP_WRITEERROR = 414; //(* "Unable to write to file '%s'.\nDisk may be full." *)
public static final int IDS_CAP_NODISKSPACE = 415; //(* "There is no space to create a capture file on the specified device." *)
public static final int IDS_CAP_SETFILESIZE = 416; //(* "Set File Size" *)
public static final int IDS_CAP_SAVEASPERCENT = 417; //(* "SaveAs: %2ld%% Hit Escape to abort." *)
public static final int IDS_CAP_DRIVER_ERROR = 418; //(* Driver specific error message *)
public static final int IDS_CAP_WAVE_OPEN_ERROR = 419; //(* "Error: Cannot open the wave input device.\nCheck sample size, frequency, and channels." *)
public static final int IDS_CAP_WAVE_ALLOC_ERROR = 420; //(* "Error: Out of memory for wave buffers." *)
public static final int IDS_CAP_WAVE_PREPARE_ERROR = 421; //(* "Error: Cannot prepare wave buffers." *)
public static final int IDS_CAP_WAVE_ADD_ERROR = 422; //(* "Error: Cannot add wave buffers." *)
public static final int IDS_CAP_WAVE_SIZE_ERROR = 423; //(* "Error: Bad wave size." *)
public static final int IDS_CAP_VIDEO_OPEN_ERROR = 424; //(* "Error: Cannot open the video input device." *)
public static final int IDS_CAP_VIDEO_ALLOC_ERROR = 425; //(* "Error: Out of memory for video buffers." *)
public static final int IDS_CAP_VIDEO_PREPARE_ERROR = 426; //(* "Error: Cannot prepare video buffers." *)
public static final int IDS_CAP_VIDEO_ADD_ERROR = 427; //(* "Error: Cannot add video buffers." *)
public static final int IDS_CAP_VIDEO_SIZE_ERROR = 428; //(* "Error: Bad video size." *)
public static final int IDS_CAP_FILE_OPEN_ERROR = 429; //(* "Error: Cannot open capture file." *)
public static final int IDS_CAP_FILE_WRITE_ERROR = 430; //(* "Error: Cannot write to capture file. Disk may be full." *)
public static final int IDS_CAP_RECORDING_ERROR = 431; //(* "Error: Cannot write to capture file. Data rate too high or disk full." *)
public static final int IDS_CAP_RECORDING_ERROR2 = 432; //(* "Error while recording" *)
public static final int IDS_CAP_AVI_INIT_ERROR = 433; //(* "Error: Unable to initialize for capture." *)
public static final int IDS_CAP_NO_FRAME_CAP_ERROR = 434; //(* "Warning: No frames captured.\nConfirm that vertical sync interrupts\nare configured and enabled." *)
public static final int IDS_CAP_NO_PALETTE_WARN = 435; //(* "Warning: Using default palette." *)
public static final int IDS_CAP_MCI_CONTROL_ERROR = 436; //(* "Error: Unable to access MCI device." *)
public static final int IDS_CAP_MCI_CANT_STEP_ERROR = 437; //(* "Error: Unable to step MCI device." *)
public static final int IDS_CAP_NO_AUDIO_CAP_ERROR = 438; //(* "Error: No audio data captured.\nCheck audio card settings." *)
public static final int IDS_CAP_AVI_DRAWDIB_ERROR = 439; //(* "Error: Unable to draw this data format." *)
public static final int IDS_CAP_COMPRESSOR_ERROR = 440; //(* "Error: Unable to initialize compressor." *)
public static final int IDS_CAP_AUDIO_DROP_ERROR = 441; //(* "Error: Audio data was lost during capture, reduce capture rate." *)
//(* status string IDs *)
public static final int IDS_CAP_STAT_LIVE_MODE = 500; //(* "Live window" *)
public static final int IDS_CAP_STAT_OVERLAY_MODE = 501; //(* "Overlay window" *)
public static final int IDS_CAP_STAT_CAP_INIT = 502; //(* "Setting up for capture - Please wait" *)
public static final int IDS_CAP_STAT_CAP_FINI = 503; //(* "Finished capture, now writing frame %ld" *)
public static final int IDS_CAP_STAT_PALETTE_BUILD = 504; //(* "Building palette map" *)
public static final int IDS_CAP_STAT_OPTPAL_BUILD = 505; //(* "Computing optimal palette" *)
public static final int IDS_CAP_STAT_I_FRAMES = 506; //(* "%d frames" *)
public static final int IDS_CAP_STAT_L_FRAMES = 507; //(* "%ld frames" *)
public static final int IDS_CAP_STAT_CAP_L_FRAMES = 508; //(* "Captured %ld frames" *)
public static final int IDS_CAP_STAT_CAP_AUDIO = 509; //(* "Capturing audio" *)
public static final int IDS_CAP_STAT_VIDEOCURRENT = 510; //(* "Captured %ld frames (%ld dropped) %d.%03d sec." *)
public static final int IDS_CAP_STAT_VIDEOAUDIO = 511; //(* "Captured %d.%03d sec. %ld frames (%ld dropped) (%d.%03d fps). %ld audio bytes (%d,%03d sps)" *)
public static final int IDS_CAP_STAT_VIDEOONLY = 512; //(* "Captured %d.%03d sec. %ld frames (%ld dropped) (%d.%03d fps)" *)
public static final int IDS_CAP_STAT_FRAMESDROPPED = 513; //(* "Dropped %ld of %ld frames (%d.%02d%%) during capture." *)
}
And fiishing, the interface User32:
package interf;
import com.sun.jna.win32.StdCallLibrary;
public interface User32 extends StdCallLibrary, MapHWND{
public boolean DestroyWindow(int hndw);
public boolean ShowWindow(long hWnd, int nCmdShow);
public HWND FindWindow(String winClass, String title);
public int SetWindowPos(
int hwnd,
int hWndInsertAfter,
int x,
int y,
int cx,
int cy,
int wFlags);
public int SendMessageA(
int hwnd,
int wMsg,
int wParam,
int lParam);
}
}
I have a JFrame with a botton and a painel of palet AWT.
In the universal variables and threading
//chamando as APIs
Avicap32 libA = (Avicap32) Native.loadLibrary("AviCap32", Avicap32.class);
User32 libU = (User32) Native.loadLibrary("User32", User32.class);
int hWnd;
int DeviceID = 0;
String DeviceIndex = String.valueOf(DeviceID);
int id, idt;
public class LoopFrame implements Runnable{
@Override
public void run() {
int i = 0;
libU.SendMessageA(hWnd, Avicap32.WM_CAP_DRIVER_CONNECT, DeviceID, id);
for(;;){
//libU.SendMessageA(hWnd, Avicap32.WM_CAP_SET_SCALE, 0, hWnd);
//libU.SendMessageA(hWnd, Avicap32.WM_CAP_SET_PREVIEWRATE, 66, hWnd);
//libU.SendMessageA(hWnd, Avicap32.WM_CAP_SET_PREVIEW, -1, hWnd);
libU.SendMessageA(hWnd, Avicap32.WM_CAP_GRAB_FRAME, -1, id);
libU.SetWindowPos(hWnd, Avicap32.HWND_BOTTOM, 0, 0, 320, 240, Avicap32.SWP_NOMOVE | Avicap32.SWP_NOZORDER);
System.out.print("\n"+i++);
}
}
}
public void starting(){
Thread thrLoop = new Thread(new LoopFrame());
repaint();
thrLoop.start();
}
In the event botton a have:
//pImga é o painel da paleta AWT
WComponentPeer cpp = (WComponentPeer) pImage.getPeer();
long h = cpp.getHWnd();
id = (int)h;
WComponentPeer cppT = (WComponentPeer) this.getPeer();
long hh = cppT.getHWnd();
idt = (int)hh;
try{
hWnd = libA.capCreateCaptureWindowA("0", Avicap32.WS_VISIBLE | Avicap32.WS_CHILD, 0, 0, 0, 0, id, 0);
}catch(Exception ex){JOptionPane.showMessageDialog(null, ex.toString());}
starting();
}
Executing, my apllication is frozen!
I don't that it's happening.
Please, someone explain me.