⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imageutil.java

📁 j2me 图象缩小和放大。自动适应屏幕大小
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.microedition.lcdui.Graphics;  
import javax.microedition.lcdui.Image;  
import javax.microedition.lcdui.game.Sprite;
  
/** 
* 图像工具类 
* @author Jagie 
* 
*/  
  
public class ImageUtil {  
  
/** 
* 图像放缩方法 
* @param srcImage 原始的Image对象 
* @param newW 放缩后的Image的宽度 
* @param newH 放缩后的Image的高度 
* @return 放缩后的Image对象 
*/  
  
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;  
}  
  
public static final Image zoomImage(Image src, int desW, int desH, boolean isBackgroundTrans, boolean isTrans) {
	Image desImg = null;
	int srcW = src.getWidth(); // source image width
	int srcH = src.getHeight(); // source image height
	int[] srcBuf = new int[srcW * srcH]; // source image pixel
	src.getRGB(srcBuf, 0, srcW, 0, 0, srcW, srcH);
//	 compute interpolation table
	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++) { /* vertical direction */
	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++) { /* horizontal direction */
	tabX[db] = (short) sb;
	tems += srcW;
	temd += desW;
	if ( tems > distance) {
	tems -= distance;
	sb++;
	}
	if ( temd > distance) {
	temd -= distance;
	db++;
	}
	}
//	 set transparence
//	 if(isTrans){
//	 int a= 100;//set the transparence of pixel 100
//	 for(int i=0;i<srcBuf.length;i++){
//	 if(srcBuf[i]==0x00FFFFFF)continue;
//	 srcBuf[i]=(a<<24) | (srcBuf[i] & 0x00FFFFFF);// modify the highest 2 value
//	 }
//	 }

//	 formation enlarge and shorten buffer pixel
	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;
	}
	if(isTrans){
//	 int a= 100;//set the transparence of pixel 100
	for(int i=0;i<desBuf.length;i++){
	if(desBuf[i]==0x00FFFFFF)continue;
	int alpha = ((desBuf[i] & 0xff000000) >>> 24)==0?0:100;
	desBuf[i]=((alpha+1)<<24) | (desBuf[i] & 0x00FFFFFF);// modify the highest 2 value
	}
	}
	if(isBackgroundTrans){
	desImg = Image.createRGBImage(desBuf, desW, desH, true);
	}else{
	desImg = Image.createRGBImage(desBuf, desW, desH, false);
	}

	return desImg;
	}
	
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);

    
}

  /*

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -