[WIN] Webcam class mit YUY2 kompabilität!

Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Neue Antwort erstellen

 

naibaf7

Betreff: [WIN] Webcam class mit YUY2 kompabilität!

BeitragSo, Okt 04, 2009 1:25
Antworten mit Zitat
Benutzer-Profile anzeigen
Hi...
auf blitzbasic.com:
http://blitzbasic.com/codearcs...?code=1437
habe ich diesen Code für das auslesen von webcams gefunden...
jedoch funktioniert der nur für sauberes RGB-Bild von der Webcam... nicht etwa für YUY2 oder andere bildformate...

Darum hier der code (der mit den meisten webcams gehen sollte, muss aber eventuell individuell angepasst werden):
Code: [AUSKLAPPEN]



'##############################################  WEBCAM  #####################################################'
'Webcam Globals-----------------------------------------------------------------------------------------------'
Global webcam_GetActiveWindow:Int () "Win32"
Global webcam_SendMessage( hWnd,MSG,wParam,lParam) "Win32"
Global webcam_FreeLibrary ( hnd:Int ) "Win32"
Global TempLoc = CreateBank(921600)

user32 = LoadLibraryA ("user32.dll")

If user32 webcam_GetActiveWindow = GetProcAddress (user32, "GetActiveWindow")
If user32 webcam_SendMessage = GetProcAddress (user32, "SendMessageA")

kernel32 = LoadLibraryA ("kernel32.dll")

If kernel32 webcam_FreeLibrary = GetProcAddress (kernel32, "FreeLibrary")

Const WM_CAP_START = WM_USER

Const WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2
Const WM_CAP_SET_CALLBACK_STATUS = WM_CAP_START + 3
Const WM_CAP_SET_CALLBACK_YIELD = WM_CAP_START + 4
Const WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5
Const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6
Const WM_CAP_SET_CALLBACK_WAVESTREAM = WM_CAP_START + 7

Const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10
Const WM_CAP_DRIVER_GET_CAPS = WM_CAP_START + 14

Const WM_CAP_DLG_VIDEOFORMAT = WM_CAP_START + 41
Const WM_CAP_DLG_VIDEOSOURCE = WM_CAP_START + 42
Const WM_CAP_DLG_VIDEODISPLAY = WM_CAP_START + 43

Const WM_CAP_GET_VIDEOFORMAT = WM_CAP_START +44
Const WM_CAP_SET_VIDEOFORMAT = WM_CAP_START +45

Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50
Const WM_CAP_SET_OVERLAY = WM_CAP_START + 51
Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
Const WM_CAP_SET_SCALE = WM_CAP_START +53
Const WM_CAP_GET_STATUS = WM_CAP_START + 54

Const WM_CAP_SET_CALLBACK_CAPCONTROL = WM_CAP_START + 85
'Webcam Globals-----------------------------------------------------------------------------------------------'
'Webcam Type--------------------------------------------------------------------------------------------------'

Type TWebcam
 Field  Handle:Int
 Field  DeviceID:Int
 Field  FrameRate:Int
 Field  LibHandle:Int
 Global Width:Int
 Global Height:Int
 Global PMWidth:Int
 Global PMHeight:Int
 Global Image:TPixmap
 Field webcam_CreateCaptureWindow:Int (WName:Byte Ptr, Style:Int, X:Int, y:Int, w:Int, h:Int, ParentWnd:Int, ID:Int) "Win32"
 Method New ()
  Image = CreatePixmap(16,16,PF_RGB888)
  FrameRate = 15
  Handle = Null
  DeviceID = -1
  'Get the library handle
  LibHandle = LoadLibraryA("AVICAP32.DLL")
  If Not LibHandle Then Throw "Can't load AVICAP32.DLL"
  'Get the Adress of the capCreateCaptureWindow function
  webcam_CreateCaptureWindow = GetProcAddress( LibHandle, "capCreateCaptureWindowA" )
  'If Not webcam_CreateCaptureWindow Then Throw "Can't get adress of capCreateCaptureWindowA"         
 End Method
 Method Delete ()
  DeInit()
  webcam_FreeLibrary(LibHandle)
  Image = Null
 End Method   
 Method Init:Int (WHnd:Int, x:Int, y:Int, w:Int, h:Int)
  Local res:Int = False
  If (Handle <> Null) DeInit()
   DeviceID = -1
   Handle = Webcam_CreateCaptureWindow (Null, WS_VISIBLE+WS_CHILD, x, y, w, h, WHnd, 1)
   If (Handle <> Null)
   res = ConnectDevice()
   If (res) EnablePreviewMode(True)
   If (res) SetFrames(Framerate)         
  EndIf
  Return (res)
 End Method
 Method DeInit ()
  If (handle <> Null)
   DestroyWindow (Handle)
   Handle = Null         
  End If      
  DeviceID = -1
 End Method
 Method ConnectDevice:Int ()
  If (deviceID = -1)
   For Local i:Int = 0 To 9
    Local res:Int = Webcam_SendMessage(Handle, WM_CAP_DRIVER_CONNECT, i, 0)
    If (res <> 0)
     DeviceID = i
     Exit
    EndIf
   Next
  EndIf
  Return (DeviceID <> -1)      
 End Method
 Method EnablePreviewMode (enable:Int)
  If (DeviceID <> -1)
   Webcam_SendMessage (Handle, WM_CAP_SET_PREVIEW, enable, 0)
  End If      
 End Method
 Method EnableOverlayMode (enable:Int)
  If (DeviceID <> -1)
   Webcam_SendMessage (Handle, WM_CAP_SET_OVERLAY, enable, 0)
  End If      
 End Method
 Method SetFrames (XFrames:Int)
  FrameRate = XFrames
  If (DeviceID <> -1)
   Webcam_SendMessage (Handle, WM_CAP_SET_PREVIEWRATE, FrameRate, 0)
  End If      
 End Method
 Method ShowSettings (nr:Int) 'use 0 to 2   
  If (DeviceID <> -1)
   Webcam_SendMessage (Handle, WM_CAP_DLG_VIDEOFORMAT+nr, 0, 0)
  End If      
 End Method
 Method SetScale (DoScale:Int)
  If (DeviceID <> -1)
   Webcam_SendMessage (Handle, WM_CAP_SET_SCALE, DoScale, 0)
  End If      
 End Method
 Method SetFrameCallbackRoutine ()
  If (DeviceID <> -1)
   Local Routine:Byte Ptr
   Routine = FrameCallback
   Webcam_SendMessage (Handle, WM_CAP_SET_CALLBACK_FRAME, 0, Int(Routine))
  End If      
 End Method
 Method SetPMSize (w:Int, h:Int)
  PMWidth = w
  PMHeight = h
 End Method
 Method SetSize (w:Int, h:Int)
  Local res:Int = False
  If (DeviceID <> -1)         
   'Get The size of the structure first
   Local size:Int = Webcam_SendMessage (Handle, WM_CAP_GET_VIDEOFORMAT, 0, Null)
   If (size > 0)
    'Get The Space for the Data
    Local Bank:TBank = CreateBank(size)
    Webcam_SendMessage (Handle, WM_CAP_GET_VIDEOFORMAT, size, Int(BankBuf(Bank)))
    'Manipulate the buffer
    Width  = PeekInt(Bank, 4)
    Height = PeekInt(Bank, 8)
    DebugLog "Width: "+Width+" Height: "+Height
    PokeInt(Bank, 4, w)
    PokeInt(Bank, 8, h)
    'Write it back
    res = Webcam_SendMessage (Handle, WM_CAP_SET_VIDEOFORMAT, size, Int(BankBuf(Bank)))
    If (res)
     Width  = w
     Height = h
    End If
    Bank = Null
   End If         
  End If      
  Return(res)
 End Method   

 Function FrameCallback (lwnd:Int, lpVHdr:Byte Ptr)
  Local VideoHeader:TBank = CreateStaticBank(lpVHdr, 40)
  Local VideoMemoryAdress:Byte Ptr = Byte Ptr(PeekInt(VideoHeader, 0))
  Local dwBytesUsed:Int = PeekInt(VideoHeader, 4)
  If (dwBytesUsed = (Width*Height*2)) ' 640 * 480 * 16bit YUY2, for 24bit RGB use: W*H*3 instead of W*H*2
   InterLoc = CreateStaticBank(VideoMemoryAdress,640*480*2)
   x=0
   y=0
   l=0
   n=0
   For n = 0 To 614400-1 Step 4
    Y0:Float = PeekByte(InterLoc,n+0)
    U0:Float = PeekByte(InterLoc,n+1)
    Y1:Float = PeekByte(InterLoc,n+2)
    V0:Float = PeekByte(InterLoc,n+3)

    B0 = 1.164*(Y0-16) + 2.018*(U0-128)
    G0 = 1.164*(Y0-16) - 0.813*(V0-128) - 0.391*(U0-128)
    R0 = 1.164*(Y0-16) + 1.596*(V0-128)
    B1 = 1.164*(Y1-16) + 2.018*(U0-128)
    G1 = 1.164*(Y1-16) - 0.813*(V0-128) - 0.391*(U0-128)
    R1 = 1.164*(Y1-16) + 1.596*(V0-128)

