现在的位置: 首页 > 综合 > 正文

在MIDP2.0中操作图片像素

2013年06月23日 ⁄ 综合 ⁄ 共 8571字 ⁄ 字号 评论关闭
文章目录

作者:chenyi 文章来源:chenyis blog

正文

   我们知道,在MIDP1.0中,除非我们利用特定厂商的API(比如Nokia),我们是没法对图片的像素进行操作的,但是在MIDP2.0中,Image和Graphics的功能都大大增强了。比如,我们可以获取Image的所有像素值,然后利用程序来修改这些像素(比如说ARGB各自的值),最后再把修改后的像素图绘制出来。通过直接操作图片像素,我们就获得了一种很强大的能力,用编程的方式实现出很多有趣的效果来,而不用额外制作新图片。比如说透明度渐变,颜色反转等。下面就是2个例子,分别实现透明度渐变和颜色反转的功能。

例题一: 透明度渐变效果的实现

   给定一张图片,假如我们想实现这么一种效果:图片由全透明状态逐渐清晰,最后达到正常状态。要实现这一个过程,我们首先要获取该图片的所有像素值,逐步让这些像素的alpha值从0转变到正常,每改变图片的所有像素值一次,我们就请求刷屏一次,把最新的像素图画出来,这样我们就能实现透明度渐变的效果了。代码实现如下:

  1. import java.io.IOException;
  2. import javax.microedition.lcdui.Canvas;
  3. import javax.microedition.lcdui.Display;
  4. import javax.microedition.lcdui.Graphics;
  5. import javax.microedition.lcdui.Image;
  6. import javax.microedition.midlet.MIDlet;
  7. import javax.microedition.midlet.MIDletStateChangeException;
  8. /**
  9.  * 
  10.  * @author Jagie
  11.  * 
  12.  */
  13. public class ShadowMIDlet extends MIDlet {
  14.     Canvas c = new ShadowCanvas();
  15.     public ShadowMIDlet() {
  16.     }
  17.     protected void startApp() throws MIDletStateChangeException {
  18.         Display.getDisplay(this).setCurrent(c);
  19.     }
  20.     protected void pauseApp() {
  21.         // TODO Auto-generated method stub
  22.     }
  23.     protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  24.         // TODO Auto-generated method stub
  25.     }
  26. }
  27. /**
  28.  * 
  29.  * @author Jagie
  30.  * 
  31.  */
  32. class ShadowCanvas extends Canvas implements Runnable {
  33.     int w, h;
  34.     // 原始图片
  35.     Image srcImage;
  36.     // 原始图片的像素数组
  37.     int[] srcRgbImage;
  38.     // 渐变图片的像素数组
  39.     int[] shadowRgbImage;
  40.     int imgWidth, imgHeight;
  41.     int count;
  42.     public ShadowCanvas() {
  43.         w = this.getWidth();
  44.         h = this.getHeight();
  45.         try {
  46.             srcImage = Image.createImage("/av.png");
  47.         } catch (IOException e) {
  48.             // TODO Auto-generated catch block
  49.             e.printStackTrace();
  50.         }
  51.         imgWidth = srcImage.getWidth();
  52.         imgHeight = srcImage.getHeight();
  53.         // 制造原始图片的像素数组,用一个int来代表每一个像素,按位表示方式是:0xAARRGGBB
  54.         srcRgbImage = new int[imgWidth * imgHeight];
  55.         // 获取原始图片的所有像素,参见MIDP APPI文档
  56.         srcImage.getRGB(srcRgbImage, 0, imgWidth, 0, 0, imgWidth, imgHeight);
  57.         shadowRgbImage = new int[srcRgbImage.length];
  58.         System.arraycopy(srcRgbImage, 0, shadowRgbImage, 0,
  59.                 shadowRgbImage.length);
  60.         // 渐变图片的所有像素已开始都是全透明的
  61.         for (int i = 0; i < shadowRgbImage.length; i++) {
  62.             shadowRgbImage[i] &= 0x00ffffff;
  63.         }
  64.         new Thread(this).start();
  65.     }
  66.     public void paint(Graphics g) {
  67.         g.setColor(0, 0, 0);
  68.         g.fillRect(0, 0, w, h);
  69.         // 绘制渐变图片
  70.         g.drawRGB(shadowRgbImage, 0, imgWidth, (w - imgWidth) / 2,
  71.                 (h - imgHeight) / 2, imgWidth, imgHeight, true);
  72.         g.setColor(0, 255, 0);
  73.         g.drawString("count=" + count, w / 2, 30, Graphics.HCENTER
  74.                 | Graphics.TOP);
  75.     }
  76.     public void run() {
  77.         while (true) {
  78.             boolean changed = false;
  79.             // 改变渐变图片的每一个像素
  80.             for (int i = 0; i < shadowRgbImage.length; i++) {
  81.                 // 获取渐变图片的某一像素的alpha值
  82.                 int alpha = (shadowRgbImage[i] & 0xff000000) >>> 24;
  83.                 // 原始图片的对应像素的alpha值
  84.                 int oldAlpha = (srcRgbImage[i] & 0xff000000) >>> 24;
  85.                 if (alpha < oldAlpha) {
  86.                     // alpha值++
  87.                     shadowRgbImage[i] = ((alpha + 1) << 24)
  88.                             | (shadowRgbImage[i] & 0x00ffffff);
  89.                     changed = true;
  90.                 }
  91.             }
  92.             try {
  93.                 Thread.sleep(10);
  94.             } catch (InterruptedException e) {
  95.                 // TODO Auto-generated catch block
  96.                 e.printStackTrace();
  97.             }
  98.             count++;
  99.             repaint();
  100.             // 当所有像素的alpha值都达到原始值后,线程运行结束
  101.             if (!changed) {
  102.                 System.out.println("over");
  103.                 break;
  104.             }
  105.         }
  106.     }
  107. }

透明度渐变效果如下:
  
  

例题二:颜色反转

   在手机游戏中,我们经常会碰上这样一种情况,比如我方飞机和敌方飞机外观是完全一样的,唯一的区别就是颜色不同,比如说敌方飞机是红色的,而我方飞机是绿色的。在MIDP1.0中,我们就只好制作2张图片来表示2种飞机,自然,这样会造成jar空间的极大浪费。但是在MIDP2.0中,通过对图片直接进行像素操作,反转RGB中的一个值,我们只需要一张图片就够了,样例代码如下:

ColorMIDlet.java

  1. import java.io.IOException;
  2. import javax.microedition.lcdui.Canvas;
  3. import javax.microedition.lcdui.Display;
  4. import javax.microedition.lcdui.Graphics;
  5. import javax.microedition.lcdui.Image;
  6. import javax.microedition.midlet.MIDlet;
  7. import javax.microedition.midlet.MIDletStateChangeException;
  8. /**
  9.  * 
  10.  * @author Jagie
  11.  * 
  12.  */
  13. public class ColorMIDlet extends MIDlet {
  14.     Canvas c = new ColorCanvas();
  15.     public ColorMIDlet() {
  16.         super();
  17.         // TODO Auto-generated constructor stub
  18.     }
  19.     protected void startApp() throws MIDletStateChangeException {
  20.         Display.getDisplay(this).setCurrent(c);
  21.     }
  22.     protected void pauseApp() {
  23.         // TODO Auto-generated method stub
  24.     }
  25.     protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  26.         // TODO Auto-generated method stub
  27.     }
  28. }
  29. /**
  30.  * 
  31.  * @author Jagie
  32.  * 
  33.  */
  34. class ColorCanvas extends Canvas {
  35.     Image srcImage;
  36.     int[] targetImage1;
  37.     int[] targetImage2;
  38.     public ColorCanvas() {
  39.         try {
  40.             srcImage = Image.createImage("/av.png");
  41.         } catch (IOException e) {
  42.             // TODO Auto-generated catch block
  43.             e.printStackTrace();
  44.         }
  45.         targetImage1 = GraphicsUtil.flipImageColor(srcImage,
  46.                 GraphicsUtil.SHIFT_RED_TO_BLUE);
  47.         targetImage2 = GraphicsUtil.flipImageColor(srcImage,
  48.                 GraphicsUtil.SHIFT_RED_TO_GREEN);
  49.     }
  50.     public void paint(Graphics g) {
  51.         g.setColor(0, 0, 0);
  52.         g.fillRect(0, 0, this.getWidth(), this.getHeight());
  53.         g.setColor(0x00ff00);
  54.         g.drawString("origin:", getWidth() / 2, 0, Graphics.TOP
  55.                 | Graphics.HCENTER);
  56.         g.drawImage(srcImage, 30, 20, Graphics.LEFT | Graphics.TOP);
  57.         g.drawString("SHIFT_RED_TO_BLUE:", getWidth() / 2,
  58.                 srcImage.getHeight() + 20, Graphics.TOP | Graphics.HCENTER);
  59.         g.drawRGB(targetImage1, 0, srcImage.getWidth(), 30, srcImage
  60.                 .getHeight() + 40, srcImage.getWidth(), srcImage.getHeight(),
  61.                 true);
  62.         g.drawString("SHIFT_RED_TO_GREEN:", getWidth() / 2, srcImage
  63.                 .getHeight() * 2 + 40, Graphics.TOP | Graphics.HCENTER);
  64.         g.drawRGB(targetImage2, 0, srcImage.getWidth(), 30, srcImage
  65.                 .getHeight() * 2 + 60, srcImage.getWidth(), srcImage
  66.                 .getHeight(), true);
  67.     }
  68. }

GraphicsUtil.java

  1. import javax.microedition.lcdui.Image;
  2. /**
  3.  * 
  4.  * @author Jagie
  5.  * 
  6.  */
  7. public class GraphicsUtil {
  8.     public static final int SHIFT_RED_TO_GREEN = 0;
  9.     public static final int SHIFT_RED_TO_BLUE = 1;
  10.     public static final int SHIFT_GREEN_TO_BLUE = 2;
  11.     public static final int SHIFT_GREEN_TO_RED = 3;
  12.     public static final int SHIFT_BLUE_TO_RED = 4;
  13.     public static final int SHIFT_BLUE_TO_GREEN = 5;
  14.     public static int[] flipImageColor(Image source, int shiftType) {
  15.         // we start by getting the image data into an int array - the number
  16.         // of 32-bit ints is equal to the width multiplied by the height
  17.         int[] rgbData = new int[(source.getWidth() * source.getHeight())];
  18.         source.getRGB(rgbData, 0, source.getWidth(), 0, 0, source.getWidth(),
  19.                 source.getHeight());
  20.         // now go through every pixel and adjust it's color
  21.         for (int i = 0; i < rgbData.length; i++) {
  22.             int p = rgbData[i];
  23.             // split out the different byte components of the pixel by
  24.             // applying
  25.             // a mask so we only get what we need, then shift it to make it
  26.             // a normal number we can play around with
  27.             int a = ((p & 0xff000000) >> 24);
  28.             int r = ((p & 0x00ff0000) >> 16);
  29.             int g = ((p & 0x0000ff00) >> 8);
  30.             int b = ((p & 0x000000ff) >> 0);
  31.             int ba = a, br = r, bb = b, bg = g; // backup copies
  32.             // flip the colors around according to the operation required
  33.             switch (shiftType) {
  34.             case SHIFT_RED_TO_GREEN:
  35.                 g = r;
  36.                 r = bg;
  37.                 break;
  38.             case SHIFT_RED_TO_BLUE:
  39.                 b = r;
  40.                 r = bb;
  41.                 break;
  42.             case SHIFT_GREEN_TO_BLUE:
  43.                 g = b;
  44.                 b = bg;
  45.                 break;
  46.             case SHIFT_GREEN_TO_RED:
  47.                 g = r;
  48.                 r = bg;
  49.                 break;
  50.             case SHIFT_BLUE_TO_RED:
  51.                 b = r;
  52.                 r = bb;
  53.                 break;
  54.             case SHIFT_BLUE_TO_GREEN:
  55.                 b = g;
  56.                 g = bb;
  57.                 break;
  58.             }
  59.             // shift all our values back in
  60.             rgbData[i] = (a << 24) | (r << 16) | (g << 8) | b;
  61.         }
  62.         return rgbData;
  63.     }
  64. }

抱歉!评论已关闭.