Java切图或者判断是不是纯色图片

一、切图代码:

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

/**
 * 图片剪裁
 * @param x 距离左上角的x轴距离
 * @param y 距离左上角的y轴距离
 * @param width 宽度
 * @param height 高度
 * @param sourcePath 图片源
 * @param descpath 目标位置
 */ 
public class printt {
	public static void imageCut(int x, int y, int width, int height, String sourcePath, String descpath) {
		FileInputStream is = null;
		ImageInputStream iis = null;
		try {
			is = new FileInputStream(sourcePath);
			String fileSuffix = sourcePath.substring(sourcePath.lastIndexOf(".") + 1);
			Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(fileSuffix);
			ImageReader reader = it.next();
			iis = ImageIO.createImageInputStream(is);
			reader.setInput(iis, true);
			ImageReadParam param = reader.getDefaultReadParam();
			Rectangle rect = new Rectangle(x, y, width, height);
			param.setSourceRegion(rect);
			BufferedImage bi = reader.read(0, param);
			ImageIO.write(bi, fileSuffix, new File(descpath));
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				is = null;
			}
			if (iis != null) {
				try {
					iis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				iis = null;
			}
		}
	}
	
	public static void main(String args[]) {
		imageCut(500,300,300,500,"f://12.jpg","f://13.jpg");
	}
}

示例图片:
12.jpg
切图结果:
13.jpg
注意:如果不关闭流的话可能会影响其他代码对图片的操作,尤其是删除等操作
 

二、判断是否是纯色图片的代码,稍微改一下可以用来判断是不是彩色图片:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * 判断是否为纯色
 * @param imgPath 图片源
 * @param percent 纯色百分比,即大于此百分比为同一种颜色则判定为纯色,范围[0-1]
 * @return
 * @throws IOException
 */ 
public class printt {
	public static boolean isSimpleColorImg(String imgPath, float percent) throws IOException {
		BufferedImage src = ImageIO.read(new File(imgPath));
		int height = src.getHeight();
		int width = src.getWidth();
		int count = 0, pixTemp = 0, pixel = 0;
		for (int i = 0; i < width; i++) {
			for (int j = 0; j < height; j++) {
				pixel = src.getRGB(i, j);
				if (pixel == pixTemp) // 如果上一个像素点和这个像素点颜色一样的话,就判定为同一种颜色
					count++;
				else
					count = 0;
				if ((float) count / (height * width) >= percent) // 如果连续相同的像素点大于设定的百分比的话,就判定为是纯色的图片
					return true;
				pixTemp = pixel;
			}
		}
		return false;
	}

	public static void main(String args[]) throws IOException {
		System.out.println(isSimpleColorImg("f://14.jpg", 0.99f));
	}
}

示例图片:
14.jpg
运行代码结果为:true
 
参考: https://www.jb51.net/article/121231.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小强签名设计

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值