Trở về
Tham gia nhóm m Autoit đ đưc hưng dn và gii đáp trc tiếp : http://fb.com/groups/autoitscript
Tin tức công nghệ  -  Thủ thuật lập trình

Saturday, September 28, 2013

Tương tác với webcam bằng thư viện Escapi


Toàn màn hìnhIn bài viết



ESCAPI là một giao diện liên kết động đơn giản để sử dụng các thiết bị quay video (phổ biến nhất là webcam,  video trong các thiết bị và những thiết bị tương tự khác).  

Về mặt kỹ thuật, nó chỉ là một wrapper đơn giản hơn Directmedia (hoặc DirectShow), nhưng đơn giản hoá như bạn không cần phải liên kết với các SDK mà chỉ cần lời gọi hàm trực tiếp.

 Escapi được tích hợp để có thể chạy trên bất kỳ nền tảng OS nào mà tương thích với các mono Webcam phhỏ biến. Escapi được đóng gòi sử dụng cho nhiều ngôn ngữ khác nhau.


 Escapi DLL bao gồm chức năng điều khiển camera như cân bằng, độ tương phản và màu sắc, thiết lập độ sáng.  

Escapi thư viện miễn phí chongười dùng cá nhân, tải Escapi tại trang chủ  http://sol.gfxile.net/escapi/index.html

Demo code về sử dụng Escapi để hiển thị hình ảnh webcam trên GUI bằng Autoit :




#Include <gdiplus.au3>
#Include <guiconstantsex.au3>

;--------------------------------------------------------------------------
; Local variables
Local $Width = 800 ;Escapi seems to use 640x480 (or lower) as webcam resolution and will
Local $Height = 600  ; adjust the image size to $Width by $Height
;--------------------------------------------------------------------------
; variables required for dshow escapi
Local $tagSimpleCapParams = _
"ptr mTargetBuf;" &amp; _
"int mWidth;" &amp; _
"int mHeight;"
Local $tSimpleCapParams = DllStructCreate($tagSimpleCapParams)
Local $tTargetBuf = DllStructCreate("BYTE[" &amp; $Width*$Height*4 &amp; "]")
Global $pTargetBuf = DllStructGetPtr($tTargetBuf)
DllStructSetData($tSimpleCapParams, 1, $pTargetBuf)
DllStructSetData($tSimpleCapParams, 2, $Width)
DllStructSetData($tSimpleCapParams, 3, $Height)
Local $pSimpleCapParams = DllStructGetPtr($tSimpleCapParams)
Local $device = 0 ;change this number to select dshow device
Local $sver = "--ERROR--"
Local $tCam = DllStructCreate("CHAR v[20]") ;buffer used to get ESCAPI device name
DllStructSetData($tCam, 1, $sver)
Local $pCam = DllStructGetPtr($tCam)
;---------------------------------------------------------------------------
;Escapi init and get Information
local $_escapi_Dll = DllOpen("escapi.dll")
Local $CamInfo
;ESCAPI version
$return = DllCall($_escapi_Dll,"int","ESCAPIDLLVersion")
$CamInfo &amp;= "Dll version = " &amp; Hex($return[0],3) &amp; @CRLF
$return = DllCall($_escapi_Dll,"int","initCOM")
; get number of available devices
$return = DllCall($_escapi_Dll,"int","countCaptureDevices")
$CamInfo &amp;= "Number of devices = " &amp; $return[0] &amp; @CRLF
; get selected device name, $device = 0
DllCall($_escapi_Dll,"none:cdecl","getCaptureDeviceName", "uint" , $device, "ptr" , $pCam  , "int" , 20)
Local $vCam = DllStructGetData($tCam , 1)
$CamInfo &amp;= "First device name = " &amp; $vCam &amp; @CRLF
;MsgBox(0,"ESCAPI info", $CamInfo)
$return = DllCall($_escapi_Dll,"int:cdecl","initCapture", "int", $device, "ptr", $pSimpleCapParams)
;---------------------------------------------------------------------------
; GUI and GDI init
Global $hwnd = GUICreate("EscApi WebCam", $Width, $Height)
GUISetState(@SW_SHOW,$hwnd)
_GDIPlus_Startup()
Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hwnd)
;---------------------------------------------------------------------------
;Get frame
DllCall($_escapi_Dll,"none:cdecl","doCapture", "int", $device)
Do
$return = DllCall($_escapi_Dll,"int:cdecl","isCaptureDone", "int", $device)
sleep(100)
Until $return[0] = 1
;---------------------------------------------------------------------------
;Display frame
While 1
DllCall($_escapi_Dll,"none:cdecl","doCapture", "int", $device)
$hBMP = _WinAPI_CreateBitmap($Width, $Height , 1 , 32 , $pTargetBuf)
Local $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
;$hImage =_GDIPlus_ImageGreyscale($hImage) ; Black White
_GDIPlus_ImageRotateFlip($hImage, 4) ; Flip Horizontal
_GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage, 0, 0, $Width, $Height)
_GDIPlus_BitmapDispose($hImage)
_WinAPI_DeleteObject($hBMP)
Local $msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
_ReduceMemory()
WEnd
;--------------------------------------------------------------------------
;Clean up
$return = DllCall($_escapi_Dll,"none:cdecl","deinitCapture", "int", $device)
DllClose($_escapi_Dll)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()
GUIDelete($hwnd)

