2

I'm taking screenshot in my runs after every run but want to reduce the size so that it doesn't occupy too much space: every screenshot is 1mb on average, having 200 test with screenshot attached will give 200mb only for screenshots. Attaching it to allure report

@Attachment
    public byte[] attachScreenshot() {
        try {
            return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
        } catch (Exception ignore) {return null;}
    }

Any ideas on how to shrink the screenshot size?

5
  • 1
    You can compress the image when you take out the screenshot from the driver before you return the byte array Commented Jun 4, 2020 at 15:44
  • 1
    Try to compress the byte array like shown here stackoverflow.com/questions/357851/… despite the PNG is already compressed it might still give you some extra compression. Commented Jun 4, 2020 at 16:08
  • 1
    Or convert the bytes array to JPEG on the fly with stronger compression like shown here stackoverflow.com/questions/32851036/… Commented Jun 4, 2020 at 16:11
  • @libanban how exactly? Commented Jun 5, 2020 at 0:17
  • Thanks @alexey.. will check it out and reply Commented Jun 5, 2020 at 1:22

1 Answer 1

0

Finally! Found the solution. My report size went from 70mb to 15mb by compressing to jpg:

private static byte[] pngBytesToJpgBytes(byte[] pngBytes) throws IOException {
        //create InputStream for ImageIO using png byte[]
        ByteArrayInputStream bais = new ByteArrayInputStream(pngBytes);
        //read png bytes as an image
        BufferedImage bufferedImage = ImageIO.read(bais);

        BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
                bufferedImage.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);

        //create OutputStream to write prepaired jpg bytes
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //write image as jpg bytes
        ImageIO.write(newBufferedImage, "JPG", baos);

        //convert OutputStream to a byte[]
        return baos.toByteArray();
    }
    ```
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.