-
1Step 1
How to get a camera image into a program:
Open Microsoft Visual Studios. Under file click “New”. Then click “Project”, then click Windows Form Application” Title it “retrieve cam”.
Under Form1.vb[Design]
Add a textbox and name it “Head_ip” and add the Camera’s IP address under text of the textbox. Add a label above it and set the text to “camera IP”. Add a picture box and set its size to 640,480. Add a timer and set it to enable.You’ll also need to add the 2 imports to the top of the class form.
Imports System.IO
Imports System.Net
Every time the timer cycles the program will retrieve a snapshot from the camera and display it in the picturebox.
Below is how the Form1.vb[Design] should look:
Here's the complete code:
Imports System.IO
Imports System.NetPublic Class Form1
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
load_jpg()
End Sub
Public Sub load_jpg()
Try
Dim sourceURL As String = "http://" & Head_ip.Text & ":7777/snapshot.cgi"
Dim buffer As Byte() = New Byte(99999) {}
Dim read As Integer, total As Integer = 0
' create HTTP request
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(sourceURL), HttpWebRequest)
req.Credentials = New NetworkCredential("username", "password")
' get response
Dim resp As WebResponse = req.GetResponse()
' get response stream
Dim stream As Stream = resp.GetResponseStream()
' read data from stream
While (InlineAssignHelper(read, stream.Read(buffer, total, 1000))) <> 0
total += read
End While
' get bitmap
Dim bmp As Bitmap
bmp = DirectCast(Bitmap.FromStream(New MemoryStream(buffer, 0, total)), Bitmap)
PictureBox1.Image = bmp
Catch
End Try
End Sub
Private Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
target = value
Return value
End Function
End ClassWhen you run it you should get something like this:
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.