【问题标题】:Reduce size of byte array image screenshot selenium减小字节数组图像屏幕截图 selenium 的大小
【发布时间】:2020-06-08 09:48:32
【问题描述】:

每次运行后我都会在运行时截取屏幕截图,但我想减小大小以便不占用太多空间:每个屏幕截图平均为 1mb,附有屏幕截图的 200 次测试将只为屏幕截图提供 200mb . 附在诱惑报告中

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

关于如何缩小屏幕截图大小的任何想法?

【问题讨论】:

  • 在返回字节数组之前从驱动中取出截图可以压缩图片
  • 尝试压缩字节数组,如下所示 stackoverflow.com/questions/357851/… 尽管 PNG 已经被压缩,它可能仍然会给你一些额外的压缩。
  • 或将字节数组动态转换为 JPEG,并使用更强的压缩率,如下所示stackoverflow.com/questions/32851036/…
  • @libanban 到底怎么样?
  • 谢谢@alexey.. 会检查并回复

标签: java arrays selenium junit4 allure


【解决方案1】:

终于!找到了解决方案。通过压缩为 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();
    }
    ```

【讨论】:

    猜你喜欢
    • 2021-07-28
    • 2013-02-14
    • 2015-12-28
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 2022-09-28
    • 2018-03-22
    • 1970-01-01
    相关资源
    最近更新 更多