Results 1 to 6 of 6

Thread: Webcam mirror/flip VB.NET help please?

  1. #1
    Constant Bitrate
    Join Date
    Sep 2006
    Location
    Cornwall, England
    Posts
    161

    Webcam mirror/flip VB.NET help please?

    Hi,

    I want to add a reversing cam to my project. I have some sample VB.NET code that uses avicap32.dll to 'get' the webcam and then opens a preview window in a picturebox on a form. Using a few Sendmessage commands, the webcam image is displayed in the picturebox.

    Everything works nicely apart from I would like the image to be flipped/mirrored, as it were if you were using a rear view mirroe. Is there any commands you have to 'send' the webcam via avicap32, or is it more complicated than that?

    Thanks
    ViVE - Volkswagen In Van Entertainment:

    VoomPC 2, VIA C7 2GHz
    7" Lilliput
    120Gb Sata drive
    1GB RAM


    [||||||||||] 100% - Planning
    [||||||||||] 100% - Software coding
    [||||||||||] 100% - Built

    (Always tweaking!)

  2. #2
    Variable Bitrate
    Join Date
    Apr 2006
    Location
    Dallas, Texas
    Posts
    264
    What format is the video when you get it from the WebCam? You might be able to flip the X,Y coords of the image on-the-fly.

  3. #3
    Variable Bitrate
    Join Date
    Apr 2006
    Location
    Dallas, Texas
    Posts
    264
    http://www.codeproject.com/KB/audio-...ctShowNET.aspx

    Here's some code that allows you to grab the webcam information.

    Check out this article to see the image inversion..

    http://forums.microsoft.com/MSDN/Sho...13689&SiteID=1

  4. #4
    Constant Bitrate
    Join Date
    Sep 2006
    Location
    Cornwall, England
    Posts
    161
    Thanks for the reply, this is the code that I've just got working. It's basic, but it works, and I'll probably build on it...



    Imports System.Runtime.InteropServices


    Public Class Form1

    Const WM_CAP As Short = &H400S
    Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
    Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
    Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
    Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
    Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
    Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
    Const WS_CHILD As Integer = &H40000000
    Const WS_VISIBLE As Integer = &H10000000
    Const SWP_NOMOVE As Short = &H2S
    Const SWP_NOSIZE As Short = 1
    Const SWP_NOZORDER As Short = &H4S
    Const HWND_BOTTOM As Short = 1
    '--------------------------------------------------
    Const WM_CAP_CONNECT As Integer = 1034
    Const WM_CAP_DISCONNECT As Integer = 1035
    Const WM_CAP_GET_FRAME As Integer = 1084
    Const WM_CAP_COPY As Integer = 1054


    Private tempObj As IDataObject
    Private tempimg As System.Drawing.Bitmap
    Private mCapHwnd As Integer ' Handle of webcam
    Dim iDevice As Integer = 0 ' Current device ID


    Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
    <MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer

    Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer, _
    ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, _
    ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer

    Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean

    Declare Function capCreateCaptureWindowA Lib "avicap32.dll" _
    (ByVal lpszWindowName As String, ByVal dwStyle As Integer, _
    ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
    ByVal nHeight As Short, ByVal hWndParent As Integer, _
    ByVal nID As Integer) As Integer

    Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, _
    ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, _
    ByVal cbVer As Integer) As Boolean



    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

    Try
    ' setup a capture window
    mCapHwnd = capCreateCaptureWindowA("WebCap", 0, 0, 0, 320, 240, Me.Handle.ToInt32(), 0)

    ' connect to the capture device
    SendMessage(mCapHwnd, WM_CAP_CONNECT, iDevice, 0)
    SendMessage(mCapHwnd, WM_CAP_SET_PREVIEW, iDevice, 0)

    'Set the preview rate in milliseconds
    SendMessage(mCapHwnd, WM_CAP_SET_PREVIEWRATE, 40, 0)

    Catch ex As Exception
    DestroyWindow(mCapHwnd)
    End Try

    Timer1.Enabled = True
    End Sub


    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click

    ' Disconnect device
    SendMessage(mCapHwnd, WM_CAP_DISCONNECT, iDevice, 0)
    Timer1.Enabled = False
    End Sub


    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    DestroyWindow(mCapHwnd)
    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    Try

    ' get the next frame;
    SendMessage(mCapHwnd, WM_CAP_GET_FRAME, 0, 0)

    ' copy the frame to the clipboard
    SendMessage(mCapHwnd, WM_CAP_COPY, 0, 0)

    ' get from the clipboard
    tempObj = Clipboard.GetDataObject()
    tempimg = tempObj.GetData(System.Windows.Forms.DataFormats.B itmap)
    tempimg.RotateFlip(RotateFlipType.RotateNoneFlipX)

    PictureBox.Image = tempimg

    Catch ex As Exception
    End Try
    End Sub
    End Class



    All I do is copy a frame to the clipboard from the camera every time the timer ticks. I then copy and flip the image to a tempory image object and then put the image in the image box.

    Seems to work OK, do you think this is a good method?
    ViVE - Volkswagen In Van Entertainment:

    VoomPC 2, VIA C7 2GHz
    7" Lilliput
    120Gb Sata drive
    1GB RAM


    [||||||||||] 100% - Planning
    [||||||||||] 100% - Software coding
    [||||||||||] 100% - Built

    (Always tweaking!)

  5. #5
    Variable Bitrate
    Join Date
    Apr 2006
    Location
    Dallas, Texas
    Posts
    264
    Well, I am used to C# more so I'll try to translate. Basically you'll be doing the same thing, only you won't be using the clipboard since that can screw with other applications.

    The basic idea is to create a call back function (handler) so that every time an image is captured from the card, you can flip it.

    I started to try and convert it all to VB.NET from C#.NET but there is just too much I would need to define, instead I'll direct you to the C# code. I'm sure you can translate it as the languages are pretty similar.

    http://www.codeproject.com/KB/audio-...?display=Print

    If you need help with converting anything I should be able to do it for you. I'm just more confortable using C#.

  6. #6
    Constant Bitrate
    Join Date
    Sep 2006
    Location
    Cornwall, England
    Posts
    161
    Cheers. But I tried this and it keeps crashing.

    The code I have seems to work OK. Just need to buy another webcam and an extender cable and try it out!


    Cheers
    ViVE - Volkswagen In Van Entertainment:

    VoomPC 2, VIA C7 2GHz
    7" Lilliput
    120Gb Sata drive
    1GB RAM


    [||||||||||] 100% - Planning
    [||||||||||] 100% - Software coding
    [||||||||||] 100% - Built

    (Always tweaking!)

Similar Threads

  1. Logitech Orbit AF Webcam
    By hardcoreracing in forum Road Runner
    Replies: 2
    Last Post: 02-16-2008, 11:53 PM
  2. Experimenting with Dash Cam. Should I convert my webcam to infrared?
    By nobb in forum General Hardware Discussion
    Replies: 1
    Last Post: 02-16-2008, 09:56 PM
  3. Webcam Options
    By jschrauwen in forum Input Devices
    Replies: 11
    Last Post: 01-04-2008, 01:15 PM
  4. FAQ: How To Make An IR Webcam
    By rijndael in forum The FAQ Emporium
    Replies: 1
    Last Post: 09-03-2007, 02:35 PM
  5. VB.net and FMOD
    By bondbond55 in forum Software & Software Development
    Replies: 0
    Last Post: 03-12-2004, 11:56 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •