Java BufferedImage Crop ImageCrop(String sourcePath, String descPath, int cropX, int cropY, int width, int height)

Here you can find the source of ImageCrop(String sourcePath, String descPath, int cropX, int cropY, int width, int height)

Description

crop image with certain parameters

License

Apache License

Parameter

Parameter Description
sourcePath a parameter
descPath a parameter
cropX a parameter
cropY a parameter
width a parameter
height a parameter

Declaration

public static void ImageCrop(String sourcePath, String descPath, int cropX, int cropY, int width, int height) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Main {
    /**//from  w  w w  .ja  v  a 2s  . co m
     * crop image with certain parameters
     *
     * @param sourcePath
     * @param descPath
     * @param cropX
     * @param cropY
     * @param width
     * @param height
     */
    public static void ImageCrop(String sourcePath, String descPath, int cropX, int cropY, int width, int height) {
        BufferedImage bufferedImage = readImageFile(sourcePath);
        bufferedImage = doCrop(bufferedImage, cropX, cropY, width, height);
        saveImageFile(bufferedImage, getImageSuffix(sourcePath), descPath);
    }

    private static BufferedImage readImageFile(String path) {
        File imageFile = new File(path);
        try {
            return ImageIO.read(imageFile);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static BufferedImage doCrop(BufferedImage bufferedImage, int cropX, int cropY, int width, int height) {
        return bufferedImage.getSubimage(cropX, cropY, width, height);
    }

    private static void saveImageFile(BufferedImage bufferedImage, String suffix, String path) {
        File imageFile = new File(path);
        try {
            ImageIO.write(bufferedImage, suffix, imageFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String getImageSuffix(String path) {
        return path.substring(path.lastIndexOf(".") + 1);
    }
}

Related

  1. cropToAspectRatio(BufferedImage image, float aspect)
  2. cropToBeSameSize(BufferedImage image1, BufferedImage image2)
  3. divideImage(BufferedImage bi, int rows, int cols, int width, int height)
  4. doCrop(BufferedImage bufferedImage, int cropX, int cropY, int width, int height)
  5. drawCropped(JPanel contentPane, ActionListener listener, BufferedImage img, int type, int sx1, int sy1, int sx2, int sy2, int x, int y, int scale)
  6. imgUtilFastCrop(BufferedImage src, int x, int y, int width, int height)
  7. zealousSubsCrop(BufferedImage img)