if_zero_equals_one
if_zero_equals_one

Reputation: 1774

How can I get a less pixelated screenshot in java?

I am using a BufferedImage in java after capturing the image out of a JFrame. I need some way to sharpen the image such that when it's enlarged it doesn't look so pixelated. Here's the thing it has to keep the same image size.

Here's the code I'm using to capture the image.

    private void grabScreenShot() throws Exception
    {
        BufferedImage image = (BufferedImage)createImage(getSize().width, getSize().height);
        paint(image.getGraphics());
        try{
            //ImageIO.write(image, "jpg", new File(TimeTable.path+"\\TimeLine.jpg"));
            ImageIO.write(image, "jpg", new File("C:\\Users\\"+TimeTable.user+"\\AppData\\TimeLineMacroProgram\\TimeLine.jpg"));
            //ImageIO.write(image, "jpg", new File("C:\\ProgramData\\TimeLineMacroProgram\\TimeLine.jpg"));
            System.out.println("Image was created");
        }
        catch (IOException e){
            System.out.println("Had trouble writing the image.");
            throw e;
        }
    }

And here's the image it creates enter image description here

Upvotes: 2

Views: 662

Answers (2)

BalusC
BalusC

Reputation: 1108537

JPG is ill suited for screenshots. It's designed for complex and colorful pictures wherein information loss during compression is nearly negligible, such as photos. For screenshots you should rather be using GIF or, better, PNG.

ImageIO.write(image, "png", new File("C:\\Users\\"+TimeTable.user+"\\AppData\\TimeLineMacroProgram\\TimeLine.png"));

You only end up with a bigger file, but you get pixelperfect sharpness and detail back, which is simply impossible with JPG.

Upvotes: 4

Darien
Darien

Reputation: 3592

I think it's because you're writing it out as a JPEG file.

I'd change the format to something non-lossy, or else force the writer to not use compression by accessing and changing its ImageWriteParam.

Upvotes: 1

Related Questions