Example usage for java.awt.image BufferedImage getSubimage

List of usage examples for java.awt.image BufferedImage getSubimage

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getSubimage.

Prototype

public BufferedImage getSubimage(int x, int y, int w, int h) 

Source Link

Document

Returns a subimage defined by a specified rectangular region.

Usage

From source file:de.ppi.selenium.util.ScreenshotUtils.java

/***
 * You can use this method to take your control pictures. Note that the file
 * should be a png./*from ww w  .  j  a  v  a2 s  .com*/
 *
 * @param element the webelement
 * @param toSaveAs the file where the screenshot should be save.
 * @param wd the webdriver.
 * @throws IOException if something goes wrong writing the result.
 */
public static void takeScreenshotOfElement(WebElement element, File toSaveAs, WebDriver wd) throws IOException {

    for (int i = 0; i < MAXIMAL_NR_OF_RETRIES; i++) { // Loop up to 10x to
                                                      // ensure a clean
                                                      // screenshot was taken
        LOG.info("Taking screen shot of locator " + element + " ... attempt #" + (i + 1));

        // Scroll to element
        // TODO Improvement element.scrollTo();

        // Take picture of the page
        File screenshot;
        boolean isRemote = false;
        if (!(wd instanceof RemoteWebDriver)) {
            screenshot = ((TakesScreenshot) wd).getScreenshotAs(OutputType.FILE);
        } else {
            Augmenter augmenter = new Augmenter();
            screenshot = ((TakesScreenshot) augmenter.augment(wd)).getScreenshotAs(OutputType.FILE);
            isRemote = true;
        }
        BufferedImage fullImage = ImageIO.read(screenshot);

        // Parse out the picture of the element
        Point point = element.getLocation();
        int eleWidth = element.getSize().getWidth();
        int eleHeight = element.getSize().getHeight();
        int x;
        int y;
        if (isRemote) {
            x = ((Locatable) element).getCoordinates().inViewPort().getX();
            y = ((Locatable) element).getCoordinates().inViewPort().getY();
        } else {
            x = point.getX();
            y = point.getY();
        }
        LOG.debug("Screenshot coordinates x: " + x + ", y: " + y);
        BufferedImage eleScreenshot = fullImage.getSubimage(x, y, eleWidth, eleHeight);
        ImageIO.write(eleScreenshot, "png", screenshot);

        // Ensure clean snapshot (sometimes WebDriver takes bad pictures and
        // they turn out all black)
        if (!isBlack(ImageIO.read(screenshot))) {
            FileUtils.copyFile(screenshot, toSaveAs);
            break;
        }
    }
}

From source file:net.cloudkit.relaxation.CaptchaTest.java

public static BufferedImage removeBlank(BufferedImage img, int whiteThreshold, int white) throws Exception {
    final int width = img.getWidth();
    final int height = img.getHeight();
    int start = 0;
    int end = 0;//from  www  .  java2  s .  c  om
    Label1: for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            if (isWhite(img.getRGB(x, y), whiteThreshold) == white) {
                start = y;
                break Label1;
            }
        }
    }
    Label2: for (int y = height - 1; y >= 0; --y) {
        for (int x = 0; x < width; ++x) {
            if (isWhite(img.getRGB(x, y), whiteThreshold) == white) {
                end = y;
                break Label2;
            }
        }
    }
    return img.getSubimage(0, start, width, end - start + 1);
}

From source file:com.galenframework.utils.GalenUtils.java

public static File makeFullScreenshot(WebDriver driver) throws IOException, InterruptedException {
    // scroll up first
    scrollVerticallyTo(driver, 0);/*from   w  w  w  .  j  a v  a2 s  .co  m*/
    byte[] bytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
    BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));
    int capturedWidth = image.getWidth();
    int capturedHeight = image.getHeight();

    long longScrollHeight = (Long) ((JavascriptExecutor) driver).executeScript(
            "return Math.max(" + "document.body.scrollHeight, document.documentElement.scrollHeight,"
                    + "document.body.offsetHeight, document.documentElement.offsetHeight,"
                    + "document.body.clientHeight, document.documentElement.clientHeight);");

    Double devicePixelRatio = ((Number) ((JavascriptExecutor) driver)
            .executeScript(JS_RETRIEVE_DEVICE_PIXEL_RATIO)).doubleValue();

    int scrollHeight = (int) longScrollHeight;

    File file = File.createTempFile("screenshot", ".png");

    int adaptedCapturedHeight = (int) (((double) capturedHeight) / devicePixelRatio);

    BufferedImage resultingImage;

    if (Math.abs(adaptedCapturedHeight - scrollHeight) > 40) {
        int scrollOffset = adaptedCapturedHeight;

        int times = scrollHeight / adaptedCapturedHeight;
        int leftover = scrollHeight % adaptedCapturedHeight;

        final BufferedImage tiledImage = new BufferedImage(capturedWidth,
                (int) (((double) scrollHeight) * devicePixelRatio), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2dTile = tiledImage.createGraphics();
        g2dTile.drawImage(image, 0, 0, null);

        int scroll = 0;
        for (int i = 0; i < times - 1; i++) {
            scroll += scrollOffset;
            scrollVerticallyTo(driver, scroll);
            BufferedImage nextImage = ImageIO.read(
                    new ByteArrayInputStream(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES)));
            g2dTile.drawImage(nextImage, 0, (i + 1) * capturedHeight, null);
        }
        if (leftover > 0) {
            scroll += scrollOffset;
            scrollVerticallyTo(driver, scroll);
            BufferedImage nextImage = ImageIO.read(
                    new ByteArrayInputStream(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES)));
            BufferedImage lastPart = nextImage.getSubimage(0,
                    nextImage.getHeight() - (int) (((double) leftover) * devicePixelRatio),
                    nextImage.getWidth(), leftover);
            g2dTile.drawImage(lastPart, 0, times * capturedHeight, null);
        }

        scrollVerticallyTo(driver, 0);

        resultingImage = tiledImage;
    } else {
        resultingImage = image;
    }

    if (GalenConfig.getConfig().shouldAutoresizeScreenshots()) {
        try {
            resultingImage = GalenUtils.resizeScreenshotIfNeeded(driver, resultingImage);
        } catch (Exception ex) {
            LOG.trace("Couldn't resize screenshot", ex);
        }
    }

    ImageIO.write(resultingImage, "png", file);
    return file;
}

From source file:net.cloudkit.relaxation.CaptchaTest.java

public static List<BufferedImage> splitImage(BufferedImage img) throws Exception {
    final List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
    final int width = img.getWidth();
    final int height = img.getHeight();
    final List<Integer> weightList = new ArrayList<Integer>();
    for (int x = 0; x < width; ++x) {
        int count = 0;
        for (int y = 0; y < height; ++y) {
            if (isWhite(img.getRGB(x, y), whiteThreshold) == 0) {
                count++;//w ww .j  a  v  a2 s  .  c om
            }
        }
        weightList.add(count);
    }
    // System.out.println(weightList.size());
    for (int i = 0; i < weightList.size(); i++) {
        int length = 0;
        while (i < weightList.size() && weightList.get(i) > 0) {
            i++;
            length++;
        }
        if (length > 18) {
            subImgs.add(removeBlank(img.getSubimage(i - length, 0, length / 2, height), whiteThreshold, 0));
            subImgs.add(removeBlank(img.getSubimage(i - length / 2, 0, length / 2, height), whiteThreshold, 0));
        } else if (length > 2) {
            subImgs.add(removeBlank(img.getSubimage(i - length, 0, length, height), whiteThreshold, 0));
        }
    }

    return subImgs;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.ImageUploadThumbnailer.java

private BufferedImage cropImage(BufferedImage image, CropRectangle crop) {
    return image.getSubimage(crop.x, crop.y, crop.width, crop.height);
}

From source file:com.codenvy.corp.MainPage.java

public void gotoManageViewAndGrabCLDIDEBornDown() throws IOException, InterruptedException {
    driver.get(String.format(mangeViewUrlCLDIDE, agileIdPLF));
    new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(burnDownMAinContainer));
    burnDownNamePlf = new WebDriverWait(driver, 10)
            .until(ExpectedConditions.visibilityOfElementLocated(By.id("ghx-items-trigger"))).getText();
    Thread.sleep(10000);//  w ww  .  j a v  a2s.c o m
    File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    Point p = burnDownMAinContainer.getLocation();
    int width = burnDownMAinContainer.getSize().getWidth();
    int height = burnDownMAinContainer.getSize().getHeight();
    BufferedImage img = ImageIO.read(screen);
    BufferedImage dest = img.getSubimage(p.getX(), p.getY() - 50, width, height);
    ImageIO.write(dest, "png", screen);
    File file = new File(burnDownNamePlf + ".png");
    FileUtils.copyFile(screen, file);
    addText(file, burnDownNamePlf);

}

