B4A Library A small vidéo MJPEG librarie for ip cam

Library for IP Camera MJPEG

have not found how to handle the CGI video stream with the library "camera" so I wrote my little personal library ...

MJPEG Video Streams (Network cameras streaming the video in the MJPEG format.)

LIST IP CAM: Webcam / Netcam - Security Video Camera Surveillance Software - CrazyPixels

B4X:
Sub Process_Globals
Dim timer1 As Timer
End Sub
Sub Globals
Dim mycam As Videomjpeg
Dim c  As Canvas
Dim p As Panel
Dim DestRect As Rect
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime =True Then
p.Initialize("p")
p.Color=Colors.LightGray
timer1.Initialize("timer1",250)
timer1.Enabled=True
End If

End Sub
Sub maj_cam
mycam.cam("192.168.2.11",8080,"/videostream.cgi?user=admin&pwd=admin")
p.RemoveView
p.Initialize("p")
p.Color=Colors.LightGray
Activity.AddView(p,50,50,320,240)
c.Initialize(p)
DestRect.Initialize(0,0,p.Width,p.Height)
c.DrawDrawable(mycam.Image,DestRect)
mycam.unbindDrawables(p)
Activity.Invalidate
End Sub

Sub Activity_Resume
p.Initialize("p")
timer1.Enabled=True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
timer1.Enabled=False
End Sub

Sub timer1_tick
maj_cam
End Sub
 

Attachments

  • ipcam.zip
    22.8 KB · Views: 1,231
Last edited:

GabrielM

Member
Licensed User
Longtime User
Hi Allan,

really nice to see this mjpeg approach working. Congrats :sign0098:

would like to ask if it there is a way of refreshing the image without invalidating whole activity?

Thank you
 

NJDude

Expert
Licensed User
Longtime User
Also, change this line,

From:
B4X:
Activity.AddView(p,50,50,320,240)
To:
B4X:
Activity.AddView(p, 50dip, 50dip, 320dip, 240dip)

Alwayd use DIP, so the view scales properly on every device.
 

GabrielM

Member
Licensed User
Longtime User
Hi,

Many thanks to both of you.

Its really an improving, also the fps increased.
:icon_clap:
 

supriono

Member
Licensed User
Longtime User
Library for IP Camera MJPEG

have not found how to handle the CGI video stream with the library "camera" so I wrote my little personal library ...

MJPEG Video Streams (Network cameras streaming the video in the MJPEG format.)

LIST IP CAM: Webcam / Netcam - Security Video Camera Surveillance Software - CrazyPixels

B4X:
Sub Process_Globals
Dim timer1 As Timer
End Sub
Sub Globals
Dim mycam As Videomjpeg
Dim c  As Canvas
Dim p As Panel
Dim DestRect As Rect
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime =True Then
p.Initialize("p")
p.Color=Colors.LightGray
timer1.Initialize("timer1",250)
timer1.Enabled=True
End If

End Sub
Sub maj_cam
mycam.cam("192.168.2.11",8080,"/videostream.cgi?user=admin&pwd=admin")
p.RemoveView
p.Initialize("p")
p.Color=Colors.LightGray
Activity.AddView(p,50,50,320,240)
c.Initialize(p)
DestRect.Initialize(0,0,p.Width,p.Height)
c.DrawDrawable(mycam.Image,DestRect)
mycam.unbindDrawables(p)
Activity.Invalidate
End Sub

Sub Activity_Resume
p.Initialize("p")
timer1.Enabled=True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
timer1.Enabled=False
End Sub

Sub timer1_tick
maj_cam
End Sub

alan1968:
your code is work with my android. samsung young but when i want to send this parameter to IP camera (http://192.168.0.100/decoder_control/cgi?command=28&user=admin&pwr=) horizontal patrol, video not respone what can i do for fixe this trouble.
 

GKCS

Member
Licensed User
Longtime User
I was just wondering.
The library itself works, but there is a much more simple way without the need of any library.
The sample code triggers a timer, which periodically call the "videostream.cgi?user=admin&pwd=admin".
This CGI returns a MJPG stream, and a single JPG frame must be extracted.

Why not use directly "another" available CGI? Such IP-Cameras also offer a Snapshot function, which can be triggered diectly with

"snapshot.cgi?user=admin&pwd=admin"

Means, this CGI returns directly a usable JPG picture instead of a MJPG stream.

So, just create a hidden WebView Object and an ImageView of any size you like.

Use again a timer to trigger the CGI periodically (e.g. every 250 ms)

<code>
Sub timer1_tick
webview1.LoadUrl("http://192.168.xxx.xxx/snapshot.cgi?user=admin&pwd=admin")
End Sub

Sub WebView1_PageFinished (Url As String)
ImageView1.Bitmap=webview1.CaptureBitmap
DoEvents
End Sub
</code>

On this way, you avoid handling the MJPG stream, and you can deal directly with the JPG and a simple ImageView ...

The refresh rate can be the same as in the MJPG code, even more speedy, because no time-overhead for decoding is used.

Have fun

Gerry
 

ciprian

Active Member
Licensed User
Longtime User
What's wrong?
B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: mjpeg_test
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

Sub Process_Globals
    Dim timer1 As Timer
End Sub
Sub Globals
    Dim mycam As Videomjpeg
    Dim c  As Canvas
    Dim p As Panel
    Dim DestRect As Rect
End Sub
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime =True Then
        p.Initialize("p")
        p.Color=Colors.LightGray
        timer1.Initialize("timer1",250)
        timer1.Enabled=True
    End If
End Sub
Sub maj_cam
    mycam.CAM("88.173.34.105",8097,"/view/viewer_index.shtml?")
    p.RemoveView
    p.Initialize("p")
    p.Color=Colors.LightGray
    Activity.AddView(p, 5dip, 5dip, 320dip, 240dip)
    c.Initialize(p)
    DestRect.Initialize(0,0,p.Width,p.Height)
    c.DrawDrawable(mycam.Image,DestRect)
    mycam.unbindDrawables(p)
    Activity.Invalidate2(DestRect)
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub timer1_tick
    maj_cam
End Sub
 

jjmcc2

New Member
Licensed User
Longtime User
What's wrong?
B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: mjpeg_test
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

Sub Process_Globals
    Dim timer1 As Timer
End Sub
Sub Globals
    Dim mycam As Videomjpeg
    Dim c  As Canvas
    Dim p As Panel
    Dim DestRect As Rect
End Sub
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime =True Then
        p.Initialize("p")
        p.Color=Colors.LightGray
        timer1.Initialize("timer1",250)
        timer1.Enabled=True
    End If
End Sub
Sub maj_cam
    mycam.CAM("88.173.34.105",8097,"/view/viewer_index.shtml?")
    p.RemoveView
    p.Initialize("p")
    p.Color=Colors.LightGray
    Activity.AddView(p, 5dip, 5dip, 320dip, 240dip)
    c.Initialize(p)
    DestRect.Initialize(0,0,p.Width,p.Height)
    c.DrawDrawable(mycam.Image,DestRect)
    mycam.unbindDrawables(p)
    Activity.Invalidate2(DestRect)
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub timer1_tick
    maj_cam
End Sub




This will fix your problem

Change:
mycam.CAM("88.173.34.105",8097,"/view/viewer_index.shtml?")

TO:
mycam.cam("88.173.34.105",8097,"/mjpg/video.mjpg")
 

ciprian

Active Member
Licensed User
Longtime User
thank you jjmcc2. i am allready using this syntax, have found last week. It's good to see that is always someone trying to help.
I'm getting 1 image per second using this. I know that Erel would try to create a complete library for ip cams, but it's not sure, and not for today.
I'm waiting this new library ...
 

supriono

Member
Licensed User
Longtime User
I was just wondering.
The library itself works, but there is a much more simple way without the need of any library.
The sample code triggers a timer, which periodically call the "videostream.cgi?user=admin&pwd=admin".
This CGI returns a MJPG stream, and a single JPG frame must be extracted.

Why not use directly "another" available CGI? Such IP-Cameras also offer a Snapshot function, which can be triggered diectly with

"snapshot.cgi?user=admin&pwd=admin"

Means, this CGI returns directly a usable JPG picture instead of a MJPG stream.

So, just create a hidden WebView Object and an ImageView of any size you like.

Use again a timer to trigger the CGI periodically (e.g. every 250 ms)

<code>
Sub timer1_tick
webview1.LoadUrl("http://192.168.xxx.xxx/snapshot.cgi?user=admin&pwd=admin")
End Sub

Sub WebView1_PageFinished (Url As String)
ImageView1.Bitmap=webview1.CaptureBitmap
DoEvents
End Sub
</code>

On this way, you avoid handling the MJPG stream, and you can deal directly with the JPG and a simple ImageView ...

The refresh rate can be the same as in the MJPG code, even more speedy, because no time-overhead for decoding is used.

Have fun

Gerry

GKCS: last time i am tray your idea..bud i don't have any picture..in android dislay
i am use IP camera TPlink versin SC3030
http://192.168.0.100:8080/cgi-bin/view/image
can u help me
 

GKCS

Member
Licensed User
Longtime User
SUPRIONO: What is the exact URL which you can use within your standard
WebBrowser to show one static JPG from your cam?
Use this URL, add the Login-parameters (admin and pwd), and then it should work.
 
Top