系统条件:MIDP2.0,cldc1.1

代码如下:
 
ImageUtil*/
 
 
 
/*
  * ImageUtil.java
  *
  * Created on 2008年1月11日, 上午9:26
  *
  */

/**
  *
  * @author alajl
  */

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.Sprite;

public class ImageUtil
 {
     public static final int TURN_LEFT = 1;
     public static final int TURN_RIGHT = 2;

/*
     *获取图片RGB数据,并返回大小为width*height大小的一维数组
     */
     public int[] getPixels(Image src)
     {
         int w = src.getWidth();
         int h = src.getHeight();
         int[] pixels = new int[w * h];
         src.getRGB(pixels, 0, w, 0, 0, w, h);
         return pixels;
     }
 
 
     /*
     *将pixels[]里的数据,生成一张图片,图片宽为w,高为h
     */
    public Image drawPixels(int[] pixels, int w, int h)
     {
         Image image = Image.createRGBImage(pixels, w, h, true);
         pixels = null;
         return image;
     }

/*
     *调整图片大小
     *destW 调整后的宽,destH调整后的高
     */ 
    public  Image effect_resizeImage(Image src, int destW, int destH)
     {
         int srcW = src.getWidth();
         int srcH = src.getHeight();
 
 
        int[] destPixels = new int[destW * destH];
       
        int[] srcPixels = getPixels(src);

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 drawPixels(destPixels, destW, destH);
     }
 
  
   /*
    * 调整图片亮度与对比度。  contrast 对比度,light 亮度
    */
    public Image effect_light_contrast(Image src,double contrast,int light)
    {
             int srcW = src.getWidth();
             int srcH = src.getHeight(); 
            int[] srcPixels = getPixels(src);
             int r = 0;
             int g = 0;
             int b = 0;
             int a = 0;
             int argb;
      //公式y =ax+b   a为对比度,b为亮度
     //int para_b  = light - 127 * (light - 1);
      for (int i = 0; i < srcH; i++)
            {
               for(int ii=0;ii<srcW;ii++)
         {
                       argb = srcPixels[i*srcW+ii]; 
        a = ((argb & 0xff000000) >> 24); // alpha channel
                       r =((argb & 0x00ff0000) >> 16); // red channel
                       g =((argb & 0x0000ff00) >> 8); // green channel
                       b =(argb & 0x000000ff); // blue channel
                       r =(int)( r*contrast+light);                    
        g =(int)( g*contrast+light);
         b =(int)( b*contrast+light);
                     
         /*r =(int)((r -127 ) * contrast + 127+para_b);
          g =(int)((g -127 ) * contrast + 127+para_b);
          b =(int)((b -127 ) * contrast + 127+para_b);*/
          if(r>255)
                                r = 255;
                        else if(r<0) r=0;
                        if(g>255)
                                g = 255;
                        else if(g<0) g=0;
                        if(b>255)
                                b = 255;
                        else if(b<0) b=0;
                srcPixels[i*srcW+ii] = ((a << 24) | (r << 16) | (g << 8) | b);
 
        }
      }
          return drawPixels(srcPixels, srcW, srcH);
    }
 
 
     /*
    * 图片镜像特效
    */
     public Image effect_mirror(Image src)
     {
           int srcW = src.getWidth();
           int srcH = src.getHeight(); 
          int[] srcPixels = getPixels(src);
           int len;
           int temp;
           for (int i = 0; i < srcH; i++)
           {
               len = (i+1)*srcW;
               for(int ii=0;ii<srcW/2;ii++)
              {  
                 temp=srcPixels[i*srcW+ii];
                 srcPixels[i*srcW+ii]=srcPixels[len-1-ii];
                 srcPixels[len-1-ii]=temp;
              }
           }
            return drawPixels(srcPixels, srcW, srcH);
     }
 
      /*
    * 图片剪切,cut_xpos,cut_ypos 切割框的起始位置坐标,cut_width,cut_height 切割框的宽与高
    */
     public Image effect_cut(Image src,int cut_xpos,int cut_ypos,int cut_width,int cut_height)
     {
           int srcW = src.getWidth();
           int srcH = src.getHeight(); 
          int[] srcPixels = getPixels(src);
           int[] desPixels = new int[cut_width*cut_height];
           int argb;
    int num = 0;
           for (int i = 0; i < srcH; i++)
           {
             if(i >= cut_ypos&&i<cut_height+cut_ypos)
             {
                  for(int ii=0;ii<srcW;ii++)
           {
                     if(ii>=cut_xpos && ii<cut_width+cut_xpos)
       {
                          desPixels[num] = srcPixels[i*srcW+ii];   
                         num++;
                         
        }
    }
              }
          }
            return drawPixels(desPixels, cut_width, cut_height);
     }

/*
    * 图片叠加,将src和image合成一张图片,x_pos,y_pos一般为0,0,也可以为自定义值
    */
     public Image effect_image_add_image(Image src,Image image,int x_pos,int y_pos)
     {  
             Image temp  = Image.createImage(src.getWidth(),src.getHeight());
       Graphics g = temp.getGraphics();
              //g.drawImage(src,x_pos,y_pos,Graphics.LEFT|Graphics.TOP);
       //g.drawImage(image,x_pos,y_pos,Graphics.LEFT|Graphics.TOP);*/
             int alpha = 168;
              int []srcRgbdata   =   new   int[src.getWidth()*src.getHeight()];  
             int []desRgbdata   =   new   int[image.getWidth()*image.getHeight()]; 
             src.getRGB(srcRgbdata,0,src.getWidth(),0,0,src.getWidth(),src.getHeight());  
             image.getRGB(desRgbdata,0,image.getWidth(),0,0,image.getWidth(),image.getHeight());   
             g.drawRGB(getTransImg(alpha,srcRgbdata,desRgbdata),0,src.getWidth(),0,0,src.getWidth(),src.getHeight(),false);  
             src=null;  
             image=null; 
             return temp;
     }
    
     /*
    * 图片上添加字符
    */
     public Image effect_image_add_str(Image src,String str,int x_pos,int y_pos)
     {
             Image temp = Image.createImage(src.getWidth(),src.getHeight());
       Graphics g = temp.getGraphics();
              g.drawImage(src,0,0,Graphics.LEFT|Graphics.TOP);
              g.setColor(0x000000);
    g.drawString(str,x_pos,y_pos,Graphics.LEFT|Graphics.TOP);
              return temp;
     }
 
      /*
    * 图片特效负片
    */
     public Image effect_negative(Image src)
     {
          int srcW = src.getWidth();
          int srcH = src.getHeight(); 
         int[] srcPixels = getPixels(src);
          int r = 0;
          int g = 0;
          int b = 0;
          int a = 0;
          int argb;  
         for (int i = 0; i < srcH; i++)
          {
               for(int ii=0;ii<srcW;ii++)
         {
    argb = srcPixels[i*srcW+ii];
                         a = ((argb & 0xff000000) >> 24); // alpha channel
                         r =255-((argb & 0x00ff0000) >> 16); // red channel
                         g =255-((argb & 0x0000ff00) >> 8); // green channel
                         b =255-(argb & 0x000000ff); // blue channel
                         srcPixels[i*srcW+ii] = ((a << 24) | (r << 16) | (g << 8) | b);
         }
          }
         return drawPixels(srcPixels, srcW, srcH);
 
        
    }
 
      /*
    * 图片特效黑白
    */
      public Image effect_black_white(Image src)
     {
          int srcW = src.getWidth();
          int srcH = src.getHeight(); 
         int[] srcPixels = getPixels(src);
          int r = 0;
          int g = 0;
          int b = 0;
          int a = 0;
          int argb;
          int temp;
          
         for (int i = 0; i < srcH; i++)
          {
               for(int ii=0;ii<srcW;ii++)
         {
    argb = srcPixels[i*srcW+ii];
                         a = ((argb & 0xff000000) >> 24); // alpha channel
                         r = ((argb & 0x00ff0000) >> 16); // red channel
                         g = ((argb & 0x0000ff00) >> 8); // green channel
                         b = (argb & 0x000000ff); // blue channel
                         temp = (int)(.299*(double)r+.587*(double)g+.114*(double)b);
                         r = temp;
                         g = temp;
                         b = temp;
                         srcPixels[i*srcW+ii] = ((a << 24) | (r << 16) | (g << 8) | b);
         }
          }
         return drawPixels(srcPixels, srcW, srcH);
 
        
    }
     
      /*
    * 图片特效粉笔画
    */
      public Image effect_crayon(Image src)
      {
         int srcW = src.getWidth();
          int srcH = src.getHeight(); 
         int[] srcPixels = getPixels(src);
          int r = 0;
          int g = 0;
          int b = 0;
          int a = 0;
          int argb;  
         int r1 = 0;
          int g1 = 0;
          int b1 = 0;
          int a1 = 0;
          int r2 = 0;
          int g2 = 0;
          int b2 = 0;
          int a2 = 0;
         
         for (int i = 0; i < srcH; i++)
          {
               for(int ii=0;ii<srcW;ii++)
        {
                   argb = srcPixels[i*srcW+ii];
                   a = ((argb & 0xff000000) >> 24); // alpha channel
                   r = ((argb & 0x00ff0000) >> 16); // red channel
                   g = ((argb & 0x0000ff00) >> 8); // green channel
                   b = (argb & 0x000000ff); // blue channel
                  if(i+1 == srcH)
                   {
                        r1= 0;
                        g1= 0;
                        b1=0;
                   }
                   else
                   {
                      argb = srcPixels[(i+1)*srcW+ii];
                     //a1 = ((argb & 0xff000000) >> 24); // alpha channel
                      r1 = ((argb & 0x00ff0000) >> 16); // red channel
                      g1 = ((argb & 0x0000ff00) >> 8); // green channel
                      b1 = (argb & 0x000000ff); // blue channel
                  }
                   if(ii+1 == srcW){
                        r2= 0;
                        g2= 0;
                        b2=0;
                   }
                   else
                   {
                       argb = srcPixels[i*srcW+ii+1];
                      r2 = ((argb & 0x00ff0000) >> 16); // red channel
                       g2 = ((argb & 0x0000ff00) >> 8); // green channel
                       b2 = (argb & 0x000000ff); // blue channel
                   }
                  // rr1=(r1-r2)^2  rr2=(r1-r3)^2
                   r = (int)Math.sqrt((double)(2*(r-r1)*(r-r1)+(r-r2)*(r-r2)));
                   g = (int)Math.sqrt((double)(2*(g-g1)*(g-g1)+(g-g2)*(g-g2)));
                   b = (int)Math.sqrt((double)(2*(b-b1)*(b-b1)+(b-b2)*(b-b2)));
                   r =255-r; // red channel
                   g =255-g; // green channel
                   b =255-b; // blue channel
                   srcPixels[i*srcW+ii] = ((a << 24) | (r << 16) | (g << 8) | b);
               }
          }
          return drawPixels(srcPixels, srcW, srcH);
      }
     
         /*
    * 图片特效蒙版
    */
       public Image effect_hoodwink(Image src)
     {
          int srcW = src.getWidth();
          int srcH = src.getHeight(); 
         int[] srcPixels = getPixels(src);
          int r = 0;
          int g = 0;
          int b = 0;
          int a = 0;
          int argb;
 
         
         for (int i = 0; i < srcH; i++)
          {
               for(int ii=0;ii<srcW;ii++)
         {
    argb = srcPixels[i*srcW+ii];
                         a = ((argb & 0xff000000) >> 24); // alpha channel
                         r = ((argb & 0x00ff0000) >> 16); // red channel
                         g = ((argb & 0x0000ff00) >> 8); // green channel
                         b = (argb & 0x000000ff); // blue channel
                         r = (int)(.299*(double)r);
                         g = (int)(.587*(double)g);
                         b = (int)(.114*(double)b);
                         srcPixels[i*srcW+ii] = ((a << 24) | (r << 16) | (g << 8) | b);
         }
          }
         return drawPixels(srcPixels, srcW, srcH);
 
        
    }
     
    private   int[]   getTransImg(int alpha,int[] srcRgbdata,int[] desRgbdata)  
    {   
           int []   tempRgbData = new int[desRgbdata.length];  
       
           int   sr  ;
            int   sg ;
            int   sb ;
            int   dr  ; 
           int   dg ;
            int   db ;
           int   tr  ;
            int   tg  ;
            int   tb  ;
            for(int   i=0;i<desRgbdata.length;i++)  
           {  
              sr   =   (srcRgbdata[i]&0xff0000)>>16;  
              sg   =   (srcRgbdata[i]&0xff00)>>8;  
              sb   =   srcRgbdata[i]&0xff;  
              dr   =   (desRgbdata[i]&0xff0000)>>16;  
              dg   =   (desRgbdata[i]&0xff00)>>8;  
              db   =   desRgbdata[i]&0xff;  
              tr   =   (sr*alpha   +   dr*(255-alpha))/255;  
              tg   =   (sg*alpha   +   dg*(255-alpha))/255;  
              tb   =   (sb*alpha   +   db*(255-alpha))/255;    
              tempRgbData[i]   =   (tr<<16)|(tg<<8)|tb;  
           }  
         return   tempRgbData;  
       }
   
         /*
    * 图片特旋转
    */
     public Image effect_rotate(Image src,int direction)
    {
        Sprite sprite = new Sprite(src);
         switch(direction)
         {
                   case 1:
                             sprite.setTransform(sprite.TRANS_ROT270);
                             break;
                   case 2:
                             sprite.setTransform(sprite.TRANS_ROT90);  
                            break;
         }
       
        Image temp = Image.createImage(src.getHeight(),src.getWidth());
   Graphics g = temp.getGraphics();
         sprite.setPosition(0,0);
         sprite.paint(g);
         return temp;  
     }

/*
    * 图片特霓虹灯
    */
     public Image effect_neonLight(Image src)
     {
          int srcW = src.getWidth();
          int srcH = src.getHeight(); 
         int[] srcPixels = getPixels(src);
          int r = 0;
          int g = 0;
          int b = 0;
          int a = 0;
          int argb;  
         int r1 = 0;
          int g1 = 0;
          int b1 = 0;
          int a1 = 0;
          int r2 = 0;
          int g2 = 0;
          int b2 = 0;
          int a2 = 0;
         
         for (int i = 0; i < srcH; i++)
          {
               for(int ii=0;ii<srcW;ii++)
        {
                   argb = srcPixels[i*srcW+ii];
                   a = ((argb & 0xff000000) >> 24); // alpha channel
                   r = ((argb & 0x00ff0000) >> 16); // red channel
                   g = ((argb & 0x0000ff00) >> 8); // green channel
                   b = (argb & 0x000000ff); // blue channel
                  if(i+1 == srcH)
                   {
                        r1= 0;
                        g1= 0;
                        b1=0;
                   }
                   else
                   {
                      argb = srcPixels[(i+1)*srcW+ii];
                     //a1 = ((argb & 0xff000000) >> 24); // alpha channel
                      r1 = ((argb & 0x00ff0000) >> 16); // red channel
                      g1 = ((argb & 0x0000ff00) >> 8); // green channel
                      b1 = (argb & 0x000000ff); // blue channel
                  }
                   if(ii+1 == srcW){
                        r2= 0;
                        g2= 0;
                        b2=0;
                   }
                   else
                   {
                       argb = srcPixels[i*srcW+ii+1];
                      r2 = ((argb & 0x00ff0000) >> 16); // red channel
                       g2 = ((argb & 0x0000ff00) >> 8); // green channel
                       b2 = (argb & 0x000000ff); // blue channel
                   }
                  // rr1=(r1-r2)^2  rr2=(r1-r3)^2
                   r = (int)Math.sqrt((double)(2*(r-r1)*(r-r1)+(r-r2)*(r-r2)));
                   g = (int)Math.sqrt((double)(2*(g-g1)*(g-g1)+(g-g2)*(g-g2)));
                   b = (int)Math.sqrt((double)(2*(b-b1)*(b-b1)+(b-b2)*(b-b2)));
                   srcPixels[i*srcW+ii] = ((a << 24) | (r << 16) | (g << 8) | b);
               }
          }
          return drawPixels(srcPixels, srcW, srcH);
     }
 
}

