0

I have the following java method, which successfully creates the png file:

        TakesScreenshot scrShot = ((TakesScreenshot) webdriver);
        File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);
        File DestFile = new File(fileWithPath + featureFileName + ".png");
//        BufferedImage img = ImageIO.read(SrcFile);
//        ImageIO.write(img, "jpg", new File(fileWithPath + featureFileName + ".jpg"));
        FileUtils.copyFile(SrcFile, DestFile);

I'm trying to convert the image to jpg using the 2 commented lines, but jpg output file is not being produced. No error. No file. I can't figure out why. Thanks in advance for any help.

  • maybe see this thread: https://stackoverflow.com/questions/464825/converting-transparent-gif-png-to-jpeg-using-java You might also consider copying the file first... it'll be a temporary file marked to be deleted. – pcalkins Nov 01 '21 at 21:39

1 Answers1

1

You are likely using OpenJDK that is having number of issues with JPG encoding, especially when you convert from png.

So that your workaround would be to convert image BufferedImage to another BufferedImage and then save it like:

try {
    TakesScreenshot scrShot = ((TakesScreenshot) driver);
    File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);
    BufferedImage pngImage  = ImageIO.read(SrcFile);
    int height = pngImage.getHeight();
    int width = pngImage.getWidth();
    BufferedImage jpgImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
    jpgImage.createGraphics().drawImage(pngImage, new AffineTransform(1f,0f,0f,1f,0,0), null);
    ImageIO.write(jpgImage, "jpg", new File("/your_path/output.jpg"));
} catch (IOException e) {
    e.printStackTrace();
}
Alexey R.
  • 8,057
  • 2
  • 11
  • 27
  • Thanks for the reply. Since we are already using ashot for full page screen shots, I ended up going with that for regular screen shots. That worked fine with minimal code changes. – JustCallMeDan Nov 03 '21 at 16:20