Java Image Cut cropImage(URL url, float x, float y, float w, float h, OutputStream out)

Here you can find the source of cropImage(URL url, float x, float y, float w, float h, OutputStream out)

Description

crop Image

License

Open Source License

Declaration

public static void cropImage(URL url, float x, float y, float w, float h, OutputStream out) throws IOException 

Method Source Code


//package com.java2s;
/*// w w  w. j av  a2 s .c o  m
 * Copyright (C) ${year} Omry Yadan <${email}>
 * All rights reserved.
 *
 * See https://github.com/omry/banana/blob/master/BSD-LICENSE for licensing information
 */

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

import java.net.*;

public class Main {
    public static void cropImage(URL url, float x, float y, float w, float h, OutputStream out) throws IOException {
        InputStream in = url.openStream();
        try {
            cropImage(in, x, y, w, h, out);
        } finally {
            in.close();
        }
    }

    public static void cropImage(String imageName, float x, float y, float w, float h, OutputStream out)
            throws IOException {
        FileInputStream fin = new FileInputStream(imageName);
        try {
            cropImage(fin, x, y, w, h, out);
        } finally {
            fin.close();
        }

    }

    public static void cropImage(InputStream in, float x, float y, float w, float h, OutputStream out)
            throws IOException {
        if (x < 0)
            x = 0;
        if (y < 0)
            y = 0;
        if (x + w > 100)
            w = 100 - x;
        if (y + h > 100)
            h = 100 - y;

        BufferedImage bi = ImageIO.read(in);
        int width = bi.getWidth();
        int height = bi.getHeight();
        BufferedImage cropped = bi.getSubimage((int) (width * x / 100), (int) (height * y / 100),
                (int) (width * w / 100), (int) (height * h / 100));
        ImageIO.write(cropped, "jpeg", out);
    }
}

Related

  1. cropImage(Image image, int x, int y, int width, int height)
  2. cropImage(Image img, int x, int y, int w, int h)
  3. cropImage(Image originalImage, int left, int top, int right, int bottom)
  4. cutImage(BufferedImage image, int posX, int posY, int width, int height)
  5. cutImage(BufferedImage img, int w, int h)
  6. cutImage(File file, int x, int y, int width, int heigth)
  7. cutImage(final BufferedImage bufferedImage, final int targetW, final int targetH)