j2me上面实现图片的特效 (缩放,裁剪,调整大小,镜像,对比度亮度调整,黑白,线条,粉笔画,蒙版)等功能相关推荐

  1. C#图片处理基本应用(裁剪,缩放,清晰度,水印)

    前言 需求源自项目中的一些应用,比如相册功能,通常用户上传相片后我们都会针对该相片再生成一张缩略图,用于其它页面上的列表显示.随便看一下,大部分网站基本都是将原图等比缩放来生成缩略图.但完美主义者会发 ...

  2. Java使用thumbnailator进行图片压缩缩放裁剪水印旋转处理

    Java使用thumbnailator进行图片压缩缩放裁剪水印旋转处理 一.thumbnailator介绍 二.使用步骤 1.maven的pom.xml引入如下 2.测试代码 一.thumbnaila ...

  3. C#图片处理基本应用(裁剪,缩放,清晰度,水印)(转)

    using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Dra ...

  4. html图片缩放6,四款css 图片按比例缩放实例(兼容ie6,7,firefox)

    使用max-width,max-height:或者min-width,min-height的css属性即可.如: 代码如下 img{max-width:100px;max-height:100px;} ...

  5. vue本地上传并预览php,vue.js 实现图片本地预览 裁剪 压缩 上传功能

    以下代码涉及 Vue 2.0 及 ES6 语法. 目标 纯 javascrpit 实现,兼容ie9及以上浏览器,在本地做好文件格式.长宽.大小的检测,减少浏览器交互. 现实是残酷的,为了兼容Ie9 还 ...

  6. Angular实现图片点击缩放组件

    Angular实现图片点击缩放组件 本文将设计一个用于网页中点击图片缩放的 Angular 组件.阅读需要注意的是,本文只讲解原理和设计理念,不过多讲解Angular框架的API. 线demo. 需求 ...

  7. java以目标尺寸按原图片像素比缩放切割图片

    2019独角兽企业重金招聘Python工程师标准>>> 需求:原始图片缩放为目标尺寸后,要求缩放后的图片保持原图片的样式不改变,但是缩放后图片的四边可以裁减. 原理:将待裁剪图片宽高 ...

  8. java 图片不失真缩放,ico格式图片转换,透明图层,jar->exe

    Java 图片缩放,转ico格式 Java 在转换图片格式为 ico 时 需要引入 image4j 的依赖,这里分享一个自己写好的并通过exe4j 打包好的exe小程序.因为功能太少所以再引入 thu ...

  9. jquery实现截取pc图片_Cropper.js 实现裁剪图片并上传(PC端)

    由于之前做项目的时候有需求是需要实现裁剪图片来做头像并上传到服务器,所以上网查询了很多资料,也试用了许多案例,发现cropper插件裁剪是比较完善的,所以结合之前的使用情况,编写了此案例.本案例是参考 ...