Rem
    R0 = R0-200
    G0 = G0-200
    B0 = B0-200
    R1 = R1-200
    G1 = G1-200
    B1 = B1-200
EndRem
    R0=clamp(R0,0,255)
    G0=clamp(G0,0,255)
    B0=clamp(B0,0,255)
    R1=clamp(R1,0,255)
    G1=clamp(G1,0,255)
    B1=clamp(B1,0,255)


    PokeByte(TempLoc,l,B0)
    PokeByte(TempLoc,l+1,G0)
    PokeByte(TempLoc,l+2,R0)
    PokeByte(TempLoc,l+3,B1)
    PokeByte(TempLoc,l+4,G1)
    PokeByte(TempLoc,l+5,R1)
    l=l+6
   Next
   Local TempMap:TPixmap = CreateStaticPixmap (BankBuf(TempLoc), Width, Height, Width*3,PF_BGR888) 'Use VideomemoryAdress instead of TempLoc if RGB is the format
   'WebCamImage = LoadImage(TempMap)
   Image = TempMap
   If (PMWidth <> Width) Or (PMHeight <> Height)
    Image = ResizePixmap (Image, PMWidth,PMHeight)
   EndIf
  Else
   DebugLog "Wrong Picture Size. Expected "+Width+"/"+Height   
  End If
 End Function
End Type
'Webcam Type--------------------------------------------------------------------------------------------------'
'##############################################  WEBCAM  #####################################################'

'##############################################  MAIN  #######################################################'

Graphics 800,600',0'32,60
Local whnd = Webcam_GetActiveWindow()
Local Webcam:TWebcam = New TWebcam
Local rot:Int

If (Webcam.Init(whnd, 0,0,640,480))
 Webcam.ShowSettings(0)
 Webcam.SetFrames(30)
 Webcam.SetSize(640,480)
 Webcam.SetPMSize(640,480) '800,600
 Webcam.SetScale(False)
 Webcam.EnablePreviewMode(False)
 Webcam.EnableOverlayMode(False)
 Webcam.SetFrameCallbackRoutine()
 While Not KeyHit(key_escape)
  Cls
  DrawPixmap (Webcam.Image, 0, 0)
  SetColor 255,255,255
  DrawText "Memory usage:"+GCMemAlloced(), 0,580
  Flip
  Wend
Else
 RuntimeError ("Cant Initialize Webcam")   
End If
EndGraphics()
'##############################################  MAIN  #######################################################'


'##############################################  FUNCTIONS  ##################################################'
Rem
bbdoc: Numeric 'clamp' builtin function
returns: Clamps the number within two numerical extents.
keyword: "Clamp"
End Rem

