B4J Tutorial [Server] Screen capture server

This is a simple server that takes a screenshot of the current desktop screen and returns the image as the response.

For example if you call it from the device browser:

upload_2014-1-30_16-26-52.png


The handler code uses JavaObject to take a screenshot and write it to the output stream:

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
   Try
     resp.ContentType = "image/png"
     Dim robot, toolkit, rectangle, ImageIO As JavaObject
     robot.InitializeNewInstance("java.awt.Robot", Null)
     toolkit.InitializeStatic("java.awt.Toolkit")
     Dim rectangle As JavaObject
     rectangle.InitializeNewInstance("java.awt.Rectangle", Array As Object( _
       toolkit.RunMethodJO("getDefaultToolkit", Null).RunMethod("getScreenSize", Null)))
     Dim image As JavaObject = robot.RunMethod("createScreenCapture", Array As Object(rectangle))
     ImageIO.InitializeStatic("javax.imageio.ImageIO").RunMethod("write", Array As Object( _
       image, "png", resp.OutputStream)) 'the image is written to the response
   Catch
     resp.SendError(500, LastException.Message)
   End Try
End Sub

The main module code:
B4X:
Sub Process_Globals
   Private srvr As Server
End Sub

Sub AppStart (Args() As String)
   srvr.Initialize("")
   srvr.Port = 51042
   srvr.AddHandler("/capture", "Capture", False)
   srvr.Start
   StartMessageLoop
End Sub
 

giga

Well-Known Member
Licensed User
Longtime User
Getting this error in the log when trying screencapture server.

Program started.
An error occurred:
(Line: 0) null
java.lang.Exception: Sub appstart signature does not match expected signature.
public static anywheresoftware.b4a.pc.RemoteObject b4j.example.main_subs_0._appstart(anywheresoftware.b4a.pc.RemoteObject) throws java.lang.Exception

Did I miss something?
 

Asim A Baki

Active Member
Licensed User
Longtime User
I got it working properly but I need faster screen capture algorithm, this createScreenCapture almost capture 3 or 4 snapshots per seconds, while I need at least 15 frames

I managed to put this function in a timer which tries to capture every 50ms
 

Asim A Baki

Active Member
Licensed User
Longtime User
after some tests I believe the problem is with the robot, it looks like the robot is very slow
B4X:
        mImage = robot.RunMethodJO ("createScreenCapture", Array As Object(rectangle))
 

tuicemen

Member
Licensed User
Longtime User
I love this!
I managed to put this into a timer as well and enabled a file save of the image if needed.
 

coslad

Well-Known Member
Licensed User
Longtime User
hi tuicemen

have you putted a timer ? Where ?

I need a self update image without refrash the page , have you did it ?
 

tuicemen

Member
Licensed User
Longtime User
Sorry for the slow reply. I'm not at home so I'm unable to supply details.
I did use a timer (in the main) saving to a temp file which gets overwritten on each tick. I also added the interval to the settings so I could easily increase the timeframe as I run this on a fast cable connection and a slower DSL connection at a different location.
I placed the temp file on a page which also views my IP cameras clicking on it opens a new page with only it displaying. Clicking on the picture here will save the image to a permanent file if needed.
The permanent file is date stamped.
 

AzureCrystal

Member
Licensed User
Longtime User
Great example and needed for a new project, thank you Erel. I modded your code to simply save a datestamped screen shot .png file as such:

B4X:
Sub Class_Globals
   Private c3po As AWTRobot
End Sub

Public Sub Initialize

End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
   Try
     c3po.ScreenCurrentRectangleSetAsRightScreen '<- Needed since I have multiple monitors attached
     c3po.ScreenCaptureToFile("screenshot-" & DateTime.Now & ".png")

     resp.Write("screen shot saved - " & DateUtils.TicksToString(DateTime.Now) )
   Catch
     resp.SendError(500, LastException.Message)
   End Try
End Sub
*Additional libs used: jAWTRobot v 1.40, jDateUtils v1.05
 
Last edited:
Top