B4J Question Screen capture.

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All

I have a screen capture image but don't know how to find it.

I found this code in the B4J HowTos files using Java Oblects and it appears to work. However it appears to store the result in an array, not being up on JO I am at a loss to recover the image to save to a folder.

Any advice appreciated.

Regards Roger

B4X:
' Make the screenshot and create the PNG file
' Parameter:
' For the Screen set doscreen = True else App Window is captured
' Filename to store the image
Sub screenShot(doscreen As Boolean, filename As String)
  Log("screenshot :: Start")
  Log("Get Screensize...")
  Dim joSZ As JavaObject           
  joSZ.InitializeStatic("java.awt.Toolkit")
  ' To get the screensize use
  ' joSZ.RunMethodJO("getDefaultToolkit", Null).RunMethod("getScreenSize", Null))

  Log("Create Rectangle...")
  Dim joR As JavaObject
  If doscreen = True Then
    Log("For the Screen...")
    'For fullscreen use:
    joR.InitializeNewInstance("java.awt.Rectangle", Array(joSZ.RunMethodJO("getDefaultToolkit", Null).RunMethod("getScreenSize", Null)))
  Else
    Log("For the AppWindow...")
    ' Get the form size
    Dim wl As Int = MainForm.WindowLeft
    Dim wt As Int = MainForm.WindowTop
    Dim wh As Int = MainForm.WindowHeight
    Dim ww As Int = MainForm.WindowWidth
    Log("Dimensions: x,y,h,w: "& MainForm.WindowLeft & "," & MainForm.WindowTop & "," & MainForm.WindowHeight & "," & MainForm.WindowWidth)
    ' For the app window use:
    'int x, int y, int width, int height
    joR.InitializeNewInstance("java.awt.Rectangle", Array As Object(wl, wt, ww, wh))
  End If

  Log("Init the Robot...")
  Dim joRobot As JavaObject           
  joRobot.InitializeNewInstance("java.awt.Robot", Null)
  ' To capture the screen use
  ' joRobot.RunMethod("createScreenCapture", Array(joR))

  Log("Create the File...")
  Dim joF As JavaObject
  ' Create the file instances for the files (the constructor)
    joF.InitializeNewInstance("java.io.File", Array As Object(filename))

  Log("Capture & Save the Image...")
  Dim joWI As JavaObject
  joWI.InitializeStatic("javax.imageio.ImageIO")
  joWI.RunMethod("write", Array As Object(joRobot.RunMethod("createScreenCapture", Array(joR)), "PNG", joF))

  Log("screenshot :: End")

End Sub

Examples
Private CFILE As String = "screenshot.png"
Sub btnScreenshot_Action
  screenShot(True, CFILE)
End Sub

Sub btnAppshot_Action
  screenShot(False, CFILE)
End Sub
 
Last edited:

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks Roycefer,

Downloaded library, added it to project, read all posts.
I haven't a clue what to do with it.:(

However I have found a solution for what I need by fiddling, not a Java Object in site.

Regards Roger
PS I will revisit your jAWTRobot library it appears to have a lot going for it.

B4X:
Sub BtnSave_action
    Private Out As OutputStream
    Private TempImage As Image
   
    If File.Exists(DirPath, "") = False Then File.MakeDir(DirPath, "")
       If FileFlag = 0 Then            'FileFlag is set to 1 by InPutFileName Sub if valid file name.   

'Take ScreenShot of Google map, save as map.png
    If File.Exists(DirPath, "Map.png") Then File.Delete(DirPath, "Map.png")
    Out = File.OpenOutput(DirPath, "Map.png", False)
    TempImage = Pane1.Snapshot
    TempImage.WriteToStream(Out)
    Out.Close
   
'Take a screenshot and Save as ScrnShotTemp.png   
    If File.Exists(DirPath, "ScrnShotTemp.png") Then File.Delete(DirPath, "ScrnShotTemp.png")
    Out = File.OpenOutput(DirPath, "ScrnShotTemp.png", False)
    TempImage = pnlDispLatLng.Snapshot
    TempImage.WriteToStream(Out)
    Out.Close

