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:com.zacwolf.commons.email.Email.java

public static BufferedImage makeRoundedFooter(int width, int cornerRadius, Color bgcolor, Color border)
        throws Exception {
    int height = (cornerRadius * 2) + 10;
    BufferedImage output = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = output.createGraphics();
    g2.setComposite(AlphaComposite.Src);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(bgcolor);//ww  w . j  a  v  a  2s.  c o  m
    g2.fillRoundRect(0, 0, width, height - 1, cornerRadius, cornerRadius);
    g2.setComposite(AlphaComposite.SrcOver);
    if (border != null) {
        g2.setColor(border);
        g2.drawRoundRect(0, 0, width - 1, height - 2, cornerRadius, cornerRadius);
    }
    g2.dispose();
    Rectangle clip = createClip(output, new Dimension(width, cornerRadius), 0, height - cornerRadius - 1);
    return output.getSubimage(clip.x, clip.y, clip.width, clip.height);
}

From source file:com.cognifide.aet.job.common.collectors.screen.ScreenCollector.java

private byte[] getImagePart(byte[] fullPage, WebElement webElement) throws IOException, ProcessingException {
    InputStream in = new ByteArrayInputStream(fullPage);
    try {//  w  w  w.  j  a v  a 2s  . c om
        BufferedImage fullImg = ImageIO.read(in);
        Point point = webElement.getLocation();
        Dimension size = webElement.getSize();
        BufferedImage screenshotSection = fullImg.getSubimage(point.getX(), point.getY(), size.getWidth(),
                size.getHeight());
        return bufferedImageToByteArray(screenshotSection);
    } catch (IOException e) {
        throw new ProcessingException("Unable to create image from taken screenshot", e);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java

/**
 * Manipulate image raw data and stream them back
 *
 * @param data     raw image data/*  w  w w . j  a v a2  s.c  om*/
 * @param out      stream
 * @param callback optional callback to set mimetype and size
 * @param mimeType mimetype
 * @param selector operations to apply
 * @throws FxApplicationException on errors
 */
@SuppressWarnings({ "UnusedAssignment" })
public static void streamingManipulate(byte[] data, OutputStream out, BinaryDownloadCallback callback,
        String mimeType, FxMediaSelector selector) throws FxApplicationException {
    try {
        ImageFormat format = FxMediaNativeEngine.getImageFormatByMimeType(mimeType);

        BufferedImage bufferedImage;

        if (format == ImageFormat.IMAGE_FORMAT_JPEG) {
            //need ImageIO for jpegs
            ByteArrayInputStream bis = new ByteArrayInputStream(data);
            bufferedImage = ImageIO.read(bis);
            bis = null;
        } else {
            try {
                bufferedImage = Sanselan.getBufferedImage(data);
            } catch (ImageReadException e) {
                //might not be supported, try ImageIO
                ByteArrayInputStream bis = new ByteArrayInputStream(data);
                bufferedImage = ImageIO.read(bis);
                bis = null;
            }
        }

        //perform requested operations
        if (selector.isCrop())
            bufferedImage = bufferedImage.getSubimage((int) selector.getCrop().getX(),
                    (int) selector.getCrop().getY(), (int) selector.getCrop().getWidth(),
                    (int) selector.getCrop().getHeight());
        if (selector.isFlipHorizontal())
            bufferedImage = flipHorizontal(bufferedImage);
        if (selector.isFlipVertical())
            bufferedImage = flipVertical(bufferedImage);
        if (selector.isScaleHeight() || selector.isScaleWidth())
            bufferedImage = scale(bufferedImage,
                    selector.isScaleWidth() ? selector.getScaleWidth() : bufferedImage.getWidth(),
                    selector.isScaleHeight() ? selector.getScaleHeight() : bufferedImage.getHeight());
        if (selector.getRotationAngle() != 0)
            bufferedImage = rotate(bufferedImage, selector.getRotationAngle());

        if (callback != null) {
            ByteArrayOutputStream _out = new ByteArrayOutputStream(data.length);
            boolean fallback = false;
            try {
                fallback = !ImageIO.write(bufferedImage, format.extension, _out);
            } catch (Exception e) {
                fallback = true;
            }
            if (fallback)
                Sanselan.writeImage(bufferedImage, _out, format, new HashMap(0));
            byte[] _newData = _out.toByteArray();
            ImageInfo imageInfo = Sanselan.getImageInfo(_newData);
            callback.setMimeType(imageInfo.getMimeType());
            callback.setBinarySize(_newData.length);
            out.write(_newData);
        } else {
            Sanselan.writeImage(bufferedImage, out, format, new HashMap(0));
        }
    } catch (Exception e) {
        throw new FxApplicationException(e, "ex.media.manipulate.error", e.getMessage());
    }
}

From source file:com.joliciel.jochre.search.highlight.ImageSnippet.java

public BufferedImage getImage() {
    try {/*w w w.j  a  v a  2 s .co m*/
        BufferedImage originalImage = ImageIO.read(imageFile);
        BufferedImage imageSnippet = new BufferedImage(this.rectangle.getWidth(), this.rectangle.getHeight(),
                BufferedImage.TYPE_INT_ARGB);
        originalImage = originalImage.getSubimage(this.rectangle.getLeft(), this.rectangle.getTop(),
                this.rectangle.getWidth(), this.rectangle.getHeight());
        Graphics2D graphics2D = imageSnippet.createGraphics();
        graphics2D.drawImage(originalImage, 0, 0, this.rectangle.getWidth(), this.rectangle.getHeight(), null);
        int extra = 2;
        for (Rectangle rect : this.highlights) {
            graphics2D.setStroke(new BasicStroke(1));
            graphics2D.setPaint(Color.BLACK);
            graphics2D.drawRect(rect.getLeft() - this.rectangle.getLeft() - extra,
                    rect.getTop() - this.rectangle.getTop() - extra, rect.getWidth() + (extra * 2),
                    rect.getHeight() + (extra * 2));
            graphics2D.setColor(new Color(255, 255, 0, 127));
            graphics2D.fillRect(rect.getLeft() - this.rectangle.getLeft() - extra,
                    rect.getTop() - this.rectangle.getTop() - extra, rect.getWidth() + (extra * 2),
                    rect.getHeight() + (extra * 2));
        }
        return imageSnippet;
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:cn.z.Ocr5.java

public static List<BufferedImage> splitImage(BufferedImage img, String filename) 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 (CommonUtil.isWhite(img.getRGB(x, y), whiteThreshold) == 0) {
                count++;//from w  w  w. j  av  a2s .  co m
            }
        }
        weightlist.add(count);
    }
    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(CommonUtil.removeBlank(img.getSubimage(i - length, 0, length / 2, height),
                    whiteThreshold, 0));
            subImgs.add(CommonUtil.removeBlank(img.getSubimage(i - length / 2, 0, length / 2, height),
                    whiteThreshold, 0));
        } else if (length > 5) {
            subImgs.add(
                    CommonUtil.removeBlank(img.getSubimage(i - length, 0, length, height), whiteThreshold, 0));
        }
    }

    //        for(int i = 0; i < subImgs.size(); i++) {
    //            FileOutputStream fos = new FileOutputStream("D:\\test\\img" + filename + i + ".jpg");
    //            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
    //            encoder.encode(subImgs.get(i));
    //            fos.close();
    //        }
    return subImgs;
}

From source file:com.salesmanager.core.util.ProductImageUtil.java

public File getCroppedImage(File originalFile, int x1, int y1, int width, int height) throws Exception {

    BufferedImage image = ImageIO.read(originalFile);
    BufferedImage out = image.getSubimage(x1, y1, width, height);
    File tempFile = File.createTempFile("temp", ".jpg");
    tempFile.deleteOnExit();// ww w  . j a  v  a2  s. c o m
    ImageIO.write(out, "jpg", tempFile);
    return tempFile;
}

From source file:net.cloudkit.relaxation.VerifyImage.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 w  w.  ja  v a2  s  .  com*/
            }
        }
        weightList.add(count);
    }
    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 > 5) {
            subImgs.add(removeBlank(img.getSubimage(i - length, 0, length, height), whiteThreshold, 0));
        }
    }

    return subImgs;
}

From source file:net.mindengine.galen.api.PageDump.java

public void exportAllScreenshots(Browser browser, File reportFolder) throws IOException {

    File screenshotOriginalFile = browser.createScreenshot();

    FileUtils.copyFile(screenshotOriginalFile,
            new File(reportFolder.getAbsolutePath() + File.separator + "page.png"));

    BufferedImage image = Rainbow4J.loadImage(screenshotOriginalFile.getAbsolutePath());

    File objectsFolder = new File(reportFolder.getAbsolutePath() + File.separator + "objects");
    objectsFolder.mkdirs();/* w w w. j a  v a 2  s . co  m*/

    for (Element element : items.values()) {
        if (element.hasImage) {
            int[] area = element.getArea();

            try {
                BufferedImage subImage = image.getSubimage(area[0], area[1], area[2], area[3]);
                Rainbow4J.saveImage(subImage, new File(
                        objectsFolder.getAbsolutePath() + File.separator + element.getObjectName() + ".png"));
            } catch (Exception ex) {
                LOG.error("Got error during saving image", ex);
            }
        }
    }
}

From source file:com.aimluck.eip.fileupload.util.FileuploadUtils.java

/**
 * ???//from   w  w  w  .j  av  a  2 s  . c o m
 * 
 * @param imgfile
 * @param width
 * @param height
 * @return
 */
public static BufferedImage shrinkAndTrimImage(BufferedImage imgfile, int width, int height) {
    int iwidth = imgfile.getWidth();
    int iheight = imgfile.getHeight();
    double ratio = Math.max((double) width / (double) iwidth, (double) height / (double) iheight);

    int shrinkedWidth;
    int shrinkedHeight;

    if ((iwidth <= width) || (iheight < height)) {
        shrinkedWidth = iwidth;
        shrinkedHeight = iheight;
    } else {
        shrinkedWidth = (int) (iwidth * ratio);
        shrinkedHeight = (int) (iheight * ratio);
    }

    // ??
    Image targetImage = imgfile.getScaledInstance(shrinkedWidth, shrinkedHeight, Image.SCALE_AREA_AVERAGING);

    int w_size = targetImage.getWidth(null);
    int h_size = targetImage.getHeight(null);
    if (targetImage.getWidth(null) < width) {
        w_size = width;
    }
    if (targetImage.getHeight(null) < height) {
        h_size = height;
    }
    BufferedImage tmpImage = new BufferedImage(w_size, h_size, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = tmpImage.createGraphics();
    g.setBackground(Color.WHITE);
    g.setColor(Color.WHITE);
    // ??????????????
    g.fillRect(0, 0, w_size, h_size);
    int diff_w = 0;
    int diff_h = 0;
    if (width > shrinkedWidth) {
        diff_w = (width - shrinkedWidth) / 2;
    }
    if (height > shrinkedHeight) {
        diff_h = (height - shrinkedHeight) / 2;
    }
    g.drawImage(targetImage, diff_w, diff_h, null);

    int _iwidth = tmpImage.getWidth();
    int _iheight = tmpImage.getHeight();
    BufferedImage _tmpImage;
    if (_iwidth > _iheight) {
        int diff = _iwidth - width;
        _tmpImage = tmpImage.getSubimage(diff / 2, 0, width, height);
    } else {
        int diff = _iheight - height;
        _tmpImage = tmpImage.getSubimage(0, diff / 2, width, height);
    }
    return _tmpImage;
}

From source file:de.anycook.upload.UploadHandler.java

/**
 * speichert eine kleine Version des Bildes
 *
 * @param image    BufferedImage//from w  w  w .j a v a 2s.  c om
 * @param filename Name der zu erzeugenden Datei
 */
private void saveSmallImage(BufferedImage image, String filename) throws IOException {
    int height = image.getHeight();
    int width = image.getWidth();
    double imageRatio = (double) width / (double) height;

    int xtranslate = 0;
    int ytranslate = 0;

    if (imageRatio > 1) {
        xtranslate = (width - height) / 2;
    } else {
        ytranslate = (height - width) / 2;
    }

    BufferedImage tempImage = image.getSubimage(xtranslate, ytranslate, width - xtranslate * 2,
            height - ytranslate * 2);
    BufferedImage newImage = new BufferedImage(smallSize, smallSize, BufferedImage.TYPE_INT_RGB);
    newImage.getGraphics().drawImage(tempImage.getScaledInstance(smallSize, smallSize, Image.SCALE_SMOOTH), 0,
            0, null);

    imageSaver.save("small/", filename, newImage);
}