Function Clamp:Float(value:Float,minvalue:Float=0.0,maxvalue:Float=1.0)
   value=Max(value,minvalue)
   value=Min(value,maxvalue)
   Return value
EndFunction

'##############################################  FUNCTIONS  ##################################################'


My Pc:
MSI 790FX-GD70, Phenom X4 955BE, 2x2GB DDR3-1600, 8800GTS-512, WD 640GB + Samsung 160GB, Scythe Kaze Master
  • Zuletzt bearbeitet von naibaf7 am So, Okt 04, 2009 12:29, insgesamt 2-mal bearbeitet

DAK

BeitragSo, Okt 04, 2009 1:39
Antworten mit Zitat
Benutzer-Profile anzeigen
extrem geile sache! damit lässt sich einiges anfangen...

hat nur ein problem: solange die settings offen sind, geht alles gut, nur sobald ich sie zu mache (ok/abbrechen is egal, welches davon) verschwindet das bild. unten rennt ein counter rauf und sobald er oben is springt er wieder runter, aber das bild bleibt schwarz...
Gewinner der 6. und der 68. BlitzCodeCompo
 

naibaf7

BeitragSo, Okt 04, 2009 2:09
Antworten mit Zitat
Benutzer-Profile anzeigen
kannst du demfall mal schnell die Settings posten?
so wie der code "as is" ist, geht der nur für 640x480 YUY2 bei 614400 bytes per frame... und auch das natürlich ein wenig stockend... es ist ne schwerwiegende sache, all diese bytes umzurechnen in jedem frame, daher nur bedingt realtime tauglich, reicht aber für meine sache
das bild am anfang ist nur vorschau (d.h. taugt nicht für das endgültige resultat, k.a. warum)
der counter ist übrigens GCMemAlloced().... also falls man den quelltext ansieht sieht man das...
My Pc:
MSI 790FX-GD70, Phenom X4 955BE, 2x2GB DDR3-1600, 8800GTS-512, WD 640GB + Samsung 160GB, Scythe Kaze Master

hamZta

Administrator

BeitragSo, Okt 04, 2009 11:22
Antworten mit Zitat
Benutzer-Profile anzeigen
Und wir lesen gleich nochmal den Thread Wie poste ich richtig?

hamZta
Blog.
 

naibaf7

BeitragSo, Okt 04, 2009 11:30
Antworten mit Zitat
Benutzer-Profile anzeigen
Shocked was ist denn daran nicht ok?
Der Titel sagt doch genug, Code ist da, es ist zwar eine Grafikspielerei aber n Screenshot kann ich ja schlecht machen (da webcam bild)... Beispiel direkt im MAIN des codes, braucht keine externen ressourcen, den Titel kennzeichne ich noch mit [WIN] (sorry) und ich finde der Code ist ordentlich formatiert Very Happy
My Pc:
MSI 790FX-GD70, Phenom X4 955BE, 2x2GB DDR3-1600, 8800GTS-512, WD 640GB + Samsung 160GB, Scythe Kaze Master
 

E. Urbach

ehemals "Basicprogger"

BeitragSo, Okt 04, 2009 14:01
Antworten mit Zitat
Benutzer-Profile anzeigen
Naives Copy & Paste & Run hat bei mir zu Windows Bluescreen und komplettem PC-Absturz geführt *g*
The box said, "Requires Windows XP or better", so I installed Ubuntu | Linux is NOT Windows
Flua :: Profiler für BB und BMax :: Partikel-Engine für BMax :: Lyphia-Projekt Quellcode (BMax) :: Automatische Parallelisierung :: Meine Musik
 

naibaf7

BeitragSo, Okt 04, 2009 14:34
Antworten mit Zitat
Benutzer-Profile anzeigen
@Basicprogger:
nicht dein ernst?
also ich hab hier zuhause auf 3 PCs geteset, der code funzt definitiv...
My Pc:
MSI 790FX-GD70, Phenom X4 955BE, 2x2GB DDR3-1600, 8800GTS-512, WD 640GB + Samsung 160GB, Scythe Kaze Master
 

