Here you can find the source of ImageCrop(String sourcePath, String descPath, int cropX, int cropY, int width, int height)
Parameter | Description |
---|---|
sourcePath | a parameter |
descPath | a parameter |
cropX | a parameter |
cropY | a parameter |
width | a parameter |
height | a parameter |
public static void ImageCrop(String sourcePath, String descPath, int cropX, int cropY, int width, int height)
//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); } }