J 2 ME画像のスケーリングに関するいくつかの関数を収集した.


いくつかの画像のスケーリング関数を収集して、実現する可能性のある方式はとても多くて、これらはやはり悪くないと感じて、分かち合って説明します:以下の関数はすべてMIDP 2に基づいています.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();//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////新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 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 intdesBuf=new[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で生成された画像と他の2つの関数で生成された画像は少し違い、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,176208);
2.1枚の画像picを元の2倍の大きさに変換し、pic=scaleImage(pci,2,1);
3.1枚の画像picを元の3分の2に変換し、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