代碼1,函數(shù) resizeImage
public static Image resizeImage(Image src, int destW, int destH) {
int srcW = src.getWidth();
int srcH = src.getHeight();
// create pixel arrays
int[] destPixels = new int[destW * destH]; // array to hold destination
// pixels
int[] srcPixels = new int[srcW * srcH]; // array with source's pixels
src.getRGB(srcPixels, 0, srcW, 0, 0, srcW, srcH);
// simple point smapled resizing
// loop through the destination pixels, find the matching pixel on
// the source and use that
for (int destY = 0; destY < destH; ++destY) {
for (int destX = 0; destX < destW; ++destX) {
int srcX = (destX * srcW) / destW;
int srcY = (destY * srcH) / destH;
destPixels[destX + destY * destW] = srcPixels[srcX + srcY * srcW];
}
}
// return a new image created from the destination pixel buffer
return Image.createRGBImage(destPixels, destW, destH, true);
}
代碼2,函數(shù)ZoomImage
public static Image ZoomImage(Image src, int desW, int desH) {
Image desImg = null;
int srcW = src.getWidth(); // 原始圖像寬
int srcH = src.getHeight(); // 原始圖像高
int[] srcBuf = new int[srcW * srcH]; // 原始圖片像素信息緩存
src.getRGB(srcBuf, 0, srcW, 0, 0, srcW, srcH);
// 計算插值表
int[] tabY = new int[desH];
int[] tabX = new int[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] = 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
int[] desBuf = new int[desW * desH];
int dx = 0;
int dy = 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;
}
// 生成圖片
desImg = Image.createRGBImage(desBuf, desW, desH, true);
return desImg;
}
代碼3,
public static Image scaleImage(Image original, int newWidth, int newHeight) {
int[] rawInput = new int[original.getHeight() * original.getWidth()];
original.getRGB(rawInput, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());
int[] rawOutput = new int[newWidth * newHeight];
// YD compensates for the x loop by subtracting the width back out
int YD = (original.getHeight() / newHeight) * original.getWidth() - original.getWidth();
int YR = original.getHeight() % newHeight;
int XD = original.getWidth() / newWidth;
int XR = original.getWidth() % newWidth;
int outOffset = 0;
int inOffset = 0;
for (int y = newHeight, YE = 0; y > 0; y--) {
for (int x = newWidth, XE = 0; x > 0; x--) {
rawOutput[outOffset++] = rawInput[inOffset];
inOffset += XD;
XE += XR;
if (XE >= newWidth) {
XE -= newWidth;
inOffset++;
}
}
inOffset += YD;
YE += YR;
if (YE >= newHeight) {
YE -= newHeight;
inOffset += original.getWidth();
}
}
return Image.createRGBImage(rawOutput, newWidth, newHeight, true);
}
我將100*100的圖放大到200*200后對比了一下,ZoomImage生成的圖片和另外兩個函數(shù)生成的圖像有點區(qū)別,感覺ZoomImage的效果好一點。再放大點就基本一樣了看不出區(qū)別了。
代碼4
/*圖像變換*/
public Image scaleImage(Image src,int scales1,int scales2)
{
return transImage(src,src.getWidth()*scales1/scales2,src.getHeight()*scales1/scales2);
}
public Image transImage(Image src, int w, int h)
{
int srcW = src.getWidth();
int srcH = src.getHeight();
int dstW=w,dstH=h;
Image tmp = Image.createImage(dstW, srcH);
Graphics g = tmp.getGraphics();
int scale=16;
int delta = (srcW << scale) / dstW;//掃描長度
int pos = delta / 2;//掃描位置
for (int x = 0; x < dstW; x++)
{
g.setClip(x, 0, 1, srcH);
g.drawImage(src, x - (pos >> scale), 0, Graphics.LEFT | Graphics.TOP);
pos += delta;
}
Image dst = Image.createImage( dstW, dstH);
g = dst.getGraphics();
delta = (srcH << scale) / dstH;
pos = delta / 2;
for (int y = 0; y < dstH; y++)
{
g.setClip(0,y, dstW, 1);
g.drawImage(tmp, 0, y - (pos >> scale), Graphics.LEFT | Graphics.TOP);
pos += delta;
}
return dst;
}
用法舉例:
1.將一張圖片pic轉(zhuǎn)換成176*208的圖
pic=transImage(pic,176,208);
2.將一張圖片pic轉(zhuǎn)換成原來的兩倍大
pic=scaleImage(pic,2,1);
3.將一張圖片pic轉(zhuǎn)換成原來的三分之二
pic=scaleImage(pic,2,3);
從上面的例子,大家應(yīng)該知道怎么用了吧,呵呵
代碼5,
public static final Image scale (Image srcImage, int newW, int newH) {
int srcW = srcImage.getWidth();
int srcH = srcImage.getHeight();
//先做水平方向上的伸縮變換
Image tmp = Image.createImage(newW, srcH);
Graphics g = tmp.getGraphics();
for (int x = 0; x < newW; x++) {
g.setClip(x, 0, 1, srcH); //按比例放縮
g.drawImage(srcImage,x-x*srcW/newW,0,Graphics.LEFT | Graphics.TOP);
}
//再做垂直方向上的伸縮變換
Image dst = Image.createImage(newW, newH);
g = dst.getGraphics();
for (int y = 0; y < newH; y++) {
g.setClip(0, y, newW, 1); //按比例放縮
g.drawImage(tmp,0,y-y*srcH/newH,Graphics.LEFT | Graphics.TOP);
}
return dst;
}
在諾基亞3250和6230i上面測試了這段代碼,使用的圖片尺寸為60*60,先后將其縮放為30*30和120*120,縮小到30*30的時候,速度很快,放大到120*120的時候感覺稍有停頓。
|