最新文章

  1. CDays–4 习题六(修改文本)及相关内容解析。
  2. AVFoundation学习Demo--拍摄视频
  3. CVPR 2017 CFNet:《End-to-end representation learning for Correlation Filter based tracking》论文笔记
  4. Guava新集合-Multiset
  5. Android中获取网络图片的三种方法
  6. python tkinter计算器实例_Python+tkinter使用80行代码实现一个计算器实例
  7. PYTOHN1.day14
  8. hadoop 集群间数据迁移
  9. Java中try与catch的使用
  10. android进阶(三)数据存储之Internal Storage
  11. AD18快速简单入门,画电路原理图以及PCB图
  12. java ppt转图片 失真_Apache POI将PPT转换成图片实例代码
  13. Proxmox VE的初试小探
  14. Excise_day01
  15. 【java】714. 买卖股票的最佳时机含手续费-----动态规划!!!
  16. 逻辑回归模型(一)——数学模型
  17. 【教程】腾讯云轻量应用服务器搭建聊天室
  18. Dubbo 3.0新特性记录
  19. python怎么测试函数_快学Python:如何测试函数与类
  20. 对于数据挖掘的几点认识[转]

热门文章

  1. 云服务器修改远程端口的方法
  2. HTTP与HTTTPS的区别
  3. 2018年内蒙古计算机专业对口升学试题,2018年对口升学考试计算机专业月考三试题整理.doc...
  4. 白话空间统计二十四:地理加权回归(八)结果解读(一)
  5. genymotion报错 Unable to start VirtualBox部分解决方法
  6. html把一条横线放在底部,html怎样把div放在底部
  7. Mac环境配置好ant后提示Permission denied
  8. PGSQL中distinct和distinct on的用法与区别
  9. 团队工具推荐(产品研发篇)
  10. 大数据实践项目 Nginx日志分析可视化