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

收集了几个图像缩放函数,可能实现的方式很多,感觉这几个还是不错的,分享下说明:以下函数都是基于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函数
1 public static Image ZoomImage(Image src, int desW, int desH) {
2 Image desImg = null;
3 int srcW = src.getWidth(); // 原始图像宽
4   int srcH = src.getHeight(); // 原始图像高
5   int[] srcBuf = new int[srcW * srcH]; // 原始图片像素信息缓存
6   src.getRGB(srcBuf, 0, srcW, 0, 0, srcW, srcH);
7 // 计算插值表
8   int[] tabY = new int[desH];
9 int[] tabX = new int[desW];
10 int sb = 0;
11 int db = 0;
12 int tems = 0;
13 int temd = 0;
14 int distance = srcH > desH ? srcH : desH;
15 for (int i = 0; i <= distance; i++) { /* 垂直方向 */
16 tabY[db] = sb;
17 tems += srcH;
18 temd += desH;
19 if (tems > distance) {
20 tems -= distance;
21 sb++;
22 }
23 if (temd > distance) {
24 temd -= distance;
25 db++;
26 }
27 }
28 sb = 0;
29 db = 0;
30 tems = 0;
31 temd = 0;
32 distance = srcW > desW ? srcW : desW;
33 for (int i = 0; i <= distance; i++) { /* 水平方向 */
34 tabX[db] = (short) sb;
35 tems += srcW;
36 temd += desW;
37 if (tems > distance) {
38 tems -= distance;
39 sb++;
40 }
41 if (temd > distance) {
42 temd -= distance;
43 db++;
44 }
45 }
46 // 生成放大缩小后图形像素buf
47   int[] desBuf = new int[desW * desH];
48 int dx = 0;
49 int dy = 0;
50 int sy = 0;
51 int oldy = -1;
52 for (int i = 0; i < desH; i++) {
53 if (oldy == tabY[i]) {
54 System.arraycopy(desBuf, dy - desW, desBuf, dy, desW);
55 } else {
56 dx = 0;
57 for (int j = 0; j < desW; j++) {
58 desBuf[dy + dx] = srcBuf[sy + tabX[j]];
59 dx++;
60 }
61 sy += (tabY[i] - oldy) * srcW;
62 }
63 oldy = tabY[i];
64 dy += desW;
65 }
66 // 生成图片
67   desImg = Image.createRGBImage(desBuf, desW, desH, true);
68 return desImg;
69 }

代码3
1 public static Image scaleImage(Image original, int newWidth, int newHeight) {
2 int[] rawInput = new int[original.getHeight() * original.getWidth()];
3 original.getRGB(rawInput, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());
4 int[] rawOutput = new int[newWidth * newHeight];
5 // YD compensates for the x loop by subtracting the width back out
6   int YD = (original.getHeight() / newHeight) * original.getWidth() - original.getWidth();
7 int YR = original.getHeight() % newHeight;
8 int XD = original.getWidth() / newWidth;
9 int XR = original.getWidth() % newWidth;
10 int outOffset = 0;
11 int inOffset = 0;
12 for (int y = newHeight, YE = 0; y > 0; y--) {
13 for (int x = newWidth, XE = 0; x > 0; x--) {
14 rawOutput[outOffset++] = rawInput[inOffset];
15 inOffset += XD;
16 XE += XR;
17 if (XE >= newWidth) {
18 XE -= newWidth;
19 inOffset++;
20 }
21 }
22 inOffset += YD;
23 YE += YR;
24 if (YE >= newHeight) {
25 YE -= newHeight;
26 inOffset += original.getWidth();
27 }
28 }
29 return Image.createRGBImage(rawOutput, newWidth, newHeight, true);
30 }

我将100*100的图片放到到200*200后对比了一下,ZoomImage生成的图片和另外两个函数生成的图像有点区别,感觉ZoomImage效果好点。再放大点就就基本一样了,看不出区别了。

代码4

    
    
    
    
1 /* 图像变换 */ 2 public Image scaleImage(Image src, int scales1, int scales2) 3 { 4 return transImage(src,src.getWidth() * scales1 / scales2,src.getHeight() * scales1 / scales2); 5 } 6 public Image transImage(Image src, int w, int h) 7 { 8 int srcW = src.getWidth(); 9 int srcH = src.getHeight(); 10 int dstW = w,dstH = h; 11 Image tmp = Image.createImage(dstW, srcH); 12 Graphics g = tmp.getGraphics(); 13 int scale = 16 ; 14 int delta = (srcW << scale) / dstW; // 扫描长度 15   int pos = delta / 2 ; // 扫描位置 16   for ( int x = 0 ; x < dstW; x ++ ) 17 { 18 g.setClip(x, 0 , 1 , srcH); 19 g.drawImage(src, x - (pos >> scale), 0 , Graphics.LEFT | Graphics.TOP); 20 pos += delta; 21 } 22 Image dst = Image.createImage( dstW, dstH); 23 g = dst.getGraphics(); 24 delta = (srcH << scale) / dstH; 25 pos = delta / 2 ; 26 for ( int y = 0 ; y < dstH; y ++ ) 27 { 28 g.setClip( 0 ,y, dstW, 1 ); 29 g.drawImage(tmp, 0 , y - (pos >> scale), Graphics.LEFT | Graphics.TOP); 30 pos += delta; 31 } 32 return dst; 33 } 34  

 

用法举例:
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,
     
     
     
     
1 public static final Image scale(Image srcImage, int newW, int newH) { 2 int srcW = srcImage.getWidth(); 3 int srcH = srcImage.getHeight(); 4 // 先做水平方向上的伸缩变换 5   Image tmp = Image.createImage(newW, srcH); 6 Graphics g = tmp.getGraphics(); 7 for ( int x = 0 ; x < newW; x ++ ) { 8 g.setClip(x, 0 , 1 , srcH); // 按比例放缩 9 g.drawImage(srcImage, x - x * srcW / newW, 0 , Graphics.LEFT | Graphics.TOP); 10 } 11 // 再做垂直方向上的伸缩变换 12 Image dst = Image.createImage(newW, newH); 13 g = dst.getGraphics(); 14 for ( int y = 0 ; y < newH; y ++ ) { 15 g.setClip( 0 , y, newW, 1 ); // 按比例放缩 16 g.drawImage(tmp, 0 , y - y * srcH / newH, Graphics.LEFT | Graphics.TOP); 17 } 18 return dst; 19 }

 

以上内容转载自:http://blog.csdn.net/pjw100/archive/2009/11/26/4876053.aspx

你可能感兴趣的:(image,Arrays,buffer,j2me,DST,distance)