整理文档,搜刮出一个java实现切图并且判断图片是否是纯色/彩色图片的代码,稍微整理精简一下做下分享。
首先上切图的代码
/** * 图片剪裁 * @param x 距离左上角的x轴距离 * @param y 距离左上角的y轴距离 * @param width 宽度 * @param height 高度 * @param sourcePath 图片源 * @param descpath 目标位置 */ publicstaticvoidimageCut(intx,inty,intwidth,intheight, String sourcePath, String descpath) { FileInputStream is =null; ImageInputStream iis =null; try{ is =newFileInputStream(sourcePath); String fileSuffix = sourcePath.substring(sourcePath.lastIndexOf(".") +1); Iteratorit = ImageIO.getImageReadersByFormatName(fileSuffix); ImageReader reader = it.next(); iis = ImageIO.createImageInputStream(is); reader.setInput(iis,true); ImageReadParam param = reader.getDefaultReadParam(); Rectangle rect =newRectangle(x, y, width, height); param.setSourceRegion(rect); BufferedImage bi = reader.read(0, param); ImageIO.write(bi, fileSuffix,newFile(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 */ publicstaticbooleanisSimpleColorImg(String imgPath,floatpercent)throwsIOException{ BufferedImage src=ImageIO.read(newFile(imgPath)); intheight=src.getHeight(); intwidth=src.getWidth(); intcount=0,pixTemp=0,pixel=0; for(inti=0;i=percent)//如果连续相同的像素点大于设定的百分比的话,就判定为是纯色的图片 returntrue; pixTemp=pixel; } } returnfalse; }
以上为本人用来判断纯色的代码,逻辑比较简单,具体还要看需求来决定
如果是判断彩色的话,可以试试如下逻辑:
1、如果有N个像素点各不相同的话可以判定为彩色
2、如果图片上有>=N种像素点的话,判断为彩色图片
敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载像素火影骨架佐助 (Perseverance Fire Shadow)手机版v1.16
下载要塞英雄 安卓版v33.20.0-39082670-Android
下载梦想城镇vivo最新版本 安卓版v12.0.1
梦想城镇vivo版是这款卡通风模拟经营类手游的渠道服版本,玩
怦然心动的瞬间 安卓版v1.0
怦然心动的瞬间是一款真人向的恋爱互动游戏,在游戏中玩家将扮演
曼尼汉堡店游戏 安卓版v1.0.3
曼尼汉堡店是一款非常好玩的精品恐怖类型冒险游戏,在这款游戏中
现代总统模拟器去广告版 安卓版v1.0.46
现代总统模拟器是一款休闲养成类游戏,可能对于不少的玩家来说都
现代总统模拟器付费完整版 安卓版v1.0.46
现代总统模拟器高级版在商店是需要付费的,相对于普通版本,高级