E. Urbach

ehemals "Basicprogger"

BeitragMo, Okt 05, 2009 16:26
Antworten mit Zitat
Benutzer-Profile anzeigen
Doch, das war ernst gemeint.
Habe eine handelsübliche Webcam, aber das ist ja vollkommen irrelevant. Es geht einfach darum, dass der Code anscheinend nicht besonders "sicher" ist. Liegt evtl. an den API-Aufrufen, habe mir den Code allerdings nicht genauer angesehen. Noch einmal ausführen möchte ich ihn jedenfalls nicht.
The box said, "Requires Windows XP or better", so I installed Ubuntu | Linux is NOT Windows
Flua :: Profiler für BB und BMax :: Partikel-Engine für BMax :: Lyphia-Projekt Quellcode (BMax) :: Automatische Parallelisierung :: Meine Musik
 

naibaf7

BeitragMo, Okt 05, 2009 19:22
Antworten mit Zitat
Benutzer-Profile anzeigen
das Problem könnte ein nicht richtig installierter Webcam-Treiber sein, oder nicht die neuste Version von Blitzmax verwendet
die API aufrufe gehen nicht mal tief ins system...
My Pc:
MSI 790FX-GD70, Phenom X4 955BE, 2x2GB DDR3-1600, 8800GTS-512, WD 640GB + Samsung 160GB, Scythe Kaze Master

DAK

BeitragMo, Okt 05, 2009 23:06
Antworten mit Zitat
Benutzer-Profile anzeigen
ich hab diese einstellungen probiert:

640x480, I420 - 460800 Bytes / RGB 24 - 921600 Bytes

haben beide nicht funktioniert.

das intressante is, dass das ganze vorm schließen des einstellungsfensters ohne probs geht...
Gewinner der 6. und der 68. BlitzCodeCompo
 

naibaf7

BeitragDi, Okt 06, 2009 13:28
Antworten mit Zitat
Benutzer-Profile anzeigen
@DAK
darum hab ich auch geschrieben für YUY2... nicht RGB oder I420

Code: [AUSKLAPPEN]


'##############################################  WEBCAM  #####################################################'
'Webcam Globals-----------------------------------------------------------------------------------------------'
Global webcam_GetActiveWindow:Int () "Win32"
Global webcam_SendMessage( hWnd,MSG,wParam,lParam) "Win32"
Global webcam_FreeLibrary ( hnd:Int ) "Win32"
Global TempLoc = CreateBank(921600)

user32 = LoadLibraryA ("user32.dll")

If user32 webcam_GetActiveWindow = GetProcAddress (user32, "GetActiveWindow")
If user32 webcam_SendMessage = GetProcAddress (user32, "SendMessageA")

kernel32 = LoadLibraryA ("kernel32.dll")

If kernel32 webcam_FreeLibrary = GetProcAddress (kernel32, "FreeLibrary")

Const WM_CAP_START = WM_USER

Const WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2
Const WM_CAP_SET_CALLBACK_STATUS = WM_CAP_START + 3
Const WM_CAP_SET_CALLBACK_YIELD = WM_CAP_START + 4
Const WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5
Const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6
Const WM_CAP_SET_CALLBACK_WAVESTREAM = WM_CAP_START + 7

Const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10
Const WM_CAP_DRIVER_GET_CAPS = WM_CAP_START + 14

Const WM_CAP_DLG_VIDEOFORMAT = WM_CAP_START + 41
Const WM_CAP_DLG_VIDEOSOURCE = WM_CAP_START + 42
Const WM_CAP_DLG_VIDEODISPLAY = WM_CAP_START + 43

Const WM_CAP_GET_VIDEOFORMAT = WM_CAP_START +44
Const WM_CAP_SET_VIDEOFORMAT = WM_CAP_START +45

Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50
Const WM_CAP_SET_OVERLAY = WM_CAP_START + 51
Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
Const WM_CAP_SET_SCALE = WM_CAP_START +53
Const WM_CAP_GET_STATUS = WM_CAP_START + 54

