zoukankan      html  css  js  c++  java
  • j2me图片处理大全

    图片的渐变效果:

      1 import java.io.IOException;
      2 
      3 import javax.microedition.lcdui.Canvas;
      4 import javax.microedition.lcdui.Graphics;
      5 import javax.microedition.lcdui.Image;
      6 
      7 //ShadowCanvas.java
      8 /** 
      9  * @author boolean liuhao137310@163.com: 
     10  * @version 创建时间:2009-2-7 下午02:01:27 
     11  * @TODO 图片的渐变效果 在第一张图片的基础上渐变出第二张图片
     12  */
     13 class ShadowCanvas extends Canvas implements Runnable { 
     14     int w, h; 
     15 
     16     /**原始图片*/ 
     17     Image img2;
     18     
     19     /**渐变图片*/
     20     Image srcImage;
     21 
     22     // 原始图片的像素数组 
     23     int[] srcRgbImage; 
     24 
     25     // 渐变图片的像素数组 
     26     int[] shadowRgbImage; 
     27 
     28     int imgWidth, imgHeight; 
     29 
     30     int count; 
     31 
     32     /**原始图片只画一次即可 此变量记录是否是第一次画原始图片*/
     33     private boolean first = true;
     34     
     35     public ShadowCanvas() { 
     36         w = this.getWidth(); 
     37         h = this.getHeight(); 
     38         try { 
     39             srcImage = Image.createImage("/logoBg.png"); 
     40             img2 = Image.createImage("/test_bg.png");
     41         } catch (IOException e) { 
     42             e.printStackTrace(); 
     43         } 
     44         imgWidth = srcImage.getWidth(); 
     45         imgHeight = srcImage.getHeight(); 
     46         // 制造原始图片的像素数组,用一个int来代表每一个像素,按位表示方式是:0xAARRGGBB 
     47         srcRgbImage = new int[imgWidth * imgHeight]; 
     48         // 获取原始图片的所有像素,参见MIDP APPI文档 
     49         srcImage.getRGB(srcRgbImage, 0, imgWidth, 0, 0, imgWidth, imgHeight); 
     50 
     51         shadowRgbImage = new int[srcRgbImage.length]; 
     52 
     53         System.arraycopy(srcRgbImage, 0, shadowRgbImage, 0, 
     54                 shadowRgbImage.length); 
     55 
     56         // 渐变图片的所有像素一开始都是全透明的 
     57         for (int i = 0; i < shadowRgbImage.length; i++) { 
     58             shadowRgbImage[i] &= 0x00ffffff; 
     59         } 
     60 
     61         new Thread(this).start(); 
     62 
     63     } 
     64 
     65     public void paint(Graphics g) { 
     66 //        g.setColor(0, 0, 0); 
     67 //        g.fillRect(0, 0, w, h); 
     68         if(first){
     69             g.drawImage(img2, 0, 0, 0);
     70             first = false;
     71         }
     72         // 绘制渐变图片 
     73         g.drawRGB(shadowRgbImage, 0, imgWidth, (w - imgWidth) / 2, 
     74                 (h - imgHeight) / 2, imgWidth, imgHeight, true); 
     75 
     76 //        g.setColor(0, 255, 0); 
     77 //        g.drawString("count=" + count, w / 2, 30, Graphics.HCENTER 
     78 //                | Graphics.TOP); 
     79     } 
     80 
     81     public void run() { 
     82         while (true) { 
     83 
     84             boolean changed = false; 
     85             // 改变渐变图片的每一个像素 
     86             for (int i = 0; i < shadowRgbImage.length; i++) { 
     87                 // 获取渐变图片的某一像素的alpha值 
     88                 int alpha = (shadowRgbImage[i] & 0xff000000) >>> 24; 
     89                 // 原始图片的对应像素的alpha值 
     90                 int oldAlpha = (srcRgbImage[i] & 0xff000000) >>> 24; 
     91 
     92                 if (alpha < oldAlpha) { 
     93                     // alpha值++  写的太精妙了 把移位用到了极致
     94                     shadowRgbImage[i] = ((alpha + 1) << 24) 
     95                             | (shadowRgbImage[i] & 0x00ffffff); 
     96                     changed = true; 
     97                 } 
     98             } 
     99 
    100             try { 
    101                 Thread.sleep(10); 
    102             } catch (InterruptedException e) { 
    103                 e.printStackTrace(); 
    104             } 
    105             count ++; 
    106             repaint(); 
    107             // 当所有像素的alpha值都达到原始值后,线程运行结束 
    108             if (!changed) { 
    109                 System.out.println("over"); 
    110                 break; 
    111             } 
    112         } 
    113 
    114     } 
    115 
    116 } 

    下边这个类是照搬别人的:

      1 ImageUtil
      2 
      3 
      4 
      5 /*
      6  * ImageUtil.java
      7  *
      8  * Created on 2008年1月11日, 上午9:26
      9  *
     10  */
     11 
     12 
     13 /**
     14  *
     15  * @author alajl
     16  */
     17 
     18 
     19 import javax.microedition.lcdui.*;
     20 import javax.microedition.lcdui.game.Sprite;
     21 
     22 
     23 public class ImageUtil
     24 {
     25     public static final int TURN_LEFT = 1;
     26     public static final int TURN_RIGHT = 2;
     27 
     28 
     29     /*
     30     *获取图片RGB数据,并返回大小为width*height大小的一维数组
     31     */
     32     public int[] getPixels(Image src)
     33     {
     34         int w = src.getWidth();
     35         int h = src.getHeight();
     36         int[] pixels = new int[w * h];
     37         src.getRGB(pixels, 0, w, 0, 0, w, h);
     38         return pixels;
     39     }
     40 
     41  
     42     /*
     43     *将pixels[]里的数据,生成一张图片,图片宽为w,高为h
     44     */ 
     45     public Image drawPixels(int[] pixels, int w, int h)
     46     {
     47         Image image = Image.createRGBImage(pixels, w, h, true);
     48         pixels = null;
     49         return image;
     50     }
     51 
     52 
     53     /*
     54     *调整图片大小
     55     *destW 调整后的宽,destH调整后的高
     56     */  
     57     public  Image effect_resizeImage(Image src, int destW, int destH)
     58     {
     59         int srcW = src.getWidth();
     60         int srcH = src.getHeight();
     61 
     62   
     63         int[] destPixels = new int[destW * destH]; 
     64         
     65         int[] srcPixels = getPixels(src); 
     66 
     67             for (int destY = 0; destY < destH; ++destY)
     68             {
     69                 for (int destX = 0; destX < destW; ++destX)
     70                 {
     71                     int srcX = (destX * srcW) / destW;
     72                     int srcY = (destY * srcH) / destH;
     73                     destPixels[destX + destY * destW] = srcPixels[srcX + srcY
     74                             * srcW];
     75                 }
     76             }
     77 
     78   
     79         return drawPixels(destPixels, destW, destH);
     80     }
     81 
     82    
     83    /*
     84    * 调整图片亮度与对比度。  contrast 对比度,light 亮度
     85    */
     86    public Image effect_light_contrast(Image src,double contrast,int light)
     87    {
     88             int srcW = src.getWidth();
     89             int srcH = src.getHeight();  
     90             int[] srcPixels = getPixels(src);
     91             int r = 0;
     92             int g = 0;
     93             int b = 0;
     94             int a = 0;
     95             int argb;
     96      //公式y =ax+b   a为对比度,b为亮度 
     97      //int para_b  = light - 127 * (light - 1);
     98      for (int i = 0; i < srcH; i++)
     99            {
    100               for(int ii=0;ii<srcW;ii++)
    101         {
    102                       argb = srcPixels[i*srcW+ii];  
    103         a = ((argb & 0xff000000) >> 24); // alpha channel
    104                       r =((argb & 0x00ff0000) >> 16); // red channel
    105                       g =((argb & 0x0000ff00) >> 8); // green channel
    106                       b =(argb & 0x000000ff); // blue channel
    107                       r =(int)( r*contrast+light);                     
    108         g =(int)( g*contrast+light);
    109         b =(int)( b*contrast+light);
    110                      
    111          /*r =(int)((r -127 ) * contrast + 127+para_b);
    112          g =(int)((g -127 ) * contrast + 127+para_b);
    113          b =(int)((b -127 ) * contrast + 127+para_b);*/
    114          if(r>255)
    115                                r = 255;
    116                        else if(r<0) r=0;
    117                        if(g>255)
    118                                g = 255;
    119                        else if(g<0) g=0;
    120                        if(b>255)
    121                                b = 255;
    122                        else if(b<0) b=0;
    123                srcPixels[i*srcW+ii] = ((a << 24) | (r << 16) | (g << 8) | b);
    124 
    125         }
    126      }
    127          return drawPixels(srcPixels, srcW, srcH);
    128    }
    129  
    130  
    131     /*
    132    * 图片镜像特效
    133    */
    134     public Image effect_mirror(Image src)
    135     {
    136           int srcW = src.getWidth();
    137           int srcH = src.getHeight();  
    138           int[] srcPixels = getPixels(src);
    139           int len;
    140           int temp;
    141           for (int i = 0; i < srcH; i++)
    142           {
    143               len = (i+1)*srcW;
    144               for(int ii=0;ii<srcW/2;ii++) 
    145               {   
    146                  temp=srcPixels[i*srcW+ii]; 
    147                  srcPixels[i*srcW+ii]=srcPixels[len-1-ii]; 
    148                  srcPixels[len-1-ii]=temp; 
    149               } 
    150            }
    151            return drawPixels(srcPixels, srcW, srcH);
    152     }
    153 
    154       /*
    155    * 图片剪切,cut_xpos,cut_ypos 切割框的起始位置坐标,cut_width,cut_height 切割框的宽与高
    156    */
    157     public Image effect_cut(Image src,int cut_xpos,int cut_ypos,int cut_width,int cut_height)
    158     {
    159           int srcW = src.getWidth();
    160           int srcH = src.getHeight();  
    161           int[] srcPixels = getPixels(src);
    162           int[] desPixels = new int[cut_width*cut_height];
    163           int argb;
    164    int num = 0;
    165           for (int i = 0; i < srcH; i++)
    166           {
    167             if(i >= cut_ypos&&i<cut_height+cut_ypos)
    168             {
    169                  for(int ii=0;ii<srcW;ii++)
    170           {
    171                     if(ii>=cut_xpos && ii<cut_width+cut_xpos)
    172       {
    173                          desPixels[num] = srcPixels[i*srcW+ii];    
    174                          num++;
    175                          
    176         }
    177    }
    178              }
    179          }
    180            return drawPixels(desPixels, cut_width, cut_height);
    181     }
    182 
    183 
    184     /*
    185    * 图片叠加,将src和image合成一张图片,x_pos,y_pos一般为0,0,也可以为自定义值
    186    */
    187     public Image effect_image_add_image(Image src,Image image,int x_pos,int y_pos)
    188     {   
    189              Image temp  = Image.createImage(src.getWidth(),src.getHeight());
    190       Graphics g = temp.getGraphics();
    191              //g.drawImage(src,x_pos,y_pos,Graphics.LEFT|Graphics.TOP);
    192       //g.drawImage(image,x_pos,y_pos,Graphics.LEFT|Graphics.TOP);*/ 
    193              int alpha = 168;
    194              int []srcRgbdata   =   new   int[src.getWidth()*src.getHeight()];   
    195              int []desRgbdata   =   new   int[image.getWidth()*image.getHeight()];  
    196              src.getRGB(srcRgbdata,0,src.getWidth(),0,0,src.getWidth(),src.getHeight());   
    197              image.getRGB(desRgbdata,0,image.getWidth(),0,0,image.getWidth(),image.getHeight());    
    198              g.drawRGB(getTransImg(alpha,srcRgbdata,desRgbdata),0,src.getWidth(),0,0,src.getWidth(),src.getHeight(),false);   
    199              src=null;   
    200              image=null;  
    201              return temp;
    202     }
    203     
    204      /*
    205    * 图片上添加字符
    206    */
    207     public Image effect_image_add_str(Image src,String str,int x_pos,int y_pos)
    208     { 
    209              Image temp = Image.createImage(src.getWidth(),src.getHeight());
    210       Graphics g = temp.getGraphics();
    211              g.drawImage(src,0,0,Graphics.LEFT|Graphics.TOP);
    212              g.setColor(0x000000);
    213    g.drawString(str,x_pos,y_pos,Graphics.LEFT|Graphics.TOP);
    214              return temp;
    215     }
    216  
    217      /*
    218    * 图片特效负片
    219    */
    220     public Image effect_negative(Image src)
    221     {
    222          int srcW = src.getWidth();
    223          int srcH = src.getHeight();  
    224          int[] srcPixels = getPixels(src);
    225          int r = 0;
    226          int g = 0;
    227          int b = 0;
    228          int a = 0;
    229          int argb;   
    230          for (int i = 0; i < srcH; i++)
    231          {
    232               for(int ii=0;ii<srcW;ii++)
    233         {
    234    argb = srcPixels[i*srcW+ii];
    235                         a = ((argb & 0xff000000) >> 24); // alpha channel
    236                         r =255-((argb & 0x00ff0000) >> 16); // red channel
    237                         g =255-((argb & 0x0000ff00) >> 8); // green channel
    238                         b =255-(argb & 0x000000ff); // blue channel
    239                         srcPixels[i*srcW+ii] = ((a << 24) | (r << 16) | (g << 8) | b);
    240         }
    241          }
    242         return drawPixels(srcPixels, srcW, srcH);
    243 
    244          
    245     }
    246 
    247       /*
    248    * 图片特效黑白
    249    */
    250      public Image effect_black_white(Image src)
    251     {
    252          int srcW = src.getWidth();
    253          int srcH = src.getHeight();  
    254          int[] srcPixels = getPixels(src);
    255          int r = 0;
    256          int g = 0;
    257          int b = 0;
    258          int a = 0;
    259          int argb;
    260          int temp;
    261           
    262          for (int i = 0; i < srcH; i++)
    263          {
    264               for(int ii=0;ii<srcW;ii++)
    265         {
    266    argb = srcPixels[i*srcW+ii];
    267                         a = ((argb & 0xff000000) >> 24); // alpha channel
    268                         r = ((argb & 0x00ff0000) >> 16); // red channel
    269                         g = ((argb & 0x0000ff00) >> 8); // green channel
    270                         b = (argb & 0x000000ff); // blue channel
    271                         temp = (int)(.299*(double)r+.587*(double)g+.114*(double)b);
    272                         r = temp;
    273                         g = temp;
    274                         b = temp;
    275                         srcPixels[i*srcW+ii] = ((a << 24) | (r << 16) | (g << 8) | b);
    276         }
    277          }
    278         return drawPixels(srcPixels, srcW, srcH);
    279 
    280          
    281     }
    282      
    283       /*
    284    * 图片特效粉笔画
    285    */
    286      public Image effect_crayon(Image src)
    287      { 
    288          int srcW = src.getWidth();
    289          int srcH = src.getHeight();  
    290          int[] srcPixels = getPixels(src);
    291          int r = 0;
    292          int g = 0;
    293          int b = 0;
    294          int a = 0;
    295          int argb;   
    296          int r1 = 0;
    297          int g1 = 0;
    298          int b1 = 0;
    299          int a1 = 0;
    300          int r2 = 0;
    301          int g2 = 0;
    302          int b2 = 0;
    303          int a2 = 0;
    304          
    305          for (int i = 0; i < srcH; i++)
    306          {
    307               for(int ii=0;ii<srcW;ii++)
    308        {
    309                   argb = srcPixels[i*srcW+ii];
    310                   a = ((argb & 0xff000000) >> 24); // alpha channel
    311                   r = ((argb & 0x00ff0000) >> 16); // red channel
    312                   g = ((argb & 0x0000ff00) >> 8); // green channel
    313                   b = (argb & 0x000000ff); // blue channel 
    314                   if(i+1 == srcH)
    315                   {
    316                        r1= 0;
    317                        g1= 0;
    318                        b1=0;
    319                   }
    320                   else
    321                   {
    322                      argb = srcPixels[(i+1)*srcW+ii]; 
    323                      //a1 = ((argb & 0xff000000) >> 24); // alpha channel
    324                      r1 = ((argb & 0x00ff0000) >> 16); // red channel
    325                      g1 = ((argb & 0x0000ff00) >> 8); // green channel
    326                      b1 = (argb & 0x000000ff); // blue channel 
    327                   }
    328                   if(ii+1 == srcW){
    329                        r2= 0;
    330                        g2= 0;
    331                        b2=0;
    332                   }
    333                   else
    334                   {
    335                       argb = srcPixels[i*srcW+ii+1]; 
    336                       r2 = ((argb & 0x00ff0000) >> 16); // red channel
    337                       g2 = ((argb & 0x0000ff00) >> 8); // green channel
    338                       b2 = (argb & 0x000000ff); // blue channel
    339                   }
    340                  // rr1=(r1-r2)^2  rr2=(r1-r3)^2
    341                   r = (int)Math.sqrt((double)(2*(r-r1)*(r-r1)+(r-r2)*(r-r2)));
    342                   g = (int)Math.sqrt((double)(2*(g-g1)*(g-g1)+(g-g2)*(g-g2)));
    343                   b = (int)Math.sqrt((double)(2*(b-b1)*(b-b1)+(b-b2)*(b-b2)));
    344                   r =255-r; // red channel
    345                   g =255-g; // green channel
    346                   b =255-b; // blue channel
    347                   srcPixels[i*srcW+ii] = ((a << 24) | (r << 16) | (g << 8) | b);
    348               }
    349          }
    350          return drawPixels(srcPixels, srcW, srcH);
    351      }
    352      
    353          /*
    354    * 图片特效蒙版
    355    */
    356       public Image effect_hoodwink(Image src)
    357     {
    358          int srcW = src.getWidth();
    359          int srcH = src.getHeight();  
    360          int[] srcPixels = getPixels(src);
    361          int r = 0;
    362          int g = 0;
    363          int b = 0;
    364          int a = 0;
    365          int argb;
    366 
    367           
    368          for (int i = 0; i < srcH; i++)
    369          {
    370               for(int ii=0;ii<srcW;ii++)
    371         {
    372    argb = srcPixels[i*srcW+ii];
    373                         a = ((argb & 0xff000000) >> 24); // alpha channel
    374                         r = ((argb & 0x00ff0000) >> 16); // red channel
    375                         g = ((argb & 0x0000ff00) >> 8); // green channel
    376                         b = (argb & 0x000000ff); // blue channel
    377                         r = (int)(.299*(double)r);
    378                         g = (int)(.587*(double)g);
    379                         b = (int)(.114*(double)b);
    380                         srcPixels[i*srcW+ii] = ((a << 24) | (r << 16) | (g << 8) | b);
    381         }
    382          }
    383         return drawPixels(srcPixels, srcW, srcH);
    384 
    385          
    386     }
    387      
    388     private   int[]   getTransImg(int alpha,int[] srcRgbdata,int[] desRgbdata)   
    389     {    
    390            int []   tempRgbData = new int[desRgbdata.length];   
    391         
    392            int   sr  ;
    393            int   sg ;
    394            int   sb ;
    395            int   dr  ;  
    396            int   dg ;
    397            int   db ; 
    398            int   tr  ;
    399            int   tg  ;
    400            int   tb  ;
    401            for(int   i=0;i<desRgbdata.length;i++)   
    402            {   
    403               sr   =   (srcRgbdata[i]&0xff0000)>>16;   
    404               sg   =   (srcRgbdata[i]&0xff00)>>8;   
    405               sb   =   srcRgbdata[i]&0xff;   
    406               dr   =   (desRgbdata[i]&0xff0000)>>16;   
    407               dg   =   (desRgbdata[i]&0xff00)>>8;   
    408               db   =   desRgbdata[i]&0xff;   
    409               tr   =   (sr*alpha   +   dr*(255-alpha))/255;   
    410               tg   =   (sg*alpha   +   dg*(255-alpha))/255;   
    411               tb   =   (sb*alpha   +   db*(255-alpha))/255;     
    412               tempRgbData[i]   =   (tr<<16)|(tg<<8)|tb;   
    413            }   
    414          return   tempRgbData;   
    415        } 
    416     
    417          /*
    418    * 图片特旋转
    419    */
    420     public Image effect_rotate(Image src,int direction) 
    421     { 
    422         Sprite sprite = new Sprite(src);
    423         switch(direction)
    424         {
    425                   case 1:
    426                             sprite.setTransform(sprite.TRANS_ROT270);
    427                             break;
    428                   case 2:
    429                             sprite.setTransform(sprite.TRANS_ROT90);   
    430                             break;
    431         }
    432        
    433         Image temp = Image.createImage(src.getHeight(),src.getWidth());
    434   Graphics g = temp.getGraphics();
    435         sprite.setPosition(0,0);
    436         sprite.paint(g);
    437         return temp;   
    438      } 
    439 
    440     
    441    
    442 
    443        /*
    444    * 图片特霓虹灯
    445    */
    446     public Image effect_neonLight(Image src)
    447     {
    448          int srcW = src.getWidth();
    449          int srcH = src.getHeight();  
    450          int[] srcPixels = getPixels(src);
    451          int r = 0;
    452          int g = 0;
    453          int b = 0;
    454          int a = 0;
    455          int argb;   
    456          int r1 = 0;
    457          int g1 = 0;
    458          int b1 = 0;
    459          int a1 = 0;
    460          int r2 = 0;
    461          int g2 = 0;
    462          int b2 = 0;
    463          int a2 = 0;
    464          
    465          for (int i = 0; i < srcH; i++)
    466          {
    467               for(int ii=0;ii<srcW;ii++)
    468        {
    469                   argb = srcPixels[i*srcW+ii];
    470                   a = ((argb & 0xff000000) >> 24); // alpha channel
    471                   r = ((argb & 0x00ff0000) >> 16); // red channel
    472                   g = ((argb & 0x0000ff00) >> 8); // green channel
    473                   b = (argb & 0x000000ff); // blue channel 
    474                   if(i+1 == srcH)
    475                   {
    476                        r1= 0;
    477                        g1= 0;
    478                        b1=0;
    479                   }
    480                   else
    481                   {
    482                      argb = srcPixels[(i+1)*srcW+ii]; 
    483                      //a1 = ((argb & 0xff000000) >> 24); // alpha channel
    484                      r1 = ((argb & 0x00ff0000) >> 16); // red channel
    485                      g1 = ((argb & 0x0000ff00) >> 8); // green channel
    486                      b1 = (argb & 0x000000ff); // blue channel 
    487                   }
    488                   if(ii+1 == srcW){
    489                        r2= 0;
    490                        g2= 0;
    491                        b2=0;
    492                   }
    493                   else
    494                   {
    495                       argb = srcPixels[i*srcW+ii+1]; 
    496                       r2 = ((argb & 0x00ff0000) >> 16); // red channel
    497                       g2 = ((argb & 0x0000ff00) >> 8); // green channel
    498                       b2 = (argb & 0x000000ff); // blue channel
    499                   }
    500                  // rr1=(r1-r2)^2  rr2=(r1-r3)^2
    501                   r = (int)Math.sqrt((double)(2*(r-r1)*(r-r1)+(r-r2)*(r-r2)));
    502                   g = (int)Math.sqrt((double)(2*(g-g1)*(g-g1)+(g-g2)*(g-g2)));
    503                   b = (int)Math.sqrt((double)(2*(b-b1)*(b-b1)+(b-b2)*(b-b2)));
    504                   srcPixels[i*srcW+ii] = ((a << 24) | (r << 16) | (g << 8) | b);
    505               }
    506          }
    507          return drawPixels(srcPixels, srcW, srcH);
    508     }
    509 
    510 }

    图片的透明处理:

     1 /**
     2      * 
     3      * @param img
     4      *            原始图片
     5      * @param transparent
     6      *            透明度 0-255之间
     7      * @return 处理透明度后的图片
     8      */
     9     public static Image effect_transparent(Image img, int transparent) {
    10         if (transparent < 0 || transparent > 255)
    11             return img;
    12 
    13         int srcW = img.getWidth();
    14         int srcH = img.getHeight();
    15         int[] srcPixels = getPixels(img); //函数功能 讲图片数据存入指定数组
    16 
    17         for (int i = 0; i < srcPixels.length; i++) {
    18             int a = srcPixels[i] >> 24;
    19             srcPixels[i] = (transparent << 24) | (srcPixels[i] & 0x00ffffff);
    20 
    21         }
    22         return drawPixels(srcPixels, srcW, srcH); //将数组转化为图片
    23     }

    还有一个任意角度翻转函数:

     1 /**
     2     *@param imgSource  源图像
     3     *@param cx   旋转点相对于源图像坐上角横坐标
     4     *@param cy   旋转点相对于源图像坐上角纵坐标
     5     *@param theta 图像逆时针旋转的角度
     6     *@return 旋转后的图像
     7     */
     8     public Image rotate(Image imgSource, int cx, int cy, double theta) 
     9     {
    10         if (Math.abs(theta % 360) < 1)
    11             return imgSource; //角度很小时直接返回
    12 
    13         int w1 = imgSource.getWidth(); //原始图像的高度和宽度
    14 
    15         int h1 = imgSource.getHeight();
    16 
    17         int[] srcMap = new int[w1 * h1];
    18 
    19         imgSource.getRGB(srcMap, 0, w1, 0, 0, w1, h1); //获取原始图像的像素信息
    20 
    21         int dx = cx > w1 / 2 ? cx : w1 - cx; //计算旋转半径
    22 
    23         int dy = cy > h1 / 2 ? cy : h1 - cy;
    24 
    25         double dr = Math.sqrt(dx * dx + dy * dy);
    26 
    27         int wh2 = (int) (2 * dr + 1); //旋转后新图像为正方形,其边长+1是为了防止数组越界
    28 
    29         int[] destMap = new int[wh2 * wh2]; //存放新图像象素的数组
    30 
    31         double destX, destY;
    32 
    33         double radian = theta * Math.PI / 180; //计算角度计算对应的弧度值
    34 
    35         for (int i = 0; i < w1; i++) {
    36             for (int j = 0; j < h1; j++) {
    37                 if (srcMap[j * w1 + i] >> 24 != 0) { //对非透明点才进行处理
    38                     // 得到当前点经旋转后相对于新图像左上角的坐标
    39                     destX = dr + (i - cx) * Math.cos(radian) + (j - cy)
    40                             * Math.sin(radian);
    41                     destY = dr + (j - cy) * Math.cos(radian) - (i - cx)
    42                             * Math.sin(radian);
    43                     //从源图像中往新图像中填充像素
    44                     destMap[(int) destY * wh2 + (int) destX] = srcMap[j * w1
    45                             + i];
    46                 }
    47             }
    48         }
    49         return Image.createRGBImage(destMap, wh2, wh2, true); //返回旋转后的图像
    50     }

    以上所有代码均来自于互联网,在此仅为备份以便后查。

  • 相关阅读:
    js试题
    JavaScript 实现removeElement函数
    关于优化
    变量申明的提升,闭包,作用域,this,运算符优先级详细举例及讲解
    前端架构:MVC以及MVVM MVP介绍
    JavaScript事件委托
    各种浏览器的Hack写法(chrome firefox ie等)
    vue页面跳转不刷新时created只执行一次
    iview-admin添加的路由为英文
    vue中使用moment时间戳
  • 原文地址:https://www.cnblogs.com/gaven/p/2483075.html
Copyright © 2011-2022 走看看