Look into it ~

          present
          隨筆 - 32, 文章 - 0, 評(píng)論 - 3, 引用 - 0
          數(shù)據(jù)加載中……

          Serializing an Image

          Creating an image from an array of data is an easy task, but to create a byte-array of data from an image is a little more complicated. But it's required if you want to send a modified image to a server.

          To create a byte-array of data from an image, we can use the getRGB(..) method in the image class in MIDP 2.0. From the getRGB method we get an int-array of data containing all the ARGB values of each pixel in the image. Integers in java are four bytes, so we need to split each int value into four byte values.

          To mask out each of the bytes in the int we can use the 'AND &' operator and the 'shift-right >>' operator. Here's an example:
          int ARGB = 0xFFFFFFFF;  // AARRGGBB 
          int a = (ARGB & 0xFF000000); 
          int r = (ARGB & 0x00FF0000); 
          int g = (ARGB & 0x0000FF00); 
          int b = (ARGB & 0x000000FF);
           
          // Here we move each bit to the right.
          = (a >> 24); // a = 0x000000FF 
          = (r >> 16); // r = 0x000000FF 
          = (g >> 8); // g = 0x000000FF
          = b;        // b = 0x000000FF


          When we convert the integer to a byte, there are some problems since there are only signed bytes in Java. A byte may contain values between -128 and 127 and from our integer we'll have a value between 0 and 255.

          Here are some conversion examples:

          Integer val 
          127 = byte val 127.
          Integer val 
          128 = byte val -128.
          Integer val 
          255 = byte val -1.

          byte ba = (byte)(a);  // if a=0x000000FF (255), then ba = -1
          byte br = (byte)(r); 
          byte bg = (byte)(g); 
          byte bb = (byte)(b);


          We have to loop though each pixel in the image and get each pixel value to our byte-array. When that's done, we can send the image to a server where we can convert the byte-array back to a integer-array and then show our picture.

          So, when we convert the byte back to an integer we have to check if the byte value is less than zero.
          int a, r, g, b;
          if(ba<0)
               a 
          = 256 - a;


          Now our new integer value will be between 0 and 255 and we just have to use the 'shift-left <<' operator and add the values together to get our new int-array.
          = (a << 24);
          = (r << 16);
          = (g << 8);
          = (b);
           
            
          0xFF000000 (a)
          + 0x00FF0000 (r)
          + 0x0000FF00 (g)
          + 0x000000FF (b)
          = 0xFFFFFFFF (argb)
           
          int ARGB = a+r+g+b;

          After the integer-array is recreated we can use the createRGBImage(..) method in the Image class to create our image.

          Be aware that the byte-array is quite large as it contains all of the ARGB values for each pixel. If an image is 100*60 px, where each pixel is four bytes, the byte array will be 24kb.
          example code

          posted on 2008-08-15 10:29 LukeW 閱讀(151) 評(píng)論(0)  編輯  收藏 所屬分類: Tips, Tricks, Hints & Code

          主站蜘蛛池模板: 峨眉山市| 武川县| 台东市| 吉水县| 灯塔市| 绵阳市| 哈尔滨市| 永登县| 保定市| 抚州市| 武城县| 游戏| 应用必备| 射阳县| 惠水县| 定日县| 阆中市| 新丰县| 石台县| 霸州市| 涞源县| 弥渡县| 信丰县| 黔东| 吉首市| 南城县| 乌鲁木齐县| 宝坻区| 汶川县| 东山县| 碌曲县| 光泽县| 南江县| 乌鲁木齐县| 崇州市| 哈密市| 克东县| 湘潭市| 谢通门县| 陇南市| 和顺县|