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:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage original = new BufferedImage(100, 100, BufferedImage.TYPE_3BYTE_BGR);

    BufferedImage copy = original.getSubimage(10, 10, 50, 50);

    ImageIO.write(copy, "png", new File("Test.png"));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");
    final BufferedImage image = ImageIO.read(url);
    int x = 50;/*from  www. ja  v a2  s  . com*/
    final Image crop = image.getSubimage(x, 0, image.getWidth() - x, image.getHeight());
    Runnable r = new Runnable() {
        @Override
        public void run() {
            JPanel gui = new JPanel();
            gui.add(new JLabel(new ImageIcon(image)), BorderLayout.LINE_START);
            gui.add(new JLabel("java2s.com"));
            gui.add(new JLabel(new ImageIcon(crop)), BorderLayout.LINE_END);
            JOptionPane.showMessageDialog(null, gui);
        }
    };
    SwingUtilities.invokeLater(r);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Robot robot = new Robot();
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    BufferedImage image = robot.createScreenCapture(new Rectangle(d));
    BufferedImage sub = image.getSubimage(0, 0, 400, 400);
    File f = new File("SubImage.png");
    ImageIO.write(sub, "png", f);
    final ImageIcon im = new ImageIcon(f.toURI().toURL());

    Runnable r = new Runnable() {

        @Override//from  ww  w  .ja  va  2  s  . c o  m
        public void run() {
            JOptionPane.showMessageDialog(null, new JLabel(im));
        }
    };
    SwingUtilities.invokeLater(r);
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    BufferedImage image = ImageIO.read(new File("E:/Java_Dev/plasma.gif"));

    // crop image
    BufferedImage firstHalf = image.getSubimage(0, 0, (image.getWidth() / 2), image.getHeight());
    BufferedImage secondHalf = image.getSubimage(image.getWidth() / 2, 0, image.getWidth() / 2,
            image.getHeight());/*from ww w.jav a 2s .co  m*/

    File croppedFile1 = new File("E:/Java_Dev/half1.png");
    File croppedFile2 = new File("E:/Java_Dev/half2.png");

    ImageIO.write(firstHalf, "png", croppedFile1);
    ImageIO.write(secondHalf, "png", croppedFile2);

    // join image
    BufferedImage joined = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
    BufferedImage image1 = ImageIO.read(new File("E:/Java_Dev/half1.png"));
    BufferedImage image2 = ImageIO.read(new File("E:/Java_Dev/half2.png"));

    Graphics2D graph = joined.createGraphics();
    graph.drawImage(image1, 0, 0, null);
    graph.drawImage(image2, image1.getWidth(), 0, null);

    File joinedFile = new File("E:/Java_Dev/joined.png");
    ImageIO.write(joined, "png", joinedFile);
}

From source file:com.timothyyip.face.FaceDetector.java

public static void main(String[] args) throws ImageReadException, IOException, ImageWriteException {

    Detector detector = new Detector("C:\\Code\\Java\\FaceDetector\\lib\\haarcascade_frontalface_default.xml");

    flickr.photoservice.flickrresponse.Rsp response = getFlickrPhotos();

    for (Photo photo : response.getPhotos().getPhoto()) {
        String timestamp = String.valueOf(Calendar.getInstance().getTimeInMillis());
        BufferedImage originalImage = getPhysicalFlickrImage(photo);

        if (originalImage != null) {
            File originalImageFile = new File(
                    "C:\\Code\\Java\\FaceDetector\\images\\original\\" + timestamp + ".jpg");

            //Sanselan.writeImage(originalImage, originalImageFile, ImageFormat.IMAGE_FORMAT_JPEG, null);
            ImageIO.write(originalImage, "JPG", originalImageFile);
            List<Rectangle> res = detector.getFaces(originalImageFile.getAbsolutePath(), 3, 1.25f, 0.1f, 1,
                    false);/*ww w  . j  ava2  s .co m*/

            for (Rectangle face : res) {
                BufferedImage faceImage = originalImage.getSubimage(face.x, face.y, face.width, face.height);

                Sanselan.writeImage(faceImage,
                        new File("C:\\Code\\Java\\FaceDetector\\images\\"
                                + String.valueOf(Calendar.getInstance().getTimeInMillis()) + ".png"),
                        ImageFormat.IMAGE_FORMAT_PNG, null);
            }

        }
    }
}

From source file:Main.java

public static BufferedImage getSubImage(BufferedImage image, int x, int y, int w, int h) {
    return image.getSubimage(x, y, w, h);
}

From source file:Main.java

/**
 * Crops (returns subimage) of specified input image at specified points.
 *
 * @param image image to crop/*w w w  .j a  v  a2 s  .c o  m*/
 * @param x1 top left x coordinate
 * @param y1 top left y coordinate
 * @param  x2 bottom right x coordinate
 * @param  y2 bottom right y coordinate
 *
 * @return image croped at specified points
 */
public static BufferedImage cropImage(BufferedImage image, int x1, int y1, int x2, int y2) {
    return image.getSubimage(x1, y1, x2 - x1, y2 - y1);
}

From source file:com.spstudio.common.image.ImageUtils.java

public static String cutImage(String sourcePath, String targetPath, int x, int y, int width, int height)
        throws IOException {
    File imageFile = new File(sourcePath);
    if (!imageFile.exists()) {
        throw new IOException("Not found the images:" + sourcePath);
    }/*  w  w w . ja  va  2  s . co m*/
    if (targetPath == null || targetPath.isEmpty()) {
        targetPath = sourcePath;
    }
    String format = sourcePath.substring(sourcePath.lastIndexOf(".") + 1, sourcePath.length());
    BufferedImage image = ImageIO.read(imageFile);
    image = image.getSubimage(x, y, width, height);
    ImageIO.write(image, format, new File(targetPath));
    return targetPath;
}

From source file:com.jaeksoft.searchlib.util.ImageUtils.java

public final static BufferedImage getSubImage(BufferedImage image, Rectangle rectangle) {
    return image.getSubimage(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}

From source file:com.openkm.util.ImageUtils.java

/**
 * crop/*from   w  w  w  . j av  a  2s .c  o m*/
 */
public static byte[] crop(byte[] img, int x, int y, int width, int height) throws RuntimeException {
    log.debug("crop({}, {}, {}, {}, {})", new Object[] { img.length, x, y, width, height });
    ByteArrayInputStream bais = new ByteArrayInputStream(img);
    byte[] imageInByte;

    try {
        BufferedImage image = ImageIO.read(bais);
        BufferedImage croppedImage = image.getSubimage(x, y, width, height);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(croppedImage, "png", baos);
        baos.flush();
        imageInByte = baos.toByteArray();
        IOUtils.closeQuietly(baos);
    } catch (IOException e) {
        log.error(e.getMessage());
        throw new RuntimeException("IOException: " + e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(bais);
    }

    log.debug("crop: {}", imageInByte.length);
    return imageInByte;
}