Func _ReduceMemory()
 Local $dll_mem = DllOpen(@SystemDir &amp; "\psapi.dll")
 Local $ai_Return = DllCall($dll_mem, 'int', 'EmptyWorkingSet', 'long', -1)
 If @error Then Return SetError(@error, @error, 1)
 Return $ai_Return[0]
EndFunc   ;==&gt;_ReduceMemory


; #FUNCTION# ====================================================================================================================
; Name...........: _GDIPlus_ImageRotateFlip
; Description ...: Rotates and flips an image
; Syntax.........: _GDIPlus_ImageRotateFlip($hImage, $iRotateFlipType)
; Parameters ....: $hImage          - Pointer to an Image object
;                  $iRotateFlipType - Type of rotation and flip:
;                  |0 - No rotation and no flipping (A 180-degree rotation, a horizontal flip and then a vertical flip)
;                  |1 - A 90-degree rotation without flipping (A 270-degree rotation, a horizontal flip and then a vertical flip)
;                  |2 - A 180-degree rotation without flipping (No rotation, a horizontal flip folow by a vertical flip)
;                  |3 - A 270-degree rotation without flipping (A 90-degree rotation, a horizontal flip and then a vertical flip)
;                  |4 - No rotation and a horizontal flip (A 180-degree rotation followed by a vertical flip)
;                  |5 - A 90-degree rotation followed by a horizontal flip (A 270-degree rotation followed by a vertical flip)
;                  |6 - A 180-degree rotation followed by a horizontal flip (No rotation and a vertical flip)
;                  |7 - A 270-degree rotation followed by a horizontal flip (A 90-degree rotation followed by a vertical flip)
; Return values .: Success      - True
;                  Failure      - False and either:
;                  |@error and @extended are set if DllCall failed
;                  |$GDIP_STATUS contains a non zero value specifying the error code
; Remarks .......: None
; Related .......: None
; Link ..........; @@MsdnLink@@ GdipImageRotateFlip
; Example .......; No
; ===============================================================================================================================
Func _GDIPlus_ImageRotateFlip($hImage, $iRotateFlipType)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipImageRotateFlip", "hwnd", $hImage, "int", $iRotateFlipType)

    If @error Then Return SetError(@error, @extended, False)
    $GDIP_STATUS = $aResult[0]
    Return $aResult[0] = 0
EndFunc   ;==&gt;_GDIPlus_ImageRotateFlip

