AdvancedHMI Software
General Category => Open Discussion => Topic started by: joko markono on December 04, 2019, 11:05:39 PM
-
Hi guys,
I know this is more towards VB.net and i need help as i'm not a real programmer.
I have the basic code from this source: http://visualbasictutoriales.blogspot.com/2015/03/webcam-video-aforge-con-visual-basic.html (http://visualbasictutoriales.blogspot.com/2015/03/webcam-video-aforge-con-visual-basic.html) you need to translate the page to English.
i need to send my AHMI data string to the live video as an overlay data. I found a tutorial on how to put text overlay on the video but as i said, i'm not a real programmer, i'm not to sure how to manipulate the code to the source code that i link above.
Here is the code that i said:
Class MainWindow
Dim videoPath As String = "D:\Music\Video Downloads\Sample1.mp4"
Dim imagePath As String = "D:\Pictures\transp_blue.png"
Private Sub MainWindow_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
PlayVideo()
ShowLabel()
End Sub
Private Sub PlayVideo()
Dim player As New MediaPlayer
player.Open(New Uri(videoPath, UriKind.Relative))
' Create the VideoDrawing.
Dim videoDrawing As New VideoDrawing()
videoDrawing.Rect = New Rect(150, 0, 100, 100)
videoDrawing.Player = player
' Assign the DrawingBrush.
Dim brush As New DrawingBrush(videoDrawing)
Me.Background = brush
' Start playback.
player.Play()
End Sub
Private Sub ShowLabel()
Dim Label1 As New Label
' Give the Label a semi-transparent background image from a PNG file:
Dim uri As New Uri(imagePath)
Dim bi As New BitmapImage(uri)
Dim labelBackground As New ImageBrush(bi)
labelBackground.Opacity = 0.5
' Set the Label properties including text
With Label1
.Background = labelBackground
.Content = "Example Text"
.Height = 100
.Width = 400
.Opacity = 0.5
.FontSize = 48
.Foreground = Brushes.Red
.FontFamily = New FontFamily("Segoe WP Black")
End With
' Add a Label to the Grid
Grid1.Children.Add(Label1)
End Sub
End Class
appreciate for all help or guidance.
-
Try adding this at the end:
Label1.BringToFront()
-
i reduce the code to simplified version:
Imports AForge.Video.DirectShow
Public Class frmCamera
Private videoDevices As FilterInfoCollection
Private videoCapabilities As VideoCapabilities()
Private videoDevice As VideoCaptureDevice
Private Sub EnumerateVideoDevices()
' enumerate video devices
videoDevices = New FilterInfoCollection(FilterCategory.VideoInputDevice)
If videoDevices.Count <> 0 Then
' add all devices to combo
For Each device As FilterInfo In videoDevices
ComboBoxSources.Items.Add(device.Name)
Next
Else
ComboBoxSources.Items.Add("No DirectShow devices found")
End If
ComboBoxSources.SelectedIndex = 0
End Sub
Private Sub EnumerateVideoModes(device As VideoCaptureDevice)
' get resolutions for selected video source
Me.Cursor = Cursors.WaitCursor
ComboBoxModes.Items.Clear()
Try
videoCapabilities = videoDevice.VideoCapabilities
For Each capabilty As VideoCapabilities In videoCapabilities
If Not ComboBoxModes.Items.Contains(capabilty.FrameSize) Then
ComboBoxModes.Items.Add(capabilty.FrameSize)
End If
Next
If videoCapabilities.Length = 0 Then
ComboBoxModes.Items.Add("Not supported")
End If
ComboBoxModes.SelectedIndex = 0
Finally
Me.Cursor = Cursors.[Default]
End Try
End Sub
Private Sub frmCamera_Load(sender As Object, e As EventArgs) Handles MyBase.Load
EnumerateVideoDevices()
End Sub
Private Sub ComboBoxSources_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxSources.SelectedIndexChanged
If videoDevices.Count <> 0 Then
videoDevice = New VideoCaptureDevice(videoDevices(ComboBoxSources.SelectedIndex).MonikerString)
EnumerateVideoModes(videoDevice)
End If
End Sub
Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
CameraStart()
End Sub
Private Sub ButtonStop_Click(sender As Object, e As EventArgs) Handles ButtonStop.Click
CameraStop()
End Sub
Private Sub CameraStart()
If videoDevice IsNot Nothing Then
If (videoCapabilities IsNot Nothing) AndAlso (videoCapabilities.Length <> 0) Then
videoDevice.DesiredFrameSize = DirectCast(ComboBoxModes.SelectedItem, Size)
End If
VideoSourcePlayer1.VideoSource = videoDevice
VideoSourcePlayer1.Start()
End If
End Sub
Private Sub CameraStop()
If VideoSourcePlayer1.VideoSource IsNot Nothing Then
' stop video device
VideoSourcePlayer1.SignalToStop()
VideoSourcePlayer1.WaitForStop()
VideoSourcePlayer1.VideoSource = Nothing
End If
End Sub
Private Sub VideoSourcePlayer1_NewFrame(sender As Object, ByRef image As Bitmap) Handles VideoSourcePlayer1.NewFrame
' add overlay
Dim g As Graphics = Graphics.FromImage(image)
g.DrawString("Augmented reality?", New Font("Arial", 16), Brushes.Black, New Rectangle(10, 10, 200, 50))
g.Dispose()
End Sub
End Class
here is the reference link: https://www.codeproject.com/Articles/559932/Fabrika-LAB-WebCam-Video (https://www.codeproject.com/Articles/559932/Fabrika-LAB-WebCam-Video)
manage to get the overlay text. but wonder how to replace the:
g.DrawString("Augmented reality?", New Font("Arial", 16), Brushes.Black, New Rectangle(10, 10, 200, 50))
by data string.
-
You need to replace "Augmented reality?" with your data string, for example if a BasicLabel is showing the data string then it would be similar to this:
g.DrawString(BasicLabel1.Value, New Font("Arial", 16), Brushes.Black, New Rectangle(10, 10, 200, 50))
'Or
g.DrawString(TextBox1.Text, New Font("Arial", 16), Brushes.Black, New Rectangle(10, 10, 200, 50))
'Or create somewhere a new variable that can hold the data string and assign to it within the sub where you get data
Private dataString as String
g.DrawString(dataString, New Font("Arial", 16), Brushes.Black, New Rectangle(10, 10, 200, 50))
-
yes, that works fine.
-
Now i'm trying the first code again. Trying to add the newframe but i get this error:
Error BC30590 Event 'NewFrame' cannot be found.
I've added the AForge.Controls
Here is the code ( scroll down to Private Sub PictureBox1_NewFrame):
Imports AForge.Video
Imports AForge.Video.DirectShow
Imports AForge.Video.FFMPEG
Imports AForge.Controls
Imports System.IO.Ports
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Public Class Form1
Inherits Form
Dim CAMARA As VideoCaptureDevice 'CAMARA QUE ESTAMOS USANDO
Dim BMP As Bitmap 'PARA GENERACION DE IMAGENES
Dim ESCRITOR As New VideoFileWriter() 'GUARDA LAS IMAGENES EN MEMORIA
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
PictureBox1.SizeMode = PictureBoxSizeMode.Zoom ' POR SI OLVIDAMOS AJUSTAR EL PICTUREBOX
'PORT SETTING BELOW (ADDED BY EFENDY):
Timer1.Interval = 300
Timer1.Enabled = False
datareceive_label.Text = FormStartPosition.CenterParent
Dim available_Ports As Array = IO.Ports.SerialPort.GetPortNames
Dim i As Integer
For i = 0 To UBound(available_Ports)
PortComboBox.Items.Add(available_Ports(i))
Next
BaudrateComboBox.Items.Clear()
BaudrateComboBox.Items.Add("4800")
BaudrateComboBox.Items.Add("9600")
BaudrateComboBox.Items.Add("19200")
BaudrateComboBox.SelectedIndex = 2
ParityComboBox.Items.Clear()
ParityComboBox.Items.Add(IO.Ports.Parity.None)
ParityComboBox.Items.Add(IO.Ports.Parity.Odd)
ParityComboBox.Items.Add(IO.Ports.Parity.Even)
ParityComboBox.SelectedIndex = 2
End Sub
Private Sub ButtonCAMARA_Click(sender As System.Object, e As System.EventArgs) Handles ButtonCAMARA.Click
Dim CAMARAS As VideoCaptureDeviceForm = New VideoCaptureDeviceForm() 'DIALOGO CAMARAS DISPONIBLES
If CAMARAS.ShowDialog() = DialogResult.OK Then
CAMARA = CAMARAS.VideoDevice 'CAMARA ELEGIDA
AddHandler CAMARA.NewFrame, New NewFrameEventHandler(AddressOf CAPTURAR) ' EJECUTARA CADA VEZ QUE SE GENERE UNA IMAGEN
CAMARA.Start() 'INICIA LA PRESENTACION DE IMAGENES EN EL PICTUREBOX
End If
End Sub
Private Sub PictureBox1_NewFrame(sender As Object, ByRef image As Bitmap) Handles PictureBox1.NewFrame
' add overlay
Dim g As Graphics = Graphics.FromImage(image)
g.DrawString(datareceive_label.Text, New Font("courier new", 12), Brushes.Black, New Rectangle(10, 10, 1000, 500))
g.Dispose()
End Sub
Private Sub ButtonVIDEO_Click(sender As System.Object, e As System.EventArgs) Handles ButtonVIDEO.Click
If ButtonVIDEO.BackColor = Color.Black Then 'NO ESTA GRABANDO VIDEO
SaveFileDialog1.DefaultExt = ".avi" ' GUARDARA COMO ARCHIVO AVI
If SaveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim ANCHO As Integer = CAMARA.VideoResolution.FrameSize.Width 'DEFINE EL ANCHO DEL FOTOGRAMA
Dim ALTO As Integer = CAMARA.VideoResolution.FrameSize.Height ' DEFINE EL ALTO DEL FOTOGRAMA
'CREA EL ARCHIVO PARA LOS DATOS CON LOS PARAMETROS DE GUARDADO
ESCRITOR.Open(SaveFileDialog1.FileName, ANCHO, ALTO, NumericUpDownFPS.Value, VideoCodec.Default, NumericUpDownBRT.Value * 1000)
ESCRITOR.WriteVideoFrame(BMP) 'EMPIEZA A GUARDAR DATOS
ButtonVIDEO.BackColor = Color.Red 'PARA QUE SEPAMOS QUE ESTA GRABANDO
End If
Else
ButtonVIDEO.BackColor = Color.Black ' ESTA GRABANDO
ESCRITOR.Close() 'DEJA DE GUARDAR DATOS
End If
End Sub
Private Sub CAPTURAR(sender As Object, eventArgs As NewFrameEventArgs)
If ButtonVIDEO.BackColor = Color.Black Then 'SI NO ESTA GRABANDO ......
BMP = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'PONE LOS DATOS EN EL BITMAP
PictureBox1.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'LOS PRESENTA EN EL PICTURE BOX
Else ' SI ESTAS GRABANDO...
Try
BMP = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'PON LOS DATOS EN EL BITMAP
PictureBox1.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'LOS PRESENTA EN EL PICTURE BOX
ESCRITOR.WriteVideoFrame(BMP) 'LOS GUARDA EN LA MEMORIA
Catch ex As Exception
End Try
End If
End Sub
Private Sub ButtonFOTO_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFOTO.Click
CAPTURA.PictureBox1.Image = PictureBox1.Image 'COPIA LA IMAGEN QUE HAY EN EL PICTUREBOX EN EL PICTUREBOX DEL FORMULARIO CAPTURA
CAPTURA.Show() 'MUESTRA EL FORMULARIO CAPTURA
End Sub
Private Sub ButtonCERRAR_Click(sender As System.Object, e As System.EventArgs) Handles ButtonCERRAR.Click
End
End Sub
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Try
CAMARA.Stop() 'CIERRA LA CAMARA
ESCRITOR.Close() 'DEJA DE GUARDAR DATOS.
Catch ex As Exception
End Try
End Sub
Private Sub NumericUpDownBRT_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDownBRT.ValueChanged
End Sub
Private Sub Open_btn_Click(sender As Object, e As EventArgs) Handles Open_btn.Click
SerialPort1.PortName = PortComboBox.Text
SerialPort1.BaudRate = BaudrateComboBox.Text
Select Case ParityComboBox.Text
Case "None"
SerialPort1.Parity = IO.Ports.Parity.None
Case "Even"
SerialPort1.Parity = IO.Ports.Parity.Even
Case "Odd"
SerialPort1.Parity = IO.Ports.Parity.Odd
End Select
If SerialPort1.IsOpen Then
SerialPort1.Close()
Timer1.Enabled = False
Open_btn.BackColor = Color.LimeGreen
Else
SerialPort1.Open()
Timer1.Enabled = True
End If
End Sub
Dim Data_received As String
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Data_received &= SerialPort1.ReadExisting
If Len(Data_received) > 0 Then
datareceive_label.Text = Data_received
End If
Data_received = ""
'SEND DATA SETTING ON SERIAL PORT 2
'below are hex codes:
'H10 = CLEAR SCREEN
'H11 = CURSOR RIGHT
'H12 = CURSOR UP
'H13 = CURSOR DOWN
'H14 = CURSOR LEFT
'H15 = HOME CURSOR
'H0A = LINE FEED
'HOD = CARRIAGE RETURN
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
End Sub
End Class
-
You need to check this post, look at the picture and read the part about "Lightning Bolt":
https://www.advancedhmi.com/forum/index.php?topic=2078.msg11795#msg11795
-
Maybe try this:
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
' add overlay
e.Graphics.DrawString(datareceive_label.Text, New Font("courier new", 12), Brushes.Black, New Rectangle(10, 10, 1000, 500))
End Sub
-
your code has no error but i got no output on the video instead, it is on the picture frame which is outside the video boundary (see picture attachment).
ALso i tried this with no luck at all:
Private Sub CAPTURAR(sender As Object, ByRef image As Bitmap)
' add overlay
Dim g As Graphics = Graphics.FromImage(image)
g.DrawString("Augmented reality?", New Font("Arial", 16), Brushes.Black, New Rectangle(10, 10, 200, 50))
g.Dispose()
End Sub
almost there.
-
I have no other suggestions for you.
In your reply #4 you did state that the things were working, so go back to that solution.
Or add another picture box or label and dedicate it for showing data only.
When working with the form, you can always bring certain controls to the front and send other controls to the back.
-
that's the strange thing. on the first code it works but not on the second code. But i notice the AForge version on both code is different.
-
See if you can use anything from here:
https://www.advancedhmi.com/forum/index.php?topic=976.0
-
alright guys, i'm almost there.
here is the code, i insert the text overlay command in the CAPTURAR sub:
Private Sub CAPTURAR(sender As Object, eventArgs As NewFrameEventArgs)
If ButtonVIDEO.BackColor = Color.Black Then 'SI NO ESTA GRABANDO ......
BMP = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'PONE LOS DATOS EN EL BITMAP
PictureBox1.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'LOS PRESENTA EN EL PICTURE BOX
Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
g.DrawString("try", New Font("Arial", 16), Brushes.White, New Rectangle(10, 10, 200, 50))
g.Dispose()
Else ' SI ESTAS GRABANDO...
Try
BMP = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'PON LOS DATOS EN EL BITMAP
PictureBox1.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'LOS PRESENTA EN EL PICTURE BOX
ESCRITOR.WriteVideoFrame(BMP) 'LOS GUARDA EN LA MEMORIA
Catch ex As Exception
End Try
End If
End Sub
manage to get the text but after few moment, it turned out to be like in the attachment.
the reason i want this program is because it is independent.
-
You will have to keep trying.
There is no easy way to even try and test what you've been doing.
-
Are you the guy on the TV news lately, hacking into people's house cameras?
Godra may be in trouble with the G-man, be his accomplice and all.
-
;D