使用 aforge.net 从网络摄像头或 IP 摄像头(带 App 的智能手机)拍照





3.00/5 (3投票s)
使用 VB.NET 从网络摄像头或 IP 摄像头拍照
引言
本文以非常简单的方式展示了如何使用 Aforge.net 库,在 Visual Basic .NET 中从网络摄像头或使用 Android 手机作为 IP 摄像头拍照。
背景
我需要使用网络摄像头为身份证拍照,但是,在我们使用的一台机器上,我们无法安装网络摄像头,并且想到了放置一个 IP 摄像头。经过一些研究,我发现可以使用带 APP 的 Android 手机作为 IP 摄像头,所以我决定制作同一个程序来同时从网络摄像头和 IP 摄像头捕获,Aforge.video 库让事情变得容易多了。
另一个具体需求是照片需要具有一定的尺寸才能用于卡片,因此我包含了裁剪选项以保存所需尺寸的图像。
首先,我想感谢以下互联网用户、文章和库,它们给了我很多关于如何完成这项工作的想法。
- 使用 Visual Basic .NET 通过 Android 摄像头(App Ip Webcam)进行捕获
https://www.youtube.com/watch?v=XXulkoWLMxY&t=198s - 使用 Visual Basic (VB.NET) 通过 Aforge 将网络摄像头转换为视频
http://visualbasictutoriales.blogspot.com/2015/03/webcam-video-aforge-con-visual-basic.html - 使用 VB.NET 进行图像裁剪和调整大小
https://codeproject.org.cn/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
将 C# 代码转换为 Visual Basic
- SharpDevelop 版本 : 4.4.1.9729-7196a277
https://github.com/icsharpcode/SharpDevelop
和
- Convert.Net
https://fishcodelib.com/Convert.htm
手机上使用的 App 是
- IP Webcam
https://play.google.com/store/apps/details?id=com.pas.webcam&hl=nl
分辨率为 640X480
Using the Code
这是程序的总览,您可以使用智能手机作为 IP 摄像头 ,使用网络摄像头
,或者加载图片文件
或退出
。
在“公共变量
”区域,您可以配置程序的行为。
#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
其中
GnGetVarsFromConfig
:此变量允许程序从 MultipleCameraPhoto.exe.config 读取所有配置变量,以便您方便地执行它。GnPathPhoto
:这是照片将要保存的路径。GnFilePhoto
:这是要保存的文件的默认名称。GnFileExtension
:这是您想要保存的文件类型(扩展名)。GnLogRequiresIp
:如果显示包含 IP 地址的TextBox
。GnIpCamera
:这是摄像头的 IP 地址(重要提示:您必须在 IP 地址末尾添加“/video
”,才能与 IPWebCam
APP 一起使用)。LogExitSave
:此变量让您知道程序是因保存按钮还是退出按钮结束的,这样可以将其用于调用它的程序以刷新照片。LogSave2File
:此变量决定是否使用文件对话框来查找路径并为其命名来保存照片。LogSaveDefault
:此变量决定是否使用变量GnPathPhoto
中给定的路径、变量GnFilePhoto
中给定的名称以及变量GnFileExtension
中给定的扩展名来保存照片。
注意:变量LogSave2File
优先于此变量,也就是说,如果LogSave2File
设置为true
,它将显示文件保存对话框,忽略此变量的功能。LogRealExit
:如果此变量为true
,程序将在执行结束时关闭,否则只会隐藏,以便调用它的程序可以在关闭前访问结果。GnLogNotExit
:如果此变量为true
,程序将不会退出,并且可以拍照,如果您想在完成程序之前拍几张照片,这会很有用。LogJustLoadFile
:此变量决定是否仅显示文件上传按钮,忽略摄像头按钮。LogLoadFileVisible
:此变量决定是否显示文件上传按钮。LogCropit
:如果变量为true
,则最终照片将被裁剪为变量IntCropWidth
和IntcropHeight
中指定的尺寸,此外,它还会显示一个带有指定尺寸的黄色框,用于裁剪图片框中心的图像。IntCropWidth
:要裁剪的框的宽度。IntcropHeight
:要裁剪的框的高度。
您可以将这些变量包含在您自己的配置文件中
<!-- 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 -->
如果您愿意,可以将此源代码用作 Visual Basic 类库,为此,您需要在项目中进行这些更改。
原始设置
将这些设置更改为
编译,这会产生一个错误
双击错误,编辑器会将您带到 Application Designer 文件,其中包含所有这些错误
编辑此文件并使其如下所示
'------------------------------------------------------------------------------
' <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
保存并重新编译。
这是将此程序用作 类
库的示例。
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
兴趣点
我没有尝试过真正的 IP 网络摄像头,但我认为它可能会起作用。
历史
- 版本 1.0