您的位置:首页 > 其它

收集了几个关于J2ME图片缩放的函数

2010-04-28 16:53 274 查看
收集了几个图像缩放函数,可能实现的方式很多,感觉这几个还是不错的,分享下
说明:以下函数都是基于MIDP2.0的,缩放后保留透明色。

代码1,resizeImage函数

收集了几个图像缩放函数,可能实现的方式很多,感觉这几个还是不错的,分享下说明:以下函数都是基于MIDP2.0的,缩放后保留透明色。

代码1,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,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生成的图片和另外两个函数生成的图像有点区别,感觉ZoomImage效果好点。再放大点就就基本一样了,看不出区别了。

代码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.将1张图片pic转换成176*208的图,pic=transImage(pic,176,208);
2.将1张图片pic转成原来的两倍大,pic=scaleImage(pci,2,1);

3.将一张图片pic转成原来的三分之二,pic=scaleImage(pic,2,3)

代码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;
}


以上内容转载自:http://blog.csdn.net/pjw100/archive/2009/11/26/4876053.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: