为了账号安全,请及时绑定邮箱和手机立即绑定

openjdk8 - 如何在 BufferedImage 上设置 BufferedImage.

openjdk8 - 如何在 BufferedImage 上设置 BufferedImage.

qq_花开花谢_0 2023-05-17 16:09:16
我有一个来自图像子部分的第一个 BufferedImage。我想设置BufferedImage imageType为BufferedImage.TYPE_3BYTE_BGR使用openjdk8。    final byte[] screenshot = ((TakesScreenshot) Context.getDriver()).getScreenshotAs(OutputType.BYTES);    FileUtils.forceMkdir(new File(System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER));    InputStream in = new ByteArrayInputStream(screenshot);    BufferedImage fullImg = ImageIO.read(in);    // Get the location of element on the page    Point point = element.getLocation();    // Get width and height of the element    int eleWidth = element.getSize().getWidth();    int eleHeight = element.getSize().getHeight();    // Crop the entire page screenshot to get only element screenshot    BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);    // ????? BufferedImage bi = new BufferedImage(eleScreenshot.getWidth(), eleScreenshot.getHeight(), BufferedImage.TYPE_3BYTE_BGR);    ImageIO.write(eleScreenshot, "jpg", new File(System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER + File.separator + screenName + ".jpg");
查看完整描述

1 回答

?
翻翻过去那场雪

TA贡献2065条经验 获得超13个赞

我通过对象将BufferedImage构造函数与BufferedImage.TYPE_3BYTE_BGR+数据副本一起使用Graphics。


BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);

ImageIO.write(convertType(eleScreenshot, BufferedImage.TYPE_3BYTE_BGR), "jpg", new File(System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER + File.separator + screenName + ".jpg"));


 private BufferedImage convertType(BufferedImage eleScreenshot, int type) {

    BufferedImage bi = new BufferedImage(eleScreenshot.getWidth(), eleScreenshot.getHeight(), type);

    Graphics g = bi.getGraphics();

    g.drawImage(eleScreenshot, 0, 0, null);

    g.dispose();

    return bi;

}


查看完整回答
反对 回复 2023-05-17
  • 1 回答
  • 0 关注
  • 174 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信