/**
* 图片剪裁
* @param x 距离左上角的x轴距离
* @param y 距离左上角的y轴距离
* @param width 宽度
* @param height 高度
* @param sourcePath 图片源
* @param descpath 目标位置
*/
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;
}
}
}
/**
* 判断是否为纯色
* @param imgPath 图片源
* @param percent 纯色百分比,即大于此百分比为同一种颜色则判定为纯色,范围[0-1]
* @return
* @throws IOException
*/
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;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有