簡體   English   中英

縮小字節數組圖像截圖 selenium

[英]Reduce size of byte array image screenshot selenium

每次運行后我都會在運行中截取屏幕截圖,但想減小大小以免占用太多空間:每個屏幕截圖平均為 1mb,附有屏幕截圖的 200 次測試將只為屏幕截圖提供 200mb。 將其附加到誘惑報告中

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

關於如何縮小屏幕截圖大小的任何想法?

最后。 找到了解決方案:通過壓縮為 jpg,我的報告大小從 70mb 變為 15mb:

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();
    }
    ```

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM