Java Full Screenshot - java

I wanted to display on the JFrame on the program the full screenshot of my screen.
So far using the code below, I was able only to display part of the screen.
The code below is the content of the paint(Graphics g).
How can I make it full screen?
// the screen resolution is 1280 x 1024 while the JPanel size is only 1024 x 768
Dimension resolution = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rectangle = new Rectangle(resolution);
robot = new Robot();
BufferedImage bufferedImage = robot.createScreenCapture(rectangle);
g.drawImage(bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), Image.SCALE_DEFAULT), 0, 0, null);

Maybe using something like this:
//get the screen size
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
BufferedImage image = robot.createScreenCapture (dim);
//other code
//...
I see you have some errors, I don't know if your code even compiles, 'cause references seems not to be declared, but a code similar to this one will caputure a screenshoot of your desktop:
import java.awt.geom.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class ScreenCapturer
{
public static void main(String[] args)throws Exception
{
Dimension resolution = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rectangle = new Rectangle(resolution);
Robot robot = new Robot();
BufferedImage bufferedImage = robot.createScreenCapture(rectangle);
Graphics g = bufferedImage.getGraphics();
//g.drawImage(bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), Image.SCALE_DEFAULT), 0, 0, null);
File out = new File("image.png");
ImageIO.write(bufferedImage,"png",out);
}
}
I saved to an png image file instead of drawing it on the screen or the frame.

Use java.awt.Toolkit.getDefaultToolkit().getScreenSize() to get the size of the screen: http://www.roseindia.net/java/java-get-example/screen-dimensions.shtml

g.drawImage(bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), Image.SCALE_DEFAULT), 0, 0, null);
Simplify your code by only using one statement per line then you might be able to understand the code.
Why are you ue the width and height of the image? How does that scale the image if you specify the full size of the image? I would guess you want:
Image scaled = bufferedImage.getScaledInstance(1024, 768, Image.SCALE_DEFAULT);
Now instead of doing custom painting you can just add your image to a JLabel:
ImageIcon icon = new ImageIcon( scaled );
JLabel label = new JLabel( icon );
frame.add( label );

Related

Add Translucent Image to JLayeredPane

So I'm making a stopmotion application - the live feed from my webcam is inside a JPanel (with setOpaque(false)) that is inside the top (10th) layer in a JLayeredPane - and basically, when I take a picture, I want to add it to one of the lower layers so that a trace of the previous pictures you've taken shows up on screen. Here's how I'm trying to do that now:
EDIT: this is my new code based off the answer below - this now does nothing, as opposed to just adding the opaque image as before - if I add this to a JPanel, though, and add the JPanel to the JLayeredPane, then all I get is gray
BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img2.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));
g.drawImage(img2, 0, 0, null);
g.dispose();
ImageIcon imgIcon = new ImageIcon(new ImageIcon(img2).getImage().getScaledInstance(img2.getWidth(), img2.getHeight(), Image.SCALE_SMOOTH));
JLabel showPic = new JLabel(imgIcon);
showPic.setSize(layers.getSize());
showPic.setBounds(layers.getX() + 18, layers.getY(), img2.getWidth(), img2.getHeight());
layers.add(showPic, new Integer(1)); //layers is my JLayeredPane
layers.repaint();
layers.revalidate();
img is the picture I've just captured from my webcam, and I'm trying to make it semi transparent, then add it to a JLabel. How can I make this work? Or is there a better way to do this?
I don't know if this will solve the problem but the image you are looking for is
BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
java.awt.Transparency.TRANSLUCENT has the value of 3 and corresponds to BufferedImage.TYPE_INT_ARGB_PRE which only god knows what it's doing.
With BufferedImage.TYPE_INT_ARGB you can create a transparent image and apply Composite etc
You also need to correct this
g.drawImage(img2, null, 0, 0);
to
g.drawImage(img2, 0, 0, null);
--
Heres how transparency works for me:
I have two images bim and bim2 and I draw one on top of the other:
BufferedImage bim=null, bim2=null;
try {
bim=ImageIO.read(new File("...."));
bim2=ImageIO.read(new File("...."));
}
catch (Exception ex) { System.err.println("error in bim "+ex); }
int wc=bim.getWidth(), hc=bim.getHeight();
BufferedImage img2 = new BufferedImage(bim.getWidth(), bim.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img2.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));
g.drawImage(bim, 0, 0, null);
g.drawImage(bim2, 0, 0, wc, hc, null);
Then I can display it on a JPanel Jframe or whatever.
I can even create the Label:
JLabel showPic = new JLabel(new ImageIcon(img2));
JFrame f=new JFrame();
f.setSize(500, 500);
f.add(showPic);
f.setVisible(true);

take snapshot of full JFrame and Jframe only [duplicate]

This question already has an answer here:
Swing: Obtain Image of JFrame
(1 answer)
Closed 7 years ago.
Heys guys, I've developped a code to take a screen shot of my whole screen, but I want it to take the screen shot of only the things inside my Jframe. Ill be using it to print the image later on by the way. And one of the main problem is, the mouse also comes inside the snapshot. I don't want the mouse or the two buttons at the bottom. I can just change visi of buttons but what should be done for mouse and inside Jframe only shot? Here is my code it takes screen shot of whole screen.
try{
Thread.sleep(1000);
Toolkit tk = Toolkit.getDefaultToolkit(); //Toolkit class returns the default toolkit
Dimension d = tk.getScreenSize();
//Dimension class object stores width & height of the toolkit screen
// toolkit.getScreenSize() determines the size of the screen
Rectangle rec = new Rectangle(0, 0, d.width, d.height);
//Creates a Rectangle with screen dimensions,
Robot ro = new Robot(); //to capture the screen image
BufferedImage img = ro.createScreenCapture(rec);
File f;
f = new File("myimage.jpg"); // File class is used to write the above generated buffered image to a file
ImageIO.write(img, "jpg", f);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
IMHO it is better to make an image of your component (a JFrame is also a Component):
BufferedImage img = new BufferedImage(yourComponent.getWidth(), yourComponent.getHeight(), BufferedImage.TYPE_INT_RGB);
yourComponent.paint(img.getGraphics());
File outputfile = new File("saved.png");
ImageIO.write(img, "png", outputfile);

Creating image of a panel just creates a panel image rather then the stuff present on the panel

I am writing a code to make an image file of a chart appearing on a panel. For that purpose I create the buffered image of that and then use ImageIO.write(). It works but it only displays the panel(grey coloured panel) but does not show the chart present on that panel. What to do in this case?? Here is my code
com.objectplanet.chart.NonFlickerPanel p =
new com.objectplanet.chart.NonFlickerPanel(new BorderLayout());
p.add("Center", chart); // this statements adds the chart in the center of the panel
ChartPanel.add("Center", p);
ChartPanel.setSize(500, 200);
ChartPanel.show();
int w = ChartPanel.getWidth();
int h = ChartPanel.getHeight();
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
ChartPanel.paint(g);
ChartPanel.printAll(g);
File f = new File("D:\\image.png");
try {
// png is an image format (like gif or jpg)
ImageIO.write(bi, "png", f);
} catch (IOException ex) {
ex.printStackTrace();
}
Well i solved the problem .Anyone facing the same problem ,here is the solution
Use paintall function rather than just paint function

Custom Cursor Icon is loaded with an extra shaded pixel

I changed the cursor Icon whenever a certain toggle button is clicked. But the loaded image contains an extra pixel in it at the bottom corner! It's annoying, like there's constant dirt on the screen. I created the cursor icon using junior icon editor. When I open the picture using windows photo viewer or photoshop, the pixel doesn't manifest itself. It only shows when I use it in the application.
The application is a Java application, and this is how I set the cursor.
Image img = getResourceMap().getImageIcon( iconFilename ).getImage();
Cursor newCursor = Toolkit.getDefaultToolkit().createCustomCursor( img,
new Point( 5, 5 ), "cursor" );
Does anyone know what could be the possible reasons for this extra shaded pixel? It occurs right below the east-facing arrow, about 2 milimeters below it.
You can see the effect running this code. The image appears as expected in a label, but as a pointer there is a darkened pixel on the lower left.
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.imageio.ImageIO;
import java.net.URL;
class ShowImage {
public static void main(String[] args) throws Exception {
URL url = new URL("http://i.stack.imgur.com/kP1jv.png");
final BufferedImage img = ImageIO.read(url);
System.out.println(
"Image is: " + img.getWidth() + "x" + img.getHeight());
SwingUtilities.invokeLater( new Runnable() {
public void run() {
JLabel l = new JLabel(new ImageIcon(img));
l.setBorder(new EmptyBorder(5,5,5,5));
l.setOpaque(true);
l.setBackground(Color.GREEN.darker());
Cursor newCursor = Toolkit.getDefaultToolkit().
createCustomCursor( img,new Point( 5, 5 ), "c" );
l.setCursor(newCursor);
JOptionPane.showMessageDialog(null, l);
}
});
}
}
At first i thought that we were specifying a bigger image than the system cursor size could handle but than i realized that if we remove the last column(!!) of pixels it worked fine!!
I don't know how to explain this!
BufferedImage originalImg = ImageIO.read(new File("kP1jv.png"));
System.out.println("originalImage is: " + originalImg.getWidth() + "x"
+ originalImg.getHeight());
Dimension d = Toolkit.getDefaultToolkit().getBestCursorSize(
originalImg.getWidth(), originalImg.getHeight());
final BufferedImage img = originalImg.getSubimage(0, 0, d.width/*-1*/, d.height-1);

How to select a portion of an image and save just that portion to a file

I am taking a screenshot of the current screen then saving the image. I want to open that image up and be able to select a box of a certain element or whatever it is i want the pic to be of and to be able to in turn save that smaller selected image to
a file. Please help.
RemoteControlConfiguration config = new RemoteControlConfiguration();
config.setPort(4447);
SeleniumServer server = new SeleniumServer(config);
try{
// TODO Auto-generated method stub
server.start();
DefaultSelenium selenium = new DefaultSelenium("localhost", 4447, "*firefox", "http://www.google.com/");
selenium.start();
selenium.open("http://www.google.com/");
selenium.waitForPageToLoad("10000");
selenium.windowMaximize();
BufferedImage image1 = Screenshot("screen1.jpg");
//selenium.type("q", "Hello world");
Thread.sleep(2000);
BufferedImage image2 = Screenshot("screen2.jpg");
public static BufferedImage Screenshot(String fileName) throws Exception
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
File file = new File(fileName);
ImageIO.write(image, "jpg", file);
return image;
}
Assuming you know the coordinates of your new bounds, create a new BufferedImage with the new size, create a graphics object for your new image, and paint the big image on this graphics object, specifying negative values for the x,y. The source image is bigger than the destination, so only the bits that fit within the destination will be written. Then you save out the smaller one using ImageIO.write()
EDIT
Thanks to Andrew Thompson for the suggestion to use subImage
BufferedImage image1 = Screenshot("screen1.jpg");
BufferedImage subImage = image1.getSubImage(x, y, width, height);

Categories