Const WM_CAP_SET_CALLBACK_CAPCONTROL = WM_CAP_START + 85

Global FormatMode = 3
'Webcam Globals-----------------------------------------------------------------------------------------------'
'Webcam Type--------------------------------------------------------------------------------------------------'

Type TWebcam
 Field  Handle:Int
 Field  DeviceID:Int
 Field  FrameRate:Int
 Field  LibHandle:Int
 Global Width:Int
 Global Height:Int
 Global PMWidth:Int
 Global PMHeight:Int
 Global Image:TPixmap
 Field webcam_CreateCaptureWindow:Int (WName:Byte Ptr, Style:Int, X:Int, y:Int, w:Int, h:Int, ParentWnd:Int, ID:Int) "Win32"
 Method New ()
  Image = CreatePixmap(16,16,PF_RGB888)
  FrameRate = 15
  Handle = Null
  DeviceID = -1
  'Get the library handle
  LibHandle = LoadLibraryA("AVICAP32.DLL")
  If Not LibHandle Then Throw "Can't load AVICAP32.DLL"
  'Get the Adress of the capCreateCaptureWindow function
  webcam_CreateCaptureWindow = GetProcAddress( LibHandle, "capCreateCaptureWindowA" )
  'If Not webcam_CreateCaptureWindow Then Throw "Can't get adress of capCreateCaptureWindowA"         
 End Method
 Method Delete ()
  DeInit()
  webcam_FreeLibrary(LibHandle)
  Image = Null
 End Method   
 Method Init:Int (WHnd:Int, x:Int, y:Int, w:Int, h:Int)
  Local res:Int = False
  If (Handle <> Null) DeInit()
   DeviceID = -1
   Handle = Webcam_CreateCaptureWindow (Null, WS_VISIBLE+WS_CHILD, x, y, w, h, WHnd, 1)
   If (Handle <> Null)
   res = ConnectDevice()
   If (res) EnablePreviewMode(True)
   If (res) SetFrames(Framerate)         
  EndIf
  Return (res)
 End Method
 Method DeInit ()
  If (handle <> Null)
   DestroyWindow (Handle)
   Handle = Null         
  End If      
  DeviceID = -1
 End Method
 Method ConnectDevice:Int ()
  If (deviceID = -1)
   For Local i:Int = 0 To 9
    Local res:Int = Webcam_SendMessage(Handle, WM_CAP_DRIVER_CONNECT, i, 0)
    If (res <> 0)
     DeviceID = i
     Exit
    EndIf
   Next
  EndIf
  Return (DeviceID <> -1)      
 End Method
 Method EnablePreviewMode (enable:Int)
  If (DeviceID <> -1)
   Webcam_SendMessage (Handle, WM_CAP_SET_PREVIEW, enable, 0)
  End If      
 End Method
 Method EnableOverlayMode (enable:Int)
  If (DeviceID <> -1)
   Webcam_SendMessage (Handle, WM_CAP_SET_OVERLAY, enable, 0)
  End If      
 End Method
 Method SetFrames (XFrames:Int)
  FrameRate = XFrames
  If (DeviceID <> -1)
   Webcam_SendMessage (Handle, WM_CAP_SET_PREVIEWRATE, FrameRate, 0)
  End If      
 End Method
 Method ShowSettings (nr:Int) 'use 0 to 2   
  If (DeviceID <> -1)
   Webcam_SendMessage (Handle, WM_CAP_DLG_VIDEOFORMAT+nr, 0, 0)
  End If      
 End Method
 Method SetScale (DoScale:Int)
  If (DeviceID <> -1)
   Webcam_SendMessage (Handle, WM_CAP_SET_SCALE, DoScale, 0)
  End If      
 End Method
 Method SetFrameCallbackRoutine ()
  If (DeviceID <> -1)
   Local Routine:Byte Ptr
   Routine = FrameCallback
   Webcam_SendMessage (Handle, WM_CAP_SET_CALLBACK_FRAME, 0, Int(Routine))
  End If      
 End Method
 Method SetPMSize (w:Int, h:Int)
  PMWidth = w
  PMHeight = h
 End Method
 Method SetSize (w:Int, h:Int)
  Local res:Int = False
  If (DeviceID <> -1)         
   'Get The size of the structure first
   Local size:Int = Webcam_SendMessage (Handle, WM_CAP_GET_VIDEOFORMAT, 0, Null)
   If (size > 0)
    'Get The Space for the Data
    Local Bank:TBank = CreateBank(size)
    Webcam_SendMessage (Handle, WM_CAP_GET_VIDEOFORMAT, size, Int(BankBuf(Bank)))
    'Manipulate the buffer
    Width  = PeekInt(Bank, 4)
    Height = PeekInt(Bank, 8)
    DebugLog "Width: "+Width+" Height: "+Height
    PokeInt(Bank, 4, w)
    PokeInt(Bank, 8, h)
    'Write it back
    res = Webcam_SendMessage (Handle, WM_CAP_SET_VIDEOFORMAT, size, Int(BankBuf(Bank)))
    If (res)
     Width  = w
     Height = h
    End If
    Bank = Null
   End If         
  End If      
  Return(res)
 End Method   

 Function FrameCallback (lwnd:Int, lpVHdr:Byte Ptr)
  Local VideoHeader:TBank = CreateStaticBank(lpVHdr, 40)
  Local VideoMemoryAdress:Byte Ptr = Byte Ptr(PeekInt(VideoHeader, 0))
  Local dwBytesUsed:Int = PeekInt(VideoHeader, 4)

  If (dwBytesUsed = (Width*Height*FormatMode)) ' 640 * 480 * 16bit YUY2, for 24bit RGB use: W*H*3 instead of W*H*2
   InterLoc = CreateStaticBank(VideoMemoryAdress,640*480*FormatMode)
   x=0
   y=0
   l=0
   n=0
   If FormatMode = 2 Then
    For n = 0 To (640*480*FormatMode)-1 Step 4
     Y0:Float = PeekByte(InterLoc,n+0)
     U0:Float = PeekByte(InterLoc,n+1)
     Y1:Float = PeekByte(InterLoc,n+2)
     V0:Float = PeekByte(InterLoc,n+3)

     B0 = 1.164*(Y0-16) + 2.018*(U0-128)
     G0 = 1.164*(Y0-16) - 0.813*(V0-128) - 0.391*(U0-128)
     R0 = 1.164*(Y0-16) + 1.596*(V0-128)
     B1 = 1.164*(Y1-16) + 2.018*(U0-128)
     G1 = 1.164*(Y1-16) - 0.813*(V0-128) - 0.391*(U0-128)
     R1 = 1.164*(Y1-16) + 1.596*(V0-128)