'Place Map on Canvas
    TempImage.Initialize(DirPath, "Map.png")
    MapCanvas.Initialize("")
    MapCanvas.Height = TempImage.Height
    MapCanvas.Width = TempImage.Width
    MapCanvas.DrawImage(TempImage, 0, 0, TempImage.Width, TempImage.Height)
   
'Place ScreenShot on Map
    TempImage.Initialize(DirPath, "ScrnShotTemp.png")
    MapCanvas.DrawImage(TempImage, 0, 0, TempImage.Width, TempImage.Height)
'Input file name
        InPutFileName
        Return                        'Ignore
    End If
   
'Save using FileName
    Out = File.OpenOutput(DirPath, FileName, False)
    MapCanvas.Snapshot.WriteToStream(Out)
    Out.Close
    CustomToastMsg.ToastMessageColor = fx.Colors.White
    CustomToastMsg.ToastLocation = CustomToastMsg.TOAST_CENTER
    CustomToastMsg.ToastDuration =CustomToastMsg.TOAST_SHORT_DELAY
    CustomToastMsg.ToastShow(FileName&" Saved to Directory ABT")
           
'Reset
    If File.Exists(DirPath, "map.png") Then File.Delete( DirPath, "map.png")
    If File.Exists(DirPath, "ScrnShotTemp.png") Then File.Delete( DirPath, "ScrnShotTemp.png")   
    FileFlag = 0
    FileName = ""
    DispInput.Text = ""
End Sub
 
Last edited:
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
As usual, the auto-complete menu and the attendant comments are the key to the solution. You're looking for robot.ScreenCaptureToFile(), which will capture the screen and save it to a file. The comments to that method tell you that if you don't like the default rectangle, you can specify a rectangle beforehand by calling one of the robot.ScreenCurrentRectangleSet...() methods.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi Roycefer,

I am up with auto-complete etc. but for auto-complete I first need a manual-start. EG a "list of types". I have looked in the documentation in the B4J tutorials forum etc. but can't find a list of types, events or members.

Have I missed something obvious?

Regards Roger.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
There's only one class, the AWTRobot class. It's best if you create only one instance of this class in your project:
B4X:
Dim lore As AWTRobot
or, if you are considerably less cool:
B4X:
Dim c3p0 As AWTRobot

The AWTRobot class has one event: _JVMShutdown(). To use that event, you have to add the shutdown hook with lore.AddJVMShutdownHook().
 
Upvote 0

specci48

Well-Known Member
Licensed User
Longtime User
... I have looked in the documentation in the B4J tutorials forum etc. but can't find a list of types, events or members.

I recommend to use one of the two available Help-Viewers. With theses tools you can easyly browser through all libraries and have a look at the available methods and functions:

the simple one: B4x Help Viewer
the extensive one: B4X Object Browser

 

Attachments

  • B4xHelpViewer.jpg
    B4xHelpViewer.jpg
    138.7 KB · Views: 326
  • B4XObjectBrowser.jpg
    B4XObjectBrowser.jpg
    197.1 KB · Views: 300
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Why not "Automata"?
I guess not cool Enough
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
I was expecting a more detailed question

Hi DonManfred,

I was expecting to find information in the blank areas:
Should there be information in the blank areas?
Is there some further action required by me to make the infomation available?
Have I done something wrong in the installation?
Have I reached the end of my dimishing brain cells and and failed to see the obvious? :(

Regards Roger

PS I'm betting on a "Yes" answer to that last question.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I was expecting to find information in the blank areas:
Should there be information in the blank areas?
If you want to get more infos about something from the jControlFX you need to open that node and use the content here i guess.

objectbrowser0063.png


objectbrowser0064.png
 
Upvote 0
Top