CONST CREATE_PALETTE=1
CONST PALETTE_TO_WEBCAM=0


TYPE QWebCam EXTENDS QOBJECT
	sts AS BYTE

	SUB CleanUp
		IF This.sts=4 THEN WEBCAM_OverlayOFF
		IF This.sts=3 THEN WEBCAM_PreviewOFF
		IF This.sts>1 THEN WEBCAM_Disconnect
		IF This.sts>0 THEN WEBCAM_CloseWindow
	END SUB

	FUNCTION CamInit(Form AS QFORM)			'Create Cap Window, init the web cam
		DIM rtn AS LONG

		IF This.sts THEN EXIT SUB				'been here before? Then don't run
		rtn=WEBCAM_OpenWindow(Form.Handle)	' Open Capture (Child) Window for WebCam
		IF rtn <> 1 THEN
			RESULT = rtn
			EXIT FUNCTION
		END IF

		This.sts=1								'set a status code
		rtn=WEBCAM_Connect					'Connect the WebCam Driver.
		IF rtn=1 THEN
 		WEBCAM_GetCapabilities			'These next two calls MUST be called,
		WEBCAM_GetCaptureOptions		'otherwise the WebCam will not work properly
		rtn=WEBCAM_IsConnected			'Check if WebCam connected to its Driver.
		IF rtn=1 THEN
			This.sts=2
			WEBCAM_SetPreviewRate(1)
			' Check if the WebCam supports Overlay (Direct Hardware) mode or just the
			' standard (cheap "Off the Market" WebCam!) software (CPU Expensive) mode.
			rtn=WEBCAM_CanItOverlay
			IF rtn=1 THEN
				WEBCAM_OverlayON
				This.sts=4
			ELSE
				WEBCAM_PreviewON	'WEBCAM_PreviewScalingON
				This.sts=3
			END IF
		END IF
		RESULT = rtn
	ELSE
		ShowMessage("WebCam could not Connect")
	END IF
END SUB


'SUB BC2(Sender AS QBUTTON)		'Record button
'  IF This.sts>1 THEN
	'rtn=WEBCAM_GetInformation
	'ShowMessage(VarPtr$(rtn))
	'ShowMessage(VarPtr$(rtn+100))

	'ShowMessage(VarPtr$(rtn+204))
	'ShowMessage(VarPtr$(rtn+304))

	'rtn=WEBCAM_GetConnectedName
	'ShowMessage(VarPtr$(rtn))
	'rtn=WEBCAM_GetConnectedVersion
	'ShowMessage(VarPtr$(rtn))

'	rtn=OPTION_SetOKMessage(Show)
'	rtn=OPTION_SetCaptureTimer(1,4)

	' This method changes the name and size of the CAPTURE.AVI file.
	'WEBCAM_SetCaptureFilename("C:\\Test.avi")
	'WEBCAM_SetCaptureFilesize(1080000000) ' 1Gb = 1,080,000,000

	' This method changes the size of the CAPTURE.AVI file to 1Gb and then
	' creates C:\Test.Avi so that C:\Test.Avi ends up containing only the
	' actual video (i.e. a video of size 1.??Mb).
'	WEBCAM_SetCaptureFilesize(1080000000) ' 1Gb = 1,080,000,000
'	WEBCAM_SetCaptureFilename("C:\\Test.avi")

	'WEBCAM_SetSaveFilename("C:\\TestCopy.avi")
'	WEBCAM_StartCapturing

'  END IF
'END SUB


SUB ShowDialogBox(TheBoxNum)		'get info button
' DIM WebCInfo AS STRING

'	$ESCAPECHARS ON
'	WebCInfo = ""		'this is actually a STATIC
'	WebCInfo = WebCInfo + STR$(WEBCAM_GetIDNumber) + "=ID# \n"
'	WEBCAM_GetCaptureStatus
'	WebCInfo = WebCInfo + STR$(STATUS_GetImageWidth) + "=ImageWidth \n"
'	WebCInfo = WebCInfo + STR$(STATUS_GetImageHeight) + "=ImageHeight \n"
'	WebCInfo = WebCInfo + STR$(STATUS_IsOverlayON) + "=OverlayON \n"
'	WebCInfo = WebCInfo + STR$(STATUS_IsPreviewON) + "=PreviewON \n"
'	WebCInfo = WebCInfo + STR$(STATUS_IsPreviewScalingON) + "=PreviewScalingON \n"
'	WebCInfo = WebCInfo + STR$(STATUS_IsUsingDefaultPalette) + "=UsingDefaultPalette \n"
'	WebCInfo = WebCInfo + STR$(STATUS_IsAudioInstalled) + "=AudioInstalled \n"
	'WebCInfo = WebCInfo + STR$(STATUS_DoesCaptureFileExist
	'WebCInfo = WebCInfo + STR$(STATUS_IsCapturingNow

	IF TheBoxNum THEN WEBCAM_StopCapturing
	SELECT CASE TheBoxNum
	CASE 1
		DIALOG_VideoCompression
	CASE 2
		DIALOG_VideoDisplay
	CASE 3
		DIALOG_VideoFormat
	CASE 4
		DIALOG_VideoSource
	END SELECT
	CASE 5
END SUB


SUB Start()
	WEBCAM_StartCapturing
END SUB



FUNCTION GrabFrameToClipBoard
	DIM i as integer
	DIM T as single
	DIM info AS TBITMAP
	dim rtn as long
	Dim hBitmap as long

	WEBCAM_PreviewScalingOFF
	WEBCAM_PreviewOFF
	FRAME_Grab					'slow, about7.5 hz
	FRAME_CopyToClipboard		'fast

    IF clipboard.hasformat(CF_BITMAP) then		
      clipboard.open
      hBitmap=clipboard.GetAsHandle(CF_BITMAP)
      clipboard.close
	else
		showmessage "Cliboard is not in a valid format"
	end if

	RESULT = GetObject(Bitmap1.Handle,SIZEOF(info),info)
	RESULT = GetObject(hBitmap,SIZEOF(info),info)
	WEBCAM_PreviewON

'	FRAME_CopyToDIB("C:\\TestDIB.bmp") ' Save with/out an extension (.jpg, etc)

	'PALETTE_SaveAsFile("C:\\Test.PAL")
	'PALETTE_ClipboardToWebcam
	'PALETTE_ManualCreate(CREATE_PALETTE,256)
	'PALETTE_ManualCreate(PALETTE_TO_WEBCAM,256)
	'PALETTE_AutoCreate(1000,256)
	'PALETTE_FileToWebcam("C:\\Test.PAL")
END FUNCTION


	CONSTRUCTOR
		sts=0
	END CONSTRUCTOR
END TYPE