Rem
     R0 = R0-100
     G0 = G0-200
     B0 = B0-200
     R1 = R1-100
     G1 = G1-200
     B1 = B1-200
EndRem

     R0=clamp(R0,0,255)
     G0=clamp(G0,0,255)
     B0=clamp(B0,0,255)
     R1=clamp(R1,0,255)
     G1=clamp(G1,0,255)
     B1=clamp(B1,0,255)
     PokeByte(TempLoc,l,B0)
     PokeByte(TempLoc,l+1,G0)
     PokeByte(TempLoc,l+2,R0)
     PokeByte(TempLoc,l+3,B1)
     PokeByte(TempLoc,l+4,G1)
     PokeByte(TempLoc,l+5,R1)
     l=l+6
    Next
   EndIf
   If FormatMode = 3 Then
    For n = 0 To (640*480*FormatMode)-1 Step 3
     B0 = PeekByte(InterLoc,n+0)
     G0 = PeekByte(InterLoc,n+1)
     R0 = PeekByte(InterLoc,n+2)
Rem
     R0 = R0-100
     G0 = G0-200
     B0 = B0-200
EndRem
     PokeByte(TempLoc,l,B0)
     PokeByte(TempLoc,l+1,G0)
     PokeByte(TempLoc,l+2,R0)
     l=l+3
    Next
   EndIf
   Local TempMap:TPixmap = CreateStaticPixmap (BankBuf(TempLoc), Width, Height, Width*3,PF_BGR888) 'Use VideomemoryAdress instead of TempLoc if RGB is the format
   'WebCamImage = LoadImage(TempMap)
   If FormatMode = 2 Then
    Image = TempMap
   EndIf
   If FormatMode = 3 Then
    Image = YFlipPixmap(TempMap)
   EndIf
   If (PMWidth <> Width) Or (PMHeight <> Height)
    Image = ResizePixmap (Image, PMWidth,PMHeight)
   EndIf
  Else
   DebugLog "Wrong Picture Size. Expected "+Width+"/"+Height   
  End If
 End Function
End Type
'Webcam Type--------------------------------------------------------------------------------------------------'
'##############################################  WEBCAM  #####################################################'

'##############################################  MAIN  #######################################################'

Graphics 800,600',0'32,60
Local whnd = Webcam_GetActiveWindow()
Local Webcam:TWebcam = New TWebcam
Local rot:Int

If (Webcam.Init(whnd, 0,0,640,480))
 Webcam.ShowSettings(0)
 Webcam.SetFrames(30)
 Webcam.SetSize(640,480)
 Webcam.SetPMSize(640,480) '800,600
 Webcam.SetScale(False)
 Webcam.EnablePreviewMode(False)
 Webcam.EnableOverlayMode(False)
 Webcam.SetFrameCallbackRoutine()
 While Not KeyHit(key_escape)
  Cls
  DrawPixmap (Webcam.Image, 0, 0)
  SetColor 255,255,255
  DrawText "Memory usage:"+GCMemAlloced(), 0,580
  Flip
  Wend
Else
 RuntimeError ("Cant Initialize Webcam")   
End If
EndGraphics()
'##############################################  MAIN  #######################################################'


'##############################################  FUNCTIONS  ##################################################'
Rem
bbdoc: Numeric 'clamp' builtin function
returns: Clamps the number within two numerical extents.
keyword: "Clamp"
End Rem

Function Clamp:Float(value:Float,minvalue:Float=0.0,maxvalue:Float=1.0)
   value=Max(value,minvalue)
   value=Min(value,maxvalue)
   Return value
EndFunction

'##############################################  FUNCTIONS  ##################################################'




das ist der code für RGB und YUY2...
FormatMode = 3 --> RGB und FormatMode=2 --> YUY2... muss man halt je nach dem einstellen... Die Globale FormatMode findet sich im übrigen am ende des Abschnittes "Webcam Globals"
My Pc:
MSI 790FX-GD70, Phenom X4 955BE, 2x2GB DDR3-1600, 8800GTS-512, WD 640GB + Samsung 160GB, Scythe Kaze Master

DAK

BeitragDi, Okt 06, 2009 13:55
Antworten mit Zitat
Benutzer-Profile anzeigen
hey, funktioniert! danke!
Gewinner der 6. und der 68. BlitzCodeCompo
 

naibaf7

BeitragDi, Okt 06, 2009 13:57
Antworten mit Zitat
Benutzer-Profile anzeigen
hab ich doch gesagt^^

naja ist halt so ne sache mit den vielen bildformate, ich brauchte nen halben tag um herauszufinden wie man am elegantesten die verschiedenen bildformate umrechnet...

ist zwar so ein bisschen umständlich, aber man hat immerhin zugriff auf alle farblayer des bildes und kann diese aktiv nach mustern vorbearbeiten (z.b. für Bilderkennung praktisch)
My Pc:
MSI 790FX-GD70, Phenom X4 955BE, 2x2GB DDR3-1600, 8800GTS-512, WD 640GB + Samsung 160GB, Scythe Kaze Master

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group