Imports System.Net.Sockets
Imports System.Threading.Thread
Imports System.Text
Imports System.IO
Public Class Form1
Dim measur(63) As Integer
Dim IPCamera As New TcpClient
Dim byteData As Byte()
Dim stx As Byte = 255
Dim ender As Byte = 217
Dim starter As Byte = 216
Dim byteval1 As Byte()
Dim ms As System.IO.MemoryStream
Dim bmp As Bitmap
Dim gh As Graphics
Dim chk As Integer
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'On Error GoTo lExit
Timer1.Stop()
If IPCamera.Connected Then
If IPCamera.Available > 0 Then
Sleep(100)
lbltext1.Text = IPCamera.Available
ReDim byteData(IPCamera.Available)
IPCamera.GetStream.Read(byteData, 0, byteData.Length)
'ms = New IO.MemoryStream(byteData)
Dim endIndex As Integer = 0
Dim enddata As Boolean = False
Dim startIndex As Integer = 0
Try
'--------------------------------------
While (Not enddata)
endIndex = Array.IndexOf(byteData, stx, endIndex)
If byteData(endIndex + 1) = starter Then
startIndex = endIndex
End If
If byteData(endIndex + 1) = ender Then
ReDim byteval1(endIndex + 1)
Array.Copy(byteData, startIndex, byteval1, startIndex, endIndex + 1)
ms = New IO.MemoryStream(byteval1)
Panel1.BackgroundImage = Image.FromStream(ms)
If Panel2.BackColor = Color.Red Then
Panel2.BackColor = Color.Pink
Else
Panel2.BackColor = Color.Red
End If
Exit While
End If
enddata = (endIndex = -1)
endIndex += 1
End While
Catch ex As Exception
lbltext3.Text = byteData.Length
End Try
End If
End If
'lExit:
Timer1.Start()
End Sub
Private Sub btConnect_Click(sender As Object, e As EventArgs) Handles btConnect.Click
Dim ip As String = TextBox1.Text
If TextBox1.Text = "" Then ip = "127.0.0.1"
Try
If IPCamera.Connected Then
IPCamera.Close()
Label69.Text = "Disconnect"
Else
IPCamera = New TcpClient
IPCamera.Connect(ip, 5000)
Sleep(100)
If IPCamera.Connected Then
Label69.Text = "Connected"
Timer1.Start()
Else
Label69.Text = "Disconnect"
End If
End If
Catch ex As Exception
MessageBox.Show("Error target IP: " + ip, "Error")
End Try
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 0 To measur.Length - 1
Dim lbl As Label = Me.Controls.Find("Label" & (i + 1), True).FirstOrDefault
lbl.BackColor = Color.FromArgb(50, Color.LightGray)
lbl.Text = ""
Next
'Label68.BackColor = Color.FromArgb(50, Color.LightGray)
End Sub
End Class
Tag : .NET, Win (Windows App), VB.NET, C#, VS 2015 (.NET 4.x), VS 2017 (.NET 4.x)
วิธี รับข้อมูล video แบบ steamming real time นะครับ นำมาแชร์ วิธีทำ
รับเป็นแบบ MJPEG นะครับ โค้ดนี้ฝั่ง client นะครับ
Code (VB.NET)
Imports System.Net.Sockets
Public Class Form1
Dim TcpClient As TcpClient
'Dim byteData As Byte()
Dim iStart As Integer, iEnd As Integer
Dim indexByte As Integer, iLen As Integer
Dim fps As Integer
Dim ms As New System.IO.MemoryStream
Dim buffer1() As Byte, buffer2() As Byte
Dim NS As NetworkStream
Dim iScan As Integer
'Dim bEnd As Boolean, bStart As Boolean
Const picMarker = &HFF
Const picStart = &HD8
Const picEnd = &HD9
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TcpClient = New TcpClient
TcpClient.Connect("192.168.1.29", 5000)
TcpClient.ReceiveBufferSize = 100000
indexByte = 0
ReDim buffer1(TcpClient.ReceiveBufferSize - 1)
ReDim buffer2(TcpClient.ReceiveBufferSize - 1)
If TcpClient.Connected Then
NS = TcpClient.GetStream
Timer1.Start()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
On Error GoTo lExit
Timer1.Stop()
If TcpClient.Connected Then
If TcpClient.Available > 0 Then
iScan += 1
iLen = TcpClient.Available
If iLen > (buffer1.Length - indexByte) Then
Array.Clear(buffer1, 0, buffer1.Length)
indexByte = 0
End If
NS.Read(buffer1, indexByte, iLen)
indexByte += iLen
Label1.Text = indexByte
Do
iStart = -1
For i = 0 To buffer1.Length - 2
If buffer1(i) = picMarker Then
If buffer1(i + 1) = picStart Then
iStart = i
Exit For
End If
End If
Next
iEnd = -1
For i = iStart To buffer1.Length - 2
If buffer1(i) = picMarker Then
If buffer1(i + 1) = picEnd Then
iEnd = i
Exit For
End If
End If
Next
If iStart >= 0 And iEnd >= 0 And iStart < iEnd Then
ms.Dispose()
ms = New System.IO.MemoryStream(buffer1, iStart, iEnd)
PictureBox1.Image = Image.FromStream(ms)
Array.Copy(buffer1, iEnd + 1, buffer2, 0, indexByte - iEnd)
Array.Clear(buffer1, 0, buffer1.Length)
Array.Copy(buffer2, 0, buffer1, 0, indexByte - iEnd)
Label2.Text = iStart
Label3.Text = iEnd
indexByte = indexByte - iEnd - 1
fps += 1
Else
Exit Do
End If
Loop
End If
End If
lExit:
Timer1.Start()
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Label5.Text = fps
Label10.Text = iScan
fps = 0
iScan = 0
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Stop()
If TcpClient.Connected Then TcpClient.Close()
TcpClient = New TcpClient
TcpClient.Connect("192.168.1.29", 5000)
indexByte = 0
If TcpClient.Connected Then
NS = TcpClient.GetStream
Timer1.Start()
End If
End Sub
End Class