I was playing with some of the screenshot stuffs in the JavaFX 2.2 and came up with idea of showing the image of the Operating system’s window stuffs in JavaFX ImageView and that was really easy to make via Java Robot API and JavaFX’s SwingFXUtils class. Thanks to the JavaFX teams 🙂
Ok here we go now . First we’ll look over how the algorithm goes for this task.
As you can see in above picture The portion of the Monitor is being screen captured by the Java Robot API . This give the BufferedImage Object of Java and then we convert them in to JavaFX Image using SwingFXUtils class. Now finally it’s all in our hand to show that Image in appropriate way.
Let’s move to the coding part.
Taking image of screen.
1 2 3 4 5 6 |
Robot ro = new Robot(); //Now this is the region which we are going to capture java.awt.Rectangle rect = java.awt.Rectangle(0,0,500,500); BufferedImage im = ro.createScreenCapture(rect); WritableImage image = new WritableImage(500,500); // Just a empty image of the specified dimensions SwingFXUtils.toFXImage(im,image); |
Now that was just the simple stuffs of showing the image captured from the screen. But let’s look now little bit more. How about making this image continously generate and give it to JavaFX and change the ImageView accordingly. For this I’ll need to make the asynchronous Task to call Java Robot API .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
private boolean stoppped = false; private ImageView imageView; // ... // ... initialization of controls Task t = new Task(){ @Override protected Object call() { while(!stopped){ try { Robot ro = new Robot(); BufferedImage im = ro.createScreenCapture(new java.awt.Rectangle(0,0,500,500)); final WritableImage image = new WritableImage(500,500); SwingFXUtils.toFXImage(im,image); //Calling the JavaFX Thread to update javafx control Platform.runLater(new Runnable(){ @Override public void run() { //This updates the imageview to newly created Image imageView.setImage(image); } }); //Sleep for 100 millisecond Thread.sleep(100); } catch (Exception ex) { //print stack trace or do other stuffs } } return null; } }; new Thread(t).start(); //... |
Wow Now this look pretty awesome. We are getting the clone desktop like a video in javafx. I think you may feel little bit sluggish on the video currently you are getting in javafx. To improve your live video looks more smooth just decrease your Thread.sleep() time gradually. Its really so fun.
Performance steps in image update
As I’ve figured out some few performance difference on the Image updates by using ImageView and Canvas. To make your application little bit fast and light then you can use Canvas API to update the image. I’ve seen that ImageView takes little bit more memory usage than the Canvas.I would prefer using Canvas to update the Image using
1 |
canvas.getGraphicsContext2D().drawImage(image, 0 , 0) |
Also I got one bug issue on using Canvas the memory usage level raised so fast. But it happened accidently only once. May some kind of bug.
Feather Edit
This is My 6 DAYS of JavaFX Chemistry Output = 4(JavaFX Development Time incl. CSS & design) + 2(Deployment+Website of App).
I think I’m pretty fast may be cause of multi-knowledge . But I have done this all my day and night. I’ve been so addicted and in love with JavaFX this week. 😛
You can checkout this software release at here : Feather Edit (free)
Only this much for today. Have a good 🙂 day folks.
Pingback: Java desktop links of the week, September 24 | Jonathan Giles
Pingback: JavaFX links of the week, September 24 // JavaFX News, Demos and Insight // FX Experience
Great tutorial Narayan ji. Simple and best! Great for all java lovers.
Excellent blog here! Also your website quite a bit up fast!
What host are you using? Can I get your associate hyperlink for your host?
I wish my website loaded up as quickly as yours lol