/********************************* * @todo 图片放大缩小 * @param srcImg 原始图片 * @param desW 变化后图片的宽 * @param desH 变化后图片的高 * @return 处理后的图片 *********************************/ private Image ZoomImage(Image srcImg, int desW, int desH) { int srcW = srcImg.getWidth(); //原始图像宽 int srcH = srcImg.getHeight(); //原始图像高
short[] srcBuf = new short[srcW * srcH]; //原始图片像素信息缓存
//srcBuf获取图片像素信息 Image desImg = Image.createImage(srcW, srcH); if (srcImg.isMutable()) { /*如果是可变图像*/ DirectUtils.getDirectGraphics(srcImg.getGraphics()). getPixels(srcBuf, 0, srcW, 0, 0, srcW, srcH, 444); } else { /*如果是非可变图像*/ desImg.getGraphics().drawImage(srcImg, 0, 0, 0); DirectUtils.getDirectGraphics(desImg.getGraphics()). getPixels(srcBuf, 0, srcW, 0, 0, srcW, srcH, 444); }
//计算插值表 short[] tabY = new short[desH]; short[] tabX = new short[desW];
int sb = 0; int db = 0; int tems = 0; int temd = 0; int distance = srcH > desH ? srcH : desH; for (int i = 0; i <= distance; i++) { /*垂直方向*/ tabY[db] = (short) sb; tems += srcH; temd += desH; if (tems > distance) { tems -= distance; sb++; } if (temd > distance) { temd -= distance; db++; } }
sb = 0; db = 0; tems = 0; temd = 0; distance = srcW > desW ? srcW : desW; for (int i = 0; i <= distance; i++) { /*水平方向*/ tabX[db] = (short) sb; tems += srcW; temd += desW; if (tems > distance) { tems -= distance; sb++; } if (temd > distance) { temd -= distance; db++; } }
//生成放大缩小后图形像素buf short[] desBuf = new short[desW * desH]; int dx = 0; int dy = 0; int sx = 0; int sy = 0; int oldy = -1; for (int i = 0; i < desH; i++) { if (oldy == tabY[i]) { System.arraycopy(desBuf, dy - desW, desBuf, dy, desW); } else { dx = 0; for (int j = 0; j < desW; j++) { desBuf[dy + dx] = srcBuf[sy + tabX[j]]; dx++; } sy += (tabY[i] - oldy) * srcW; } oldy = tabY[i]; dy += desW;
|
有一个超简单的办法,你可以对小图片的开头和结尾取样,然后在大图片中搜索。。。这个代码写的不够严谨,不过
byte[] smallPic=....
byte[] bigPic=...
long[] bigLongPic=把bigPic变成long类型 //转换成long可加快搜索速度
long start=ToLong(smallPic,true);
long end=ToLong(smallPic,false);
int offset=smallPic.length-16; //保存小图片开头到结尾的取样点的偏移
for(int i=0;i<bigLongPic.length;i++){
if(bigLongPic[i]==start && bigLongPic[i+offset]==end){ //比较取样点
//这里再具体的比较
}
}
private long ToLong(byte [] ba,bool start){
long r=0;
if(start){
for(int i=0;i<8;i++)r|=ba[i]<<(8-i);
}
else{
for(int i=ba.length-1,j=0;i>=0;i--,j++)r|=ba[i]<<(i-j);
}