;; _GDIPlus_ImageGreyscale()
;;    Creates a greyscale copy of Image object and returns its handle. To destroy it, use _GDIPlus_ImageDispose() or _GDIPlus_BitmapDispose()
Func _GDIPlus_ImageGreyscale(Const ByRef $hImage)
    Local $tColorMatrix, $x, $hImgAttrib, $iW = _GDIPlus_ImageGetWidth($hImage), $iH = _GDIPlus_ImageGetHeight($hImage), $hGraphics, $hGraphics2, $hBitmap
;;create color matrix data
    $tColorMatrix = DllStructCreate("float[5];float[5];float[5];float[5];float[5]")
    ;greyscale values:
    $x = DllStructSetData($tColorMatrix, 1, 0.30, 1) * DllStructSetData($tColorMatrix, 1, 0.30, 2) * DllStructSetData($tColorMatrix, 1, 0.30, 3) * _
         DllStructSetData($tColorMatrix, 2, 0.59, 1) * DllStructSetData($tColorMatrix, 2, 0.59, 2) * DllStructSetData($tColorMatrix, 2, 0.59, 3) * _
         DllStructSetData($tColorMatrix, 3, 0.11, 1) * DllStructSetData($tColorMatrix, 3, 0.11, 2) * DllStructSetData($tColorMatrix, 3, 0.11, 3) * _
         DllStructSetData($tColorMatrix, 4, 1.00, 4) * DllStructSetData($tColorMatrix, 5, 1.00, 5)
;;create an image attributes object and update its color matrix
    $hImgAttrib = _GDIPlus_ImageAttributesCreate()
    _GDIPlus_ImageAttributesSetColorMatrix($hImgAttrib, 1, DllStructGetPtr($tColorMatrix))
;;copy image
    $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage)
    $hBitmap = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $hGraphics)
    $hGraphics2 = _GDIPlus_ImageGetGraphicsContext($hBitmap)
;;draw original into copy with attributes
    _GDIPlus_GraphicsDrawImageRectRectEx($hGraphics2, $hImage, 0, 0, $iW, $iH, 0, 0, $iW, $iH, 2, $hImgAttrib)
;;clean up
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_GraphicsDispose($hGraphics2)
    _GDIPlus_ImageAttributesDispose($hImgAttrib)

    Return $hBitmap
EndFunc

;;;;; other GDIPlus wrappers ;;;;;

Func _GDIPlus_ImageAttributesSetColorMatrix($hImgAttrib, $iColorAdjustType, $pColorMatrix = 0, $pGrayMatrix = 0, $iColorMatrixFlags = 0)
    Local $fEnable = 1, $aResult = DllCall($ghGDIPDll, "int", "GdipSetImageAttributesColorMatrix", "ptr",$hImgAttrib, "int",$iColorAdjustType, _
                                            "int",$fEnable, "ptr",$pColorMatrix, "ptr",$pGrayMatrix, "int",$iColorMatrixFlags)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc

;;Creates ImageAttributes object
Func _GDIPlus_ImageAttributesCreate()
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipCreateImageAttributes", "ptr*", 0)
    Return SetError($aResult[0], 0, $aResult[1])
EndFunc

;;Deletes ImageAttributes object
Func _GDIPlus_ImageAttributesDispose($hImgAttrib)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipDisposeImageAttributes", "ptr", $hImgAttrib)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc


;; Same as _GDIPlus_GraphicsDrawImageRectRect(), but adds 1 optional parameter - $hImgAttrib (handle to ImageAttributes object)
Func _GDIPlus_GraphicsDrawImageRectRectEx($hGraphics, $hImage, $iSrcX, $iSrcY, $iSrcWidth, $iSrcHeight, $iDstX, $iDstY, $iDstWidth, $iDstHeight, $iUnit = 2, $hImgAttrib = 0)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipDrawImageRectRectI", "hwnd", $hGraphics, "hwnd", $hImage, "int", $iDstX, "int", _
            $iDstY, "int", $iDstWidth, "int", $iDstHeight, "int", $iSrcX, "int", $iSrcY, "int", $iSrcWidth, "int", _
            $iSrcHeight, "int", $iUnit, "ptr", $hImgAttrib, "int", 0, "int", 0)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc