我是新来的论坛,除了我需要快速解答之外我们需要快速解答。我们的防御工作将在下周进行,我一直在寻找如何将代码应用到IP摄像机流到我们系统的日子......这个是为了我们的论文..它是如此困难它让我头疼..有代码..谢谢伙计们...这是来自frmWatch模块,你记录和保存和阅读相机..我也想添加一个对此的夜视......谢谢..:3
Imports System.Collections.Generic
`Imports System.ComponentModel`
`Imports System.Data`
`Imports System.Drawing`
`Imports System.Drawing.Imaging`
`Imports System.Text`
`Imports System.Windows.Forms`
`Imports System.Threading`
`Imports System.Data.SqlClient`
`Imports AForge`
`Imports AForge.Imaging`
`Imports AForge.Imaging.Filters`
`Imports AForge.Video`
`Imports AForge.Video.DirectShow`
`Imports AForge.Video.FFMPEG`
`Imports System.IO`
`Imports System.Net`
`Public Class frmWatch`
'list of video devices
Private videoDevices As FilterInfoCollection
' form for cameras' movement
'Private moveCamerasForm As MoveCamerasForm
' form to show detected objects
'Private detectedObjectsForm As DetectedObjectsForm
'' form to tune object detection filter
'Private tuneObjectFilterForm As TuneObjectFilterForm
'Private colorFilter As New ColorFiltering()
'Private grayFilter As Grayscale.CommonAlgorithms
' use two blob counters, so the could run in parallel in two threads
` Private blobCounter1 As New BlobCounter()`
` Private blobCounter2 As New BlobCounter()`
` Private camera1Acquired As AutoResetEvent = Nothing`
` Private camera2Acquired As AutoResetEvent = Nothing`
` Private trackingThread As Thread = Nothing`
'' object coordinates in both cameras
Private x1 As Single, y1 As Single, x2 As Single, y2 As Single
Dim videoWidth1 As Integer
Dim videoHeight1 As Integer
Dim videoWidth2 As Integer
Dim videoHeight2 As Integer
Private Sub frmTest_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
StopCameras()
CloseVideoWriters()
End Sub
Sub CloseVideoWriters()
videoWriter1.Close()
videoWriter2.Close()
End Sub
Private Sub frmTest_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
If My.Computer.FileSystem.GetDirectoryInfo("C:\VIDEOS\").Exists = False Then
My.Computer.FileSystem.CreateDirectory("C:\VIDEOS\")
End If
Try
' enumerate video devices
videoDevices = New FilterInfoCollection(FilterCategory.VideoInputDevice)
If videoDevices.Count = 0 Then
Throw New Exception()
End If
Dim i As Integer = 1, n As Integer = videoDevices.Count
While i <= n
Dim cameraName As String = (i & " : ") + videoDevices(i - 1).Name
camera1Combo.Items.Add(cameraName)
camera2Combo.Items.Add(cameraName)
i += 1
End While
' check cameras count
If videoDevices.Count = 1 Then
camera2Combo.Items.Clear()
camera2Combo.Items.Add("Only one camera found")
camera2Combo.SelectedIndex = 0
camera2Combo.Enabled = False
Else
camera2Combo.SelectedIndex = 1
End If
camera1Combo.SelectedIndex = 0
Catch
startButton.Enabled = False
camera1Combo.Items.Add("No cameras found")
camera2Combo.Items.Add("No cameras found")
camera1Combo.SelectedIndex = 0
camera2Combo.SelectedIndex = 0
camera1Combo.Enabled = False
camera2Combo.Enabled = False
End Try
End Sub
' On "Start" button click - start cameras
Dim videoPlaying As Boolean = False
Private Sub startButton_Click(sender As Object, e As EventArgs) Handles startButton.Click
If videoPlaying = False Then
videoPlaying = True
startButton.Image = picStop.Image
StartCameras()
Else
If recording = True Then
recording = False
Timer1.Enabled = False
picRecording.Visible = False
btnRecord.Image = picRecord.Image
videoWriter1.Close()
SaveVideo1()
If camera2Combo.Enabled = True Then
videoWriter2.Close()
SaveVideo2()
End If
End If
videoPlaying = False
startButton.Image = picPlay.Image
StopCameras()
End If
End Sub
' On "Stop" button click - stop cameras
'Private Sub stopButton_Click(sender As Object, e As EventArgs) Handles stopButton.Click
' StopCameras()
'End Sub
' Start cameras
Private Sub StartCameras()
' create first video source
Dim videoSource1 As New VideoCaptureDevice(videoDevices(camera1Combo.SelectedIndex).MonikerString)
videoSource1.DesiredFrameRate = Me.txtFrameRate.Text
VideoSourcePlayer1.VideoSource = videoSource1
VideoSourcePlayer1.Start()
' create second video source
If camera2Combo.Enabled = True Then
'System.Threading.Thread.Sleep(500)
Dim videoSource2 As New VideoCaptureDevice(videoDevices(camera2Combo.SelectedIndex).MonikerString)
videoSource2.DesiredFrameRate = Me.txtFrameRate.Text
VideoSourcePlayer2.VideoSource = videoSource2
VideoSourcePlayer2.Start()
End If
'camera1Acquired = New AutoResetEvent(False)
'camera2Acquired = New AutoResetEvent(False)
''start tracking thread
'trackingThread = New Thread(New ThreadStart(AddressOf TrackingThreadS))
'trackingThread.Start()
End Sub
' Stop cameras
Private Sub StopCameras()
VideoSourcePlayer1.SignalToStop()
VideoSourcePlayer2.SignalToStop()
VideoSourcePlayer1.WaitForStop()
VideoSourcePlayer2.WaitForStop()
End Sub
Private Sub videoSourcePlayer1_NewFrame(sender As Object, ByRef image As Bitmap) Handles VideoSourcePlayer1.NewFrame
videoWidth1 = image.Width
videoHeight1 = image.Height
RecordVideo1(image)
'If objectDetectionCheck.Checked Then
' Dim objectImage As Bitmap = colorFilter.Apply(image)
' ' lock image for further processing
' Dim objectData As BitmapData = objectImage.LockBits(New Rectangle(0, 0, image.Width, image.Height), ImageLockMode.[ReadOnly], image.PixelFormat)
' ' grayscaling
' Dim grayImage As UnmanagedImage = Grayscale.CommonAlgorithms.BT709.Apply(New UnmanagedImage(objectData))
' ' unlock image
' objectImage.UnlockBits(objectData)
' ' locate blobs
' blobCounter1.ProcessImage(grayImage)
' Dim rects As Rectangle() = blobCounter1.GetObjectsRectangles()
' If rects.Length > 0 Then
' Dim objectRect As Rectangle = rects(0)
' ' draw rectangle around derected object
' Dim g As Graphics = Graphics.FromImage(image)
' Using pen As New Pen(Color.FromArgb(160, 255, 160), 3)
' g.DrawRectangle(pen, objectRect)
' End Using
' g.Dispose()
' ' get object's center coordinates relative to image center
' SyncLock Me
' x1 = (objectRect.Left + objectRect.Right - objectImage.Width) \ 2
' y1 = (objectImage.Height - (objectRect.Top + objectRect.Bottom)) \ 2
' ' map to [-1, 1] range
' x1 /= (objectImage.Width \ 2)
' y1 /= (objectImage.Height \ 2)
' camera1Acquired.[Set]()
' End SyncLock
' End If
' If detectedObjectsForm IsNot Nothing Then
' detectedObjectsForm.UpdateObjectPicture(0, objectImage)
' End If
'End If
End Sub
'' Thread to track object
'Private Sub TrackingThreadS()
' Dim targetX As Single = 0
' Dim targetY As Single = 0
' While True
' camera1Acquired.WaitOne()
' camera2Acquired.WaitOne()
' SyncLock Me
' ' stop the thread if it was signaled
' If (x1 = -1) AndAlso (y1 = -1) AndAlso (x2 = -1) AndAlso (y2 = -1) Then
' Exit While
' End If
' ' get middle point
' targetX = (x1 + x2) / 2
' targetY = (y1 + y2) / 2
' End SyncLock
' 'If moveCamerasForm IsNot Nothing Then
' ' ' run motors for the specified amount of degrees
' ' moveCamerasForm.RunMotors(2 * targetX, -2 * targetY)
' 'End If
' End While
'End Sub
' received frame from the 2nd camera
Private Sub videoSourcePlayer2_NewFrame(sender As Object, ByRef image As Bitmap) Handles VideoSourcePlayer2.NewFrame
videoWidth2 = image.Width
videoHeight2 = image.Height
RecordVideo2(image)
'If objectDetectionCheck.Checked Then
' Dim objectImage As Bitmap = colorFilter.Apply(image)
' ' lock image for further processing
' Dim objectData As BitmapData = objectImage.LockBits(New Rectangle(0, 0, image.Width, image.Height), ImageLockMode.[ReadOnly], image.PixelFormat)
' ' grayscaling
' Dim grayImage As UnmanagedImage = Grayscale.CommonAlgorithms.BT709.Apply(New UnmanagedImage(objectData))
' ' unlock image
' objectImage.UnlockBits(objectData)
' ' locate blobs
' blobCounter2.ProcessImage(grayImage)
' Dim rects As Rectangle() = blobCounter2.GetObjectsRectangles()
' If rects.Length > 0 Then
' Dim objectRect As Rectangle = rects(0)
' ' draw rectangle around derected object
' Dim g As Graphics = Graphics.FromImage(image)
' Using pen As New Pen(Color.FromArgb(160, 255, 160), 3)
' g.DrawRectangle(pen, objectRect)
' End Using
' g.Dispose()
' ' get object's center coordinates relative to image center
' SyncLock Me
' x2 = (objectRect.Left + objectRect.Right - objectImage.Width) \ 2
' y2 = (objectImage.Height - (objectRect.Top + objectRect.Bottom)) \ 2
' ' map to [-1, 1] range
' x2 /= (objectImage.Width \ 2)
' y2 /= (objectImage.Height \ 2)
' camera2Acquired.[Set]()
' End SyncLock
' End If
' If detectedObjectsForm IsNot Nothing Then
' detectedObjectsForm.UpdateObjectPicture(1, objectImage)
' End If
'End If
End Sub
'Private Sub showDetectedObjectsButton_Click(sender As System.Object, e As System.EventArgs) Handles showDetectedObjectsButton.Click
' If detectedObjectsForm Is Nothing Then
' detectedObjectsForm = New DetectedObjectsForm()
' End If
' detectedObjectsForm.Show()
'End Sub
'Private Sub tuneObjectFilterButton_Click(sender As System.Object, e As System.EventArgs) Handles tuneObjectFilterButton.Click
' If tuneObjectFilterForm Is Nothing Then
' tuneObjectFilterForm = New TuneObjectFilterForm()
' tuneObjectFilterForm.OnFilterUpdate = New EventHandler(AddressOf tuneObjectFilterForm_OnFilterUpdate)
' tuneObjectFilterForm.RedRange = colorFilter.Red
' tuneObjectFilterForm.GreenRange = colorFilter.Green
' tuneObjectFilterForm.BlueRange = colorFilter.Blue
' End If
' tuneObjectFilterForm.Show()
'End Sub
' Object filter properties are updated
Private Sub tuneObjectFilterForm_OnFilterUpdate(sender As Object, eventArgs As EventArgs)
'colorFilter.Red = tuneObjectFilterForm.RedRange
'colorFilter.Green = tuneObjectFilterForm.GreenRange
'colorFilter.Blue = tuneObjectFilterForm.BlueRange
End Sub
Sub OpenWriterVideo1()
If camera1Combo.Enabled = True Then
videoWriter1.Open("C:\VIDEOS\CAMERA1.avi", videoWidth1, videoHeight1, 10, VideoCodec.MPEG4)
End If
End Sub
Sub OpenWriterVideo2()
If camera2Combo.Enabled = True Then
videoWriter2.Open("C:\VIDEOS\CAMERA2.avi", videoWidth2, videoHeight2, 10, VideoCodec.MPEG4)
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs)
Dim width As Integer = 320 * 2
Dim height As Integer = 240 * 2
' create instance of video writer
Dim writer As New AForge.Video.FFMPEG.VideoFileWriter()
' create new video file
writer.Open("C:\Jonathan\My Consultation Projects\Rymer\Resources\AVISample Videos\SAMPLE2.avi", width, height, 25, VideoCodec.MPEG4)
' create a bitmap to save into the video file
Dim image As New Bitmap(width, height, PixelFormat.Format24bppRgb)
' write 1000 video frames
For i As Integer = 0 To 999
image.SetPixel(i Mod width, i Mod height, Color.White)
writer.WriteVideoFrame(image)
Next
Dim codec As String = writer.Codec
writer.Close()
End Sub
Dim recordControl1 As Integer
Dim videoWriter1 As New AForge.Video.FFMPEG.VideoFileWriter()
Sub RecordVideo1(image As Bitmap)
If recording = True Then
' create a bitmap to save into the video file
'Dim image As New Bitmap(width, height, PixelFormat.Format24bppRgb)
' write 1000 video frames
'For i As Integer = 0 To 999
' image.SetPixel(i Mod width, i Mod height, Color.White)
videoWriter1.WriteVideoFrame(image)
'Next
recordControl1 += 1
If recordControl1 = modOper.recordLimit Then
videoWriter1.Close()
recordControl1 = 0
SaveVideo1()
OpenWriterVideo1()
End If
'System.IO.File.Copy(Application.StartupPath & "\SAMPLE3.avi", Application.StartupPath & "\" & newFilename, True)
End If
End Sub
Dim recordControl2 As Integer
Dim videoWriter2 As New AForge.Video.FFMPEG.VideoFileWriter()
Sub RecordVideo2(image As Bitmap)
' create instance of video writer
If recording = True Then
' create a bitmap to save into the video file
'Dim image As New Bitmap(width, height, PixelFormat.Format24bppRgb)
' write 1000 video frames
'For i As Integer = 0 To 999
' image.SetPixel(i Mod width, i Mod height, Color.White)
videoWriter2.WriteVideoFrame(image)
'Next
recordControl2 += 1
If recordControl2 = modOper.recordLimit Then
videoWriter2.Close()
recordControl2 = 0
SaveVideo2()
OpenWriterVideo2()
End If
'System.IO.File.Copy(Application.StartupPath & "\SAMPLE3.avi", Application.StartupPath & "\" & newFilename, True)
End If
End Sub
Sub SaveVideo2()
Dim newFilename As String = "CAMERA2-" & Now.Year & "-" & Now.Month & "-" & Now.Day & "-" & Now.Hour & Now.Minute & Now.Second & ".avi"
System.IO.File.Copy("C:\VIDEOS\CAMERA2.avi", "C:\Videos\" & newFilename, True)
Dim cls As New DATACLS
'cls.SaveVideoToDB("C:\Videos\" & newFilename)
cls.SaveVideoToDatabase("C:\Videos\" & newFilename)
End Sub
Sub SaveVideo1()
' create instance of video writer
Dim newFilename As String = "CAMERA1-" & Now.Year & "-" & Now.Month & "-" & Now.Day & "-" & Now.Hour & Now.Minute & Now.Second & ".avi"
System.IO.File.Copy("C:\VIDEOS\CAMERA1.avi", "C:\Videos\" & newFilename, True)
Dim duration As String = GetDuration("C:\VIDEOS\CAMERA1.avi")
Dim cls As New DATACLS
'cls.SaveVideoToDB("C:\Videos\" & newFilename)
cls.SaveVideoToDatabase("C:\Videos\" & newFilename)
End Sub
Function GetDuration(ByVal MovieFullPath As String) As String
Dim fileinfo As New FileInfo(MovieFullPath)
Dim folderinfo As New DirectoryInfo("C:\Videos")
If File.Exists(MovieFullPath) Then
Dim objShell As Object = CreateObject("Shell.Application")
Dim objFolder As Object = _
objShell.Namespace(Path.GetDirectoryName(MovieFullPath))
For Each file In folderinfo.GetFiles
If file.Name = Path.GetFileName(MovieFullPath) Then
Return (objFolder.GetDetailsOf(file.Name, 27).ToString)
Exit For
Exit Function
End If
Next
Return ""
Else
Return ""
End If
End Function
Dim recording As Boolean = False
Private Sub btnRecord_Click(sender As System.Object, e As System.EventArgs) Handles btnRecord.Click
If camera1Combo.Enabled = True Then
Dim fminute As New frmMinute
If videoPlaying = True Then
If recording = False Then
If fminute.ShowDialog = Windows.Forms.DialogResult.OK Then
OpenWriterVideo1()
OpenWriterVideo2()
recording = True
btnRecord.Image = picRecordStop.Image
Timer1.Enabled = True
End If
Else
Timer1.Enabled = False
picRecording.Visible = False
recording = False
btnRecord.Image = picRecord.Image
videoWriter1.Close()
videoWriter2.Close()
SaveVideo1()
If camera2Combo.Enabled = True Then
videoWriter2.Close()
SaveVideo2()
End If
End If
End If
End If
End Sub
Dim pcVisible = False
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If pcVisible = False Then
pcVisible = True
picRecording.Visible = True
Else
pcVisible = False
picRecording.Visible = False
End If
End Sub
'Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' 'Dim duration As String = Me.GetDuration("C:\Videos\CAMERA1.avi")
' Dim duration As String = getinfo("C:\Videos\CAMERA1.avi")
'End Sub
'Function ConvertTime(ByVal TheTime As Integer)
' ' Converts Windows Media Player (AxWMP Component)
' ' Default Play Time in Milliseconds to either: (00:00) MM:SS (Minutes:Seconds)
' ' or HH:MM:SS (00:00:00) (Hours:Minutes:Seconds)
' On Error Resume Next
' Dim NewTime As String
' Dim Sec As Single
' Dim min As Single
' Dim h As Single
' Dim a As String = ""
' If TheTime = 60 Then
' ConvertTime = "1:00"
' Exit Function
' End If
' If TheTime > 60 Then
' Sec = TheTime
' min = Sec / 60
' min = Int(min)
' Sec = Sec - min * 60
' h = min / 60
' h = Int(h)
' min = min - h * 60
' 'If your current time in Seconds is less than 10 then designate a 0 prior to the single digit second - Prevents you from showing 0:1 as opposed to 0:01.
' If Sec < 10 Then
' a = "0"
' End If
' 'edit this line to add Hours(HH:MM:SS) if you would like
' ConvertTime = String.Format("{0}:{1}{2}", min, a, Sec)
' End If
' If TheTime < 60 Then
' Sec = TheTime
' min = Sec / 60
' min = Int(min)
' Sec = Sec - min * 60
' h = min / 60
' h = Int(h)
' min = min - h * 60
' If Sec < 10 Then
' a = "0"
' End If
' ConvertTime = String.Format("{0}:{1}{2}", min, a, Sec)
' End If
'End Function
'Function getinfo(filename As String) As String
' AxWindowsMediaPlayer1.URL = filename
' AxWindowsMediaPlayer1.Ctlcontrols.play()
' AxWindowsMediaPlayer1.Ctlcontrols.stop()
' Return (ConvertTime(AxWindowsMediaPlayer1.currentMedia.duration))
'End Function
'Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' 'Get the duration of the avi file here while it is playing
' lblTotalTime.Text = ConvertTime(AxWindowsMediaPlayer1.Ctlcontrols.currentItem.duration)
' AxWindowsMediaPlayer1.Ctlcontrols.stop()
'End Sub
`End Class