title: Convert a Bitmap image to grayscale within BlackBerry J2Me
body: <p>I have been trying to use the samples from here: <BR>
  <a href="https://stackoverflow.com/questions/2412054/j2me-convert-transparent-png-image-to-grayscale">J2ME: Convert transparent PNG image to grayscale</a><BR>
  and here:<BR>
  <a href="http://www.java2s.com/Code/Java/Collections-Data-Structure/intarraytobytearray.htm" rel="nofollow noreferrer">http://www.java2s.com/Code/Java/Collections-Data-Structure/intarraytobytearray.htm</a><BR></p>
  
  <p>to convert an Bitmap image object to grayscale on the fly but I am running into issues when I am trying to re-encode my byte to an image and I get the following error/stack:</p>
  
  <pre><code>(Suspended (exception IllegalArgumentException))    
  EncodedImage.createEncodedImage(byte[], int, int, String) line: 367 
  EncodedImage.createEncodedImage(byte[], int, int) line: 279 
  ScreenTemp.getGrayScaleImage(Bitmap) line: 404
  </code></pre>
  
  <p>Here is my code I am trying:</p>
  
  <pre><code>    Bitmap btemp = getGrayScaleImage(Bitmap.getBitmapResource("add.png"));
      BitmapField bftemp = new BitmapField(btemp, BitmapField.FOCUSABLE | BitmapField.FIELD_HCENTER | BitmapField.FIELD_VCENTER);
      add(bftemp);
  
  
      public Bitmap getGrayScaleImage(Bitmap image) {
      int width = image.getWidth();
      int height = image.getHeight();     
      int[] rgbData = new int[width * height];        
      image.getARGB(rgbData, 0, width, 0, 0, width, height);
      for (int x = 0; x &lt; width*height ; x++) {
          rgbData[x] = getGrayScale(rgbData[x]);
      }
      byte[] b = int2byte(rgbData);
      final EncodedImage jpegPic = EncodedImage.createEncodedImage(b, 0, b.length);
      return jpegPic.getBitmap();
  }
  private int getGrayScale(int c) {
      int[] p = new int[4];
      p[0] = (int) ((c &amp; 0xFF000000) &gt;&gt;&gt; 24); // Opacity level
      p[1] = (int) ((c &amp; 0x00FF0000) &gt;&gt;&gt; 16); // Red level
      p[2] = (int) ((c &amp; 0x0000FF00) &gt;&gt;&gt; 8); // Green level
      p[3] = (int) (c &amp; 0x000000FF); // Blue level
  
      int nc = p[1] / 3 + p[2] / 3 + p[3] / 3;
      // a little bit brighter
      nc = nc / 2 + 127;
  
      p[1] = nc;
      p[2] = nc;
      p[3] = nc;
  
      int gc = (p[0] &lt;&lt; 24 | p[1] &lt;&lt; 16 | p[2] &lt;&lt; 8 | p[3]);
      return gc;
  }
  private static byte[] int2byte(int[] src) {
      int srcLength = src.length;
      byte[]dst = new byte[srcLength &lt;&lt; 2];
  
      for (int i=0; i&lt;srcLength; i++) {
          int x = src[i];
          int j = i &lt;&lt; 2;
          dst[j++] = (byte) ((x &gt;&gt;&gt; 0) &amp; 0xff);           
          dst[j++] = (byte) ((x &gt;&gt;&gt; 8) &amp; 0xff);
          dst[j++] = (byte) ((x &gt;&gt;&gt; 16) &amp; 0xff);
          dst[j++] = (byte) ((x &gt;&gt;&gt; 24) &amp; 0xff);
      }
      return dst;
  }
  </code></pre>
  
  <p>Any help would be great!</p>
  
  <p>Thanks,
  Justin</p>
  
  <p>EDIT:
  Thanks to the below information I was able to fix this issue.  Here is the code.  You no longer need the int2byte and here is the updated the getGrayScaleImage method:</p>
  
  <pre><code>public Bitmap getGrayScaleImage(Bitmap image) {
      int width = image.getWidth();
      int height = image.getHeight();     
      int[] rgbData = new int[width * height];        
      image.getARGB(rgbData, 0, width, 0, 0, width, height);
      for (int x = 0; x &lt; width*height ; x++) {
          rgbData[x] = getGrayScale(rgbData[x]);
      }
      byte[] b = int2byte(rgbData);
      Bitmap bit = new Bitmap(width, height);
      bit.setARGB(rgbData, 0, width, 0, 0, width, height);
      return bit;
  }
  </code></pre>