B4J Tutorial [Server] Screen capture server

Discussion in 'B4J Tutorials' started by Erel, Jan 30, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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:

    Code:
    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(
    500LastException.Message)
       
    End Try
    End Sub
    The main module code:
    Code:
    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
     
    jinyistudio, MarkusR and AzureCrystal like this.
  2. giga

    giga Well-Known Member Licensed 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?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Make sure that you started a non-UI project.
     
  4. giga

    giga Well-Known Member Licensed User

    Just caught my mistake as you replied (that was it). Sorry to bother you.
     
  5. Asim A Baki

    Asim A Baki Active Member Licensed User

    I got this error
    java.lang.ClassNotFoundException: b4j.example.capture
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    You should create a class name Capture and add the handler code above.
     
  7. Asim A Baki

    Asim A Baki Active Member Licensed 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
     
  8. Asim A Baki

    Asim A Baki Active Member Licensed User

    after some tests I believe the problem is with the robot, it looks like the robot is very slow
    Code:
    mImage = robot.RunMethodJO ("createScreenCapture"Array As Object(rectangle))
     
  9. tuicemen

    tuicemen Member Licensed User

    I love this!
    I managed to put this into a timer as well and enabled a file save of the image if needed.
     
  10. coslad

    coslad Well-Known Member Licensed User

    hi tuicemen

    have you putted a timer ? Where ?

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

    tuicemen Member Licensed 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.
     
  12. AzureCrystal

    AzureCrystal Member Licensed 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:

    Code:
    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(
    500LastException.Message)
       
    End Try
    End Sub
    *Additional libs used: jAWTRobot v 1.40, jDateUtils v1.05
     
    Last edited: Mar 28, 2016
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice