猿问

openjdk8 - 如何在 BufferedImage 上设置 BufferedImage.

我有一个来自图像子部分的第一个 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");



qq_花开花谢_0
浏览 175回答 1
1回答

翻翻过去那场雪

我通过对象将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;}
随时随地看视频慕课网APP

相关分类

Java
我要回答