1

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?

bahader
  • 11
  • 4
  • 1
    You can compress the image when you take out the screenshot from the driver before you return the byte array – libanbn Jun 04 '20 at 15:44
  • 1
    Try to compress the byte array like shown here https://stackoverflow.com/questions/357851/in-java-how-to-zip-file-from-byte-array despite the PNG is already compressed it might still give you some extra compression. – Alexey R. Jun 04 '20 at 16:08
  • 1
    Or convert the bytes array to JPEG on the fly with stronger compression like shown here https://stackoverflow.com/questions/32851036/converting-png-byte-array-to-jpeg-byte-array-in-java – Alexey R. Jun 04 '20 at 16:11
  • @libanban how exactly? – bahader Jun 05 '20 at 00:17
  • Thanks @alexey.. will check it out and reply – bahader Jun 05 '20 at 01:22

1 Answers1

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();
    }
    ```
bahader
  • 11
  • 4