B4J Question jAWTRobot ScreenCaptureToFile

ThRuST

Well-Known Member
Licensed User
Longtime User
I want to use jAWTRobot ScreenCaptureToFile to take a screenshot of a specified area of the screen and save it to a file as a PNG file. Start and stop coordinates can be let's say imageview1.
A code snippet example how this works will do the trick, thanks.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
B4X:
lore.ScreenCurrentRectangleSetAsArbitrary(MainForm.WindowLeft + ImageView1.Left, MainForm.WindowTop + ImageView1.Top, ImageView1.Width, ImageView1.Height).ScreenCaptureToFile(File.Combine(File.DirApp,"filename.png"))

This is explained in the comments for the ScreenCapture methods.

If you're just taking a screenshot of a Node over which you have control, then you don't need the jAWTRobot library. You can use built-in methods:
B4X:
Dim o As OutputStream = File.OpenOutput(File.DirApp,"filename.png",False)
ImageView1.Snapshot.WriteToStream(o)
o.Close
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Thanks, I am creating a notepad that looks very realistic and it's really nice (textarea with transparent background and a notepad image underneath), so I want to add a function to save a note to a file and another option to save it to clipboard memory, so that users will be able to post notes directly when using Facebook and for other uses.

I tried this your first example and will try your second code, but I only want to copy a portion of the screen, so the imageview1 is a good practice.

B4X:
lore.ScreenCurrentRectangleSetAsArbitrary(MainForm.WindowLeft + ImageView1.Left, MainForm.WindowTop + ImageView1.Top, ImageView1.Width, ImageView1.Height).ScreenCaptureToFile("d:\MyNote.png")

Both generated this error

Waiting for debugger to connect...
Program started.
java.io.FileNotFoundException: E:\#PROGRAMMING\#Source Code\B4J\Notebook\Objects\d:\MyNote.png (Felaktig syntax för filnamn, katalognamn eller volymetikett)
at java.base/java.io.RandomAccessFile.open0(Native Method)
at java.base/java.io.RandomAccessFile.open(RandomAccessFile.java:343)
at java.base/java.io.RandomAccessFile.<init>(RandomAccessFile.java:259)
at java.base/java.io.RandomAccessFile.<init>(RandomAccessFile.java:214)
at java.desktop/javax.imageio.stream.FileImageOutputStream.<init>(FileImageOutputStream.java:69)
at java.desktop/com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(FileImageOutputStreamSpi.java:55)
at java.desktop/javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:419)
at java.desktop/javax.imageio.ImageIO.write(ImageIO.java:1549)
at butt.droid.awtRobot.AWTRobot.ScreenCaptureToFile(AWTRobot.java:816)
at Notebook.Duologic.Se.main._btncapture_click(main.java:115)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:613)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at anywheresoftware.b4a.BA$1.run(BA.java:215)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
at java.base/java.lang.Thread.run(Thread.java:844)
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
You're not constructing a valid file path. Look at the info for the java.io.FileNotFoundException.
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
This is what I tried

B4X:
Dim SaveToPath As String = "test.png"
    lore.ScreenCurrentRectangleSetAsArbitrary(MainForm.WindowLeft + ImageView1.Left, MainForm.WindowTop + ImageView1.Top, ImageView1.Width, ImageView1.Height).ScreenCaptureToFile(SaveToPath)

It works when removing the d:\ in front of the filename. See my note :)

test.png
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
As for my followup question, can your awesome library handle images to clipboard? I would like to have an option to store the image in clipboard so the note can be pasted directly to the screen, that would be awesome. Thanks for your help with this.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
The comments for the ScreenCapture method say that the image will be saved relative to the current working directory. I think you will be better off using the ImageView1.Snapshot method.

The fx.Clipboard Object can place Images in the clipboard.
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I see, can you update your library to support a custom path? It would make it the best solution :)
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Ok, I added a ScreenCapture2 method that does that. The latest version is 1.55. Download it from the library's thread.
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
And here's how to use it

B4X:
Dim SaveToPath As String = "d:\MyNote.png"
    lore.ScreenCurrentRectangleSetAsArbitrary(MainForm.WindowLeft + ImageView1.Left + 20, MainForm.WindowTop + ImageView1.Top + 60, ImageView1.Width - 50, ImageView1.Height- 50).ScreenCaptureToFile2(SaveToPath)



MyNote.png
 
Upvote 0
Top