Take Photos from a Webcam or IP Camera (SmartPhone using an App) with aforge.net





3.00/5 (3 votes)
Take photos from a webcam or IP Camera in VB.NET
Introduction
This article shows in a very simple way how to take photos from a webcam or with an Android phone as IP Camera using the Aforge.net library, in Visual Basic .NET.
Background
I needed to take pictures for ID cards using a webcam, however, on a machine we were using, we could not install the webcam and the idea of placing an IP camera. Doing a little research, I discovered that you could use an Android phone with a APP as an IP camera, so I decided to make the same program capture both from a webcam and an IP camera, the Aforge.video library made things much easier.
Another specific need is that the photo had to have a certain size for the card, so I included the Crop option to save the image of the desired size.
First of all, I want to thank the following Internet users, articles and libraries who gave me many ideas on how to do this.
- Capturar Camara Android (App Ip Webcam) con Visual Basic .NET
https://www.youtube.com/watch?v=XXulkoWLMxY&t=198s - Webcam a Video (Aforge) con Visual Basic (VB.NET)
http://visualbasictutoriales.blogspot.com/2015/03/webcam-video-aforge-con-visual-basic.html - Image Cropping with Image Resizing Using VB.NET
https://www.codeproject.com/Articles/27061/Image-Cropping-with-Image-Resizing-Using-VB-NET - AFORGE.NET
http://www.aforgenet.com/framework/ - AForge.NET/Samples/Video/Player
https://github.com/andrewkirillov/AForge.NET/tree/master/Samples/Video/Player
To translate code from C# to Visual Basic:
- SharpDevelop Version : 4.4.1.9729-7196a277
https://github.com/icsharpcode/SharpDevelop
and:
- Convert.Net
https://fishcodelib.com/Convert.htm
The App used in the phone is:
- IP Webcam
https://play.google.com/store/apps/details?id=com.pas.webcam&hl=nl
In resolution 640X480
Using the Code
This is the general view of the program, you can use a smartphone as an IP Camera , use a Webcamera
or load a picture file
or Exit
.
In the region "Public Variables
", you can configure the behavior of the program.
#Region "Public Variables"
Public GnGetVarsFromConfig = True
Public GnPathPhoto As String = "C:\Work\Images\"
Public GnFilePhoto As String = "TryPhoto"
Public GnFileExtension As String = "Png"
Public GnLogRequiresIp As Boolean = True
Public GnIpCamera As String = "http://192.168.0.4:8080/video"
Public LogExitSave As Boolean = True
Public LogSave2File As Boolean = True
Public LogSaveDefault As Boolean = False
Public LogRealExit As Boolean = True
Public GnLogNotExit As Boolean = True
Public LogJustLoadFile As Boolean = False
Public LogLoadFileVisible As Boolean = True
Public LogCropit As Boolean = True
'Crop dimesions
Public IntCropWidth As Integer = 340
Public IntcropHeight As Integer = 454
#End Region
where:
GnGetVarsFromConfig
: This variable allows the program to read all the configuration variables from the MultipleCameraPhoto.exe.config, to execute it at your convenience.GnPathPhoto
: It is the path where the photograph will be saved.GnFilePhoto
: It is the default name of the file to save.GnFileExtension
: It is the type of file that you want to save (extension).GnLogRequiresIp
: If theTextBox
containing the IP address is displayed.GnIpCamera
: It is IP address of the camera (Important: you must add "/video
" at the end of the IP address, to work with the IPWebCam
APP).LogExitSave
: This variable lets you know if the program ended by the save button or exit button, this is to use it in the program that calls it to refresh the photo.LogSave2File
: This variable determines if the photograph is saved using the file dialog to find the path and give it a name.LogSaveDefault
: This variable determines if the photograph is saved with the path given in the variableGnPathPhoto
, the name given in the variableGnFilePhoto
and with the extension given in the variableGnFileExtension
.
Note: The variableLogSave2File
has a precedence over this variable, that is, ifLogSave2File
is set totrue
, it will display the file save dialog, ignoring the function of this variable.LogRealExit
: If this variable istrue
, the program will close at the end of the execution, otherwise it will only be hidden so that the program that calls it can access the results before closing it.GnLogNotExit
: If this variable istrue
, the program will not come out and will be available to take another picture, this is useful if you want to take several pictures before finishing the program.LogJustLoadFile
: This variable determines whether only the file upload button is shown, ignoring the camera buttons.LogLoadFileVisible
: This variable determines whether the file upload button is displayed.LogCropit
: If the variable is intrue
, the final photo is cropped to the dimensions specified in the variablesIntCropWidth
andIntcropHeight
, additionally, it will show a yellow box with the dimensions that are specified to cut the image in the center of the picture box.IntCropWidth
: Width of the box to be cut.IntcropHeight
: Height of the box to be cut.
You can include these variables in your own configuration file:
<!-- Public Variables Definition -->
<add key="GnPathPhoto" value="C:\Work\Images\"/>
<add key="GnFilePhoto" value="TryPhoto"/>
<add key="GnFileExtension" value="Png"/>
<add key="GnLogRequiresIp" value="True"/>
<add key="GnIpCamera" value="http://192.168.0.4:8080/video"/>
<add key="GnLogExitSave" value="True"/>
<add key="GnLogSave2File" value="True"/>
<add key="GnLogSaveDefault" value="False"/>
<add key="GnLogRealExit" value="True"/>
<add key="GnLogNotExit" value="True"/>
<add key="GnLogJustLoadFile" value="False"/>
<add key="GnLogLoadFileVisible" value="True"/>
<add key="GnLogCropit" value="False"/>
<!-- Crop dimesions-->
<add key="GnIntCropWidth" value="340"/>
<add key="GnIntcropHeight" value="454"/>
<!-- End Of Public Variables Definition -->
If you wish, you can use this source code as a Visual Basic class library, for this, you have to make these changes in the project.
Original settings:
Change these settings to:
Compile, this generates an error:
Double click on the error and the editor takes you to the Application Designer file with all these errors:
Edit this file and leave it as follows:
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
' Runtime Version:4.0.30319.42000
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
Option Strict On
Option Explicit On
Namespace My
'NOTE: This file is auto-generated; do not modify it directly. To make changes,
' or if you encounter build errors in this file, go to the Project Designer
' (go to Project Properties or double-click the My Project node in
' Solution Explorer), and make changes on the Application tab.
'
Partial Friend Class MyApplication
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
Public Sub New()
End Sub
End Class
End Namespace
Save it and recompile.
Here is an example of how to use this program as a class
Library.
Imports MultipleCameraPhoto
Public Class <YourClass>
.
.
#Region "Buttons"
Private Sub BtnTakePicture_Click(sender As Object, e As EventArgs) _
Handles BtnTakePicture.Click
Dim StrFileName As String = StrFotoIdOrder
'WHERE PicBoxPhoto is the picture box of your application
CapturaFoto(StrPathFotos, StrFileName, "Png", PicBoxPhoto)
End Sub
Private Sub BtnLoadPicture_Click(sender As Object, e As EventArgs) _
Handles BtnLoadPicture.Click
'WHERE PicBoxPhoto is the picture box of your application
FindLoadPictures(StrPathFotos, StrFotoIdOrder, ".Png", PicBoxPhoto)
End Sub
#End Region
#Region "Photos"
'***********************************************************
'* Photos *
'***********************************************************
Private Sub CapturePhoto(ByVal StrPathFileName As String, _
ByVal StrFileName As String, ByVal StrExtension As String, ByRef PictureBox As PictureBox)
Dim FrnCapturePhoto As New TakeAPicture
'Dar Parametros
FrnCapturePhoto.GnGetVarsFromConfig = False
FrnCapturePhoto.GnLogNotExit = False
FrnCapturePhoto.GnPathPhoto = StrPathFileName
FrnCapturePhoto.GnFilePhoto = StrFileName
FrnCapturePhoto.GnFileExtension = StrExtension
FrnCapturePhoto.GnLogRequiresIp = False
FrnCapturePhoto.GnIpCamera = configApp.GetValue("IpCamera", GetType(System.String))
FrnCapturePhoto.GnLogExitSave = False
FrnCapturePhoto.GnLogSave2File = False
FrnCapturePhoto.GnLogSaveDefault = True
FrnCapturePhoto.GnLogRealExit = False
FrnCapturePhoto.GnLogJustLoadFile = False
FrnCapturePhoto.GnLogLoadFileVisible = True
FrnCapturePhoto.ShowDialog()
If FrnCapturePhoto.GnLogExitSave Then
LoadPhoto(StrPathFileName, StrFileName + "." + StrExtension, PictureBox)
End If
End Sub
Private Sub FindLoadPictures(ByVal StrPathFileName As String, _
ByVal StrFileName As String, ByVal StrExtension As String, ByVal PictureBox As PictureBox)
Dim FrnCapturePhoto As New TakeAPicture
'Dar Parametros
FrnCapturePhoto.GnGetVarsFromConfig = False
FrnCapturePhoto.GnPathPhoto = StrPathFileName
FrnCapturePhoto.GnFilePhoto = StrFileName
FrnCapturePhoto.GnFileExtension = StrExtension
FrnCapturePhoto.GnLogRequiresIp = False
FrnCapturePhoto.GnIpCamera = configApp.GetValue("IpCamera", GetType(System.String))
FrnCapturePhoto.GnLogExitSave = False
FrnCapturePhoto.GnLogSave2File = False
FrnCapturePhoto.GnLogSaveDefault = True
FrnCapturePhoto.GnLogRealExit = False
FrnCapturePhoto.GnLogJustLoadFile = True
FrnCapturePhoto.GnLogLoadFileVisible = True
FrnCapturePhoto.ShowDialog()
If FrnCapturePhoto.GnLogExitSave Then
CargaFoto(StrPathFileName, StrFileName + "." + StrExtension, PictureBox)
End If
End Sub
Private Sub LoadPhoto(ByVal StrPathFileName As String, ByVal StrFileName As String, _
ByRef PictureBox As PictureBox)
Dim fs As System.IO.FileStream
Dim NewPic As New PictureBox
Try
If System.IO.File.Exists(StrPathFileName & StrFileName) Then
' Specify a valid picture file path on your computer.
fs = New System.IO.FileStream(StrPathFileName & StrFileName, _
IO.FileMode.Open, IO.FileAccess.Read)
NewPic.Image = System.Drawing.Image.FromStream(fs)
fs.Close()
Else
fs = New System.IO.FileStream(StrPathFileName & "NoPictureFound.png", _
IO.FileMode.Open, IO.FileAccess.Read)
NewPic.Image = System.Drawing.Image.FromStream(fs)
fs.Close()
End If
Catch ex As Exception
'MessageBox.Show("Error, LoadPhoto: " & ex.Message, _
"LoadPhoto: " & Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
PictureBox.Image = NewPic.Image
PictureBox.SizeMode = PictureBoxSizeMode.StretchImage
NewPic.Dispose()
NewPic = Nothing
End Sub
Points Of Interest
I didn't try a real IP Web cam, but I think it may work.
History
- Version 1.0