java.awt.image.CropImageFilter Java Examples

The following examples show how to use java.awt.image.CropImageFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ImageUtil.java    From imageServer with Apache License 2.0 6 votes vote down vote up
/**
 * 图像切割(指定切片的宽度和高度)
 * @param bi 原图像
 * @param x 裁剪原图像起点坐标X
 * @param y 裁剪原图像起点坐标Y
 * @param width 目标切片宽度
 * @param height 目标切片高度
 * @return
 */
public static BufferedImage cut(BufferedImage bi,int x, int y, int width, int height) {

    BufferedImage tag = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = tag.createGraphics();
    tag = g2.getDeviceConfiguration().createCompatibleImage(width, height,
            Transparency.TRANSLUCENT);
    g2.dispose();
    g2 = tag.createGraphics();

    int srcWidth = bi.getHeight(); // 源图宽度
    int srcHeight = bi.getWidth(); // 源图高度
    if (srcWidth > 0 && srcHeight > 0) {
        ImageFilter cropFilter = new CropImageFilter(x, y, width, height);
        Image img = Toolkit.getDefaultToolkit().createImage(
                new FilteredImageSource(bi.getSource(),cropFilter));
        g2.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图
        g2.dispose();
    }
    return tag;
}
 
Example #2
Source File: ImageUtils.java    From JavaWeb with Apache License 2.0 6 votes vote down vote up
/**
 * 图像切割(按指定起点坐标和宽高切割)
 * @param srcImg 源图像地址
 * @param outImg 切片后的图像地址
 * @param x 目标切片起点坐标X
 * @param y 目标切片起点坐标Y
 * @param width 目标切片宽度
 * @param height 目标切片高度
 * @param imageType 图片类型
 */
public static void cut(File srcImg, File outImg, int x, int y, int width, int height,String imageType) throws Exception{
	//读取源图像
	BufferedImage bi = ImageIO.read(srcImg);
	int srcWidth = bi.getHeight();//源图宽度
	int srcHeight = bi.getWidth();//源图高度
	if (srcWidth > 0 && srcHeight > 0) {
		Image image = bi.getScaledInstance(srcWidth, srcHeight,Image.SCALE_DEFAULT);
		//四个参数分别为图像起点坐标和宽高
		//即:CropImageFilter(int x,int y,int width,int height)
		ImageFilter cropFilter = new CropImageFilter(x, y, width,height);
		Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));
		BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
		Graphics g = tag.getGraphics();
		g.drawImage(img, 0, 0, width, height, null);//绘制切割后的图
		g.dispose();
		//输出为文件
		ImageIO.write(tag, imageType, outImg);
	}
}
 
Example #3
Source File: ImageUtil.java    From util with Apache License 2.0 5 votes vote down vote up
/**
 * 图像切割(按指定起点坐标和宽高切割)
 * @param srcImageFile 源图像地址
 * @param result 切片后的图像地址
 * @param x 目标切片起点坐标X
 * @param y 目标切片起点坐标Y
 * @param width 目标切片宽度
 * @param height 目标切片高度
 */
public final static void cut(String srcImageFile, String result,
        int x, int y, int width, int height) {
    try {
        // 读取源图像
        BufferedImage bi = ImageIO.read(new File(srcImageFile));
        int srcWidth = bi.getHeight(); // 源图宽度
        int srcHeight = bi.getWidth(); // 源图高度
        if (srcWidth > 0 && srcHeight > 0) {
            Image image = bi.getScaledInstance(srcWidth, srcHeight,
                    Image.SCALE_DEFAULT);
            // 四个参数分别为图像起点坐标和宽高
            // 即: CropImageFilter(int x,int y,int width,int height)
            ImageFilter cropFilter = new CropImageFilter(x, y, width, height);
            Image img = Toolkit.getDefaultToolkit().createImage(
                    new FilteredImageSource(image.getSource(),
                            cropFilter));
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图
            g.dispose();
            // 输出为文件
            ImageIO.write(tag, "JPEG", new File(result));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #4
Source File: HeadlessCropImageFilter.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main (String[] args) {
    new CropImageFilter(20, 20, 40, 50).clone();
}