分享

几个不错的J2ME图片缩放函数 - AJava

 hotbain 2010-07-27
代码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.将一张图片pic转换成176*208的图

pic=transImage(pic,176,208);

2.将一张图片pic转换成原来的两倍大

pic=scaleImage(pic,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;       
} 

在诺基亚3250和6230i上面测试了这段代码,使用的图片尺寸为60*60,先后将其缩放为30*30和120*120,缩小到30*30的时候,速度很快,放大到120*120的时候感觉稍有停顿。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多