Black borders around png screenshot - java

Below are the codes i have written to take screenshot of an active window. However, im unsure why there are black borders surrounding the image. Please help and tell me if there is any mistakes in my codes.
Robot robot; //create robot instance
robot = new Robot(); //gets and saves a reference to a new Robot
robot.delay(3000); //delay robot for 3 seconds
//get dimensions of the bounding rectangle of the specified window
WinDef.HWND hwnd = User32.INSTANCE.GetForegroundWindow();
RECT WindowDimensions = new RECT();
//get screen coordinates of upper-left and lower-right corners of the window in dimensionsOfWindow
User32.INSTANCE.GetWindowRect(hwnd, WindowDimensions);
//capture image of only active window
BufferedImage screenFullImage = robot.createScreenCapture(WindowDimensions.toRectangle());
//write buffered image to file
try{
//File f = new File(dir + "\\screenshot " + createTimeStampStr() + ".png"); //file path of output
File f = new File("D:\\screenshot " + createTimeStampStr() + ".png"); //file path of output
ImageIO.write(screenFullImage, "png", f);
}catch(IOException e){
System.out.println("Error: " + e);
}

Related

Why does my screenshot only show my desktop?

I want to take a screenshot of my screen while in Google Chrome but when my screenshot saves its only my desktop? Im on a mac btw
try {
Robot robot = new Robot();
String format = "jpg";
String fileName = "FullScreenshot." + format;
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
ImageIO.write(screenFullImage, format, new File(fileName));
System.out.println("A full screenshot saved!");
} catch (AWTException | IOException ex) {
System.err.println(ex);
}

Printing Image from JLabel in java

I'm writing a code which generate barcode in Image then display this image in label in my form then I want to print this barcode so I wrote this code
Icon icon = lbl.getIcon();
BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
Graphics graphic = bi.createGraphics();
try {
lbl.printAll(graphic);
JOptionPane.showmessage(null, "success");
g.dispose();
} catch(Exception ex) {
JOptionPane.showmessage(null, "Error While Printing : " + ex.getMessage());
}
but I can't try it because I don't have printer, so this code will run correctly or any updates ?

Take screenshot element in selenium on ubuntu wrong position

When I change the environment from Windows 10 to Ubuntu and some function is not working good. Like that function take a screenshot
Please help me find the solution take a screenshot on Ubuntu. When take a screenshot on Ubuntu the picture I think is the wrong position of the element. Thank you
Working for Windows 10
public void captureElementScreenshot(WebElement element, String fileName) {
//Capture entire page screenshot as buffer.
//Used TakesScreenshot, OutputType Interface of selenium and File class of java to capture screenshot of entire page.
File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
//Used selenium getSize() method to get height and width of element.
//Retrieve width of element.
int ImageWidth = element.getSize().getWidth();
//Retrieve height of element.
int ImageHeight = element.getSize().getHeight();
//Used selenium Point class to get x y coordinates of Image element.
//get location(x y coordinates) of the element.
Point point = element.getLocation();
int xcord = point.getX();
int ycord = point.getY();
//Reading full image screenshot.
BufferedImage img = null;
try {
img = ImageIO.read(screen);
BufferedImage dest = img.getSubimage(xcord, ycord, ImageWidth, ImageHeight);
ImageIO.write(dest, "png", screen);
//Used FileUtils class of apache.commons.io.
//save Image screenshot In D: drive.
FileUtils.copyFile(screen, new File(System.getProperty("user.dir") + "//src//test//screenshot//" + fileName + ".png"));
System.out.println("Success save file " + fileName);
} catch (IOException e) {
e.printStackTrace();
}
//cut Image using height, width and x y coordinates parameters.
}
And this config browser
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "//src//test//browser//chromedriver");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("force-device-scale-factor=0.75");
options.addArguments("high-dpi-support=0.75");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
driver.manage().window().maximize();
It's about two line code. I have commented and It's working
options.addArguments("force-device-scale-factor=0.75");
options.addArguments("high-dpi-support=0.75");

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);

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