简体   繁体   中英

Java robot class doesn't work after being exported [Solved][Reason: Window scaling ]

So I have a class that utilizes java's robot class to take a screen picture and make 5 smaller pictures from it.

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;

public class screencap {

    static Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

public ArrayList<BufferedImage> get() throws AWTException, IOException {
    ArrayList<BufferedImage> array = new ArrayList<BufferedImage>();
    Robot robot = new Robot();
    BufferedImage bufferedImage = robot.createScreenCapture(rectangle);
    
         BufferedImage img1 = bufferedImage.getSubimage(481,1039,190,31);
         BufferedImage img2 = bufferedImage.getSubimage(682,1039,190,31);
         BufferedImage img3 = bufferedImage.getSubimage(883,1039,190,31);
         BufferedImage img4 = bufferedImage.getSubimage(1085,1039,190,31);
         BufferedImage img5 = bufferedImage.getSubimage(1286,1039,190,31);
         array.add(img1);
         array.add(img2);
         array.add(img3);
         array.add(img4);
         array.add(img5);     
        return array;


}
}

So what I can do is use the class above to get an array of 5 buffered image like so

  screencap nsc = new screencap(); 
              
            try {
                ArrayList<BufferedImage> bfl = nsc.get();
            } catch (AWTException | IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

The problem that I encountered was that the code worked flawlessly when I ran it on eclipse but when I exported it as an executable jar file it didn't work and couldn't even throw an error. I'm pretty sure the code at fault here is screencap.get() but I don't know how to fix it. Can someone tell me what went wrong? Thanks in advance.

SOLVED: I turned off window scaling. Apparently it messes with my program somehow.

Also, when I execute the program from cmd it works even if window scaling is on @ArnaudClaudel thank you for suggesting I use cmd

The reason my program didn't work is that I have window scaling turned on to 125%. It made my exported program look bigger than it supposed to be, making pictures look blurry and it messes with the function in the question. I fixed it by turning off window scaling. Another solution is to execute the jar file from the cmd.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM