Sign in to follow this  
Followers 0
nullschritt

webcam not saving current frames to file (GDI+ locking file)

6 posts in this topic

#1 ·  Posted (edited)

Hello all, I am working on an instant messenger application, and am working towards adding video call support, however I seem to have a problem, the script only grabs the first frame of the webcam control, and uses that same frame every other time a snapshot is taken, this very same code used to work perfectly when I used it with a small http server script to make a webhosted cam.

 

Below is my test code

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstants.au3>
#include <Webcam.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

#include <GDIPlus.au3>
#include <Memory.au3>

$gui = GUICreate("Webcam UDF Test",320,240)
_WebcamInit()
$camcontrol = _Webcam($gui,320,240,0,0)
GUISetState(@SW_SHOW)

$timer = TimerInit()
While 1
 $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then
  _WebcamStop()
  Exit
 EndIf
 Sleep(10)
 if TimerDiff($timer) > 2000 Then

    _WebcamSnapShot()
    _GDIPlus_Startup()
    Local $hBitmap = _GDIPlus_BitmapCreateFromFile($snapfile)

    ; This is the binary data of your image.
    FileDelete(@ScriptDir&"\testing.jpg")
    FileWrite(@ScriptDir&"\testing.jpg", _GDIPlus_SaveImage2Binary($hBitmap, 60))
    _GDIPlus_Shutdown()
     $timer = TimerInit()
 EndIf

WEnd
 
Func _GDIPlus_CreateBitmapFromStream($pStream)
 
    Local $Ret = DllCall($ghGDIPDll, 'uint', 'GdipCreateBitmapFromStream', 'ptr', $pStream, 'ptr*', 0)
 
    If (@error) Or ($Ret[0]) Then
        Return SetError(@error, @extended, 0)
    EndIf
    Return $Ret[2]
EndFunc   ;==>_GDIPlus_CreateBitmapFromStream
 
 

This creates a webcam to capture from, then converts the resulting file into a jpeg file, you will notice it;s the same jpeg file over and over

 

oh yeah and here's Webcam.au3

 

I don't know if I'm doing something wrong or if perhaps this is an autoit bug?

Edit:

Local $hBitmap = _GDIPlus_BitmapCreateFromFile($snapfile) 
It seems this line locks the snapshot file preventing it from being overwritten, how can I  "unlock" the file?

 

(after executing this line, I cannot modify or delete the file until the script closes)

Edited by nullschritt

Share this post


Link to post
Share on other sites



Can anyone at least confirm this is a problem on their system, or if it's just a problem I am experiencing on my system?

Share this post


Link to post
Share on other sites

nullschritt, there seems to be a problem after the GDI+ initialising part in your code which results in AutoIt crashing. I modified it (a lot) to check for the errors:

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstants.au3>
#include <Webcam.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

#include <GDIPlus.au3>
#include <Memory.au3>

$gui = GUICreate("Webcam UDF Test",320,240)
_WebcamInit()
$camcontrol = _Webcam($gui,320,240,0,0)
GUISetState(@SW_SHOW)

Global $i = 1

While 1
$i = $i+1
Sleep(1000)
_MyFunc()
WEnd

Func _MyFunc()
 $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then
  _WebcamStop()
  Exit
 EndIf

    _WebcamSnapShot(@ScriptDir & "\snap" & $i & ".bmp")
    
    
EndFunc

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

The GDI is necessary, it converts the bmp to jpeg, I forgot to include some of the code for it, here it is.

Func _GDIPlus_CreateBitmapFromStream($pStream)

    Local $Ret = DllCall($ghGDIPDll, 'uint', 'GdipCreateBitmapFromStream', 'ptr', $pStream, 'ptr*', 0)

    If (@error) Or ($Ret[0]) Then
        Return SetError(@error, @extended, 0)
    EndIf
    Return $Ret[2]
EndFunc   ;==>_GDIPlus_CreateBitmapFromStream
Edited by nullschritt

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

 

nullschritt, there seems to be a problem after the GDI+ initialising part in your code which results in AutoIt crashing. I modified it (a lot) to check for the errors:

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstants.au3>
#include <Webcam.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

#include <GDIPlus.au3>
#include <Memory.au3>

$gui = GUICreate("Webcam UDF Test",320,240)
_WebcamInit()
$camcontrol = _Webcam($gui,320,240,0,0)
GUISetState(@SW_SHOW)

Global $i = 1

While 1
$i = $i+1
Sleep(1000)
_MyFunc()
WEnd

Func _MyFunc()
 $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then
  _WebcamStop()
  Exit
 EndIf

    _WebcamSnapShot(@ScriptDir & "\snap" & $i & ".bmp")
    
    
EndFunc

Also this code seems to work, but I need to write to the same file over and over not a new file with a new name, so that the data can be read and sent over an internet connection, is it possible to have it save to the same file over and over, and always save the newest snapshot to it?

edit:it seems to overwrite itself perfectly, until I preform a gdiplus function on the image.

Edited by nullschritt

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Local $hBitmap = _GDIPlus_BitmapCreateFromFile($snapfile) 

It seems this line locks the snapshot file preventing it from being overwritten, how can I  "unlock" the file?

 

(after executing this line, I cannot modify or delete the file until the script closes)

Edit:

I had to add in

 

_GDIPlus_BitmapDispose($hBitmapxx)
Edited by nullschritt

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0