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:
The handler code uses JavaObject to take a screenshot and write it to the output stream:
The main module code:
For example if you call it from the device browser:
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