From source file:com.codenvy.corp.MainPage.java

public void gotoManageViewAndGoToIdeBornDown() throws IOException, InterruptedException {
    driver.get(String.format(mangeViewUrlIDE, agileIdIDE));
    new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(burnDownMAinContainer));
    burnDownNameIDE = new WebDriverWait(driver, 10)
            .until(ExpectedConditions.visibilityOfElementLocated(By.id("ghx-items-trigger"))).getText();
    Thread.sleep(10000);/*from ww  w . ja va2s .c  om*/
    File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    Point p = burnDownMAinContainer.getLocation();

    int width = burnDownMAinContainer.getSize().getWidth();
    int height = burnDownMAinContainer.getSize().getHeight();

    BufferedImage img = ImageIO.read(screen);

    BufferedImage dest = img.getSubimage(p.getX(), p.getY() - 50, width, height);

    ImageIO.write(dest, "png", screen);
    File file = new File(burnDownNameIDE + ".png");
    FileUtils.copyFile(screen, file);
    addText(file, burnDownNameIDE);
}

From source file:com.inbook.resource.PngSprite.java

public Image getIcon(BufferedImage source, int width, int height, String iconName) {
    int position = positions.get(iconName);
    BufferedImage subimage = source.getSubimage(0, position * height, width, height);
    return SwingFXUtils.toFXImage(subimage, null);
}

From source file:com.zacwolf.commons.email.Email.java

public static BufferedImage makeRoundedBanner(BufferedImage image, int cornerRadius) {
    int w = image.getWidth();
    int h = image.getHeight() + 10;
    BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = output.createGraphics();
    g2.setComposite(AlphaComposite.Src);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(Color.WHITE);//  w w  w .ja  v  a2s. c  o  m
    g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius));
    g2.setComposite(AlphaComposite.SrcAtop);
    g2.drawImage(image, 0, 0, null);
    g2.setComposite(AlphaComposite.SrcOver);
    //               g2.setColor(new Color(153,153,153));//slight grey border
    //               g2.drawRoundRect(0, 0, w-1, h, cornerRadius, cornerRadius);
    g2.dispose();
    return output.getSubimage(0, 0, image.getWidth(), image.getHeight());
}

From source file:ScreenCapture.java

public boolean crop() {
    if (startPoint.equals(endPoint))
        return true;

    boolean succeeded = true;

    int x1 = (startPoint.x < endPoint.x) ? startPoint.x : endPoint.x;
    int y1 = (startPoint.y < endPoint.y) ? startPoint.y : endPoint.y;

    int x2 = (startPoint.x > endPoint.x) ? startPoint.x : endPoint.x;
    int y2 = (startPoint.y > endPoint.y) ? startPoint.y : endPoint.y;

    int width = (x2 - x1) + 1;
    int height = (y2 - y1) + 1;

    BufferedImage biCrop = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = biCrop.createGraphics();
    BufferedImage bi = (BufferedImage) image;
    BufferedImage bi2 = bi.getSubimage(x1, y1, width, height);
    g2d.drawImage(bi2, null, 0, 0);/* w w w .  j  a v  a  2 s . com*/

    g2d.dispose();

    if (succeeded)
        setImage(biCrop);
    else {
        startPoint.x = endPoint.x;
        startPoint.y = endPoint.y;
        repaint();
    }

    return succeeded;
}