Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6327553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:18:03+00:00 2026-05-24T17:18:03+00:00

I have been trying to use the samples from here: J2ME: Convert transparent PNG

  • 0

I have been trying to use the samples from here:

J2ME: Convert transparent PNG image to grayscale

and here:

http://www.java2s.com/Code/Java/Collections-Data-Structure/intarraytobytearray.htm

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:

(Suspended (exception IllegalArgumentException))    
EncodedImage.createEncodedImage(byte[], int, int, String) line: 367 
EncodedImage.createEncodedImage(byte[], int, int) line: 279 
ScreenTemp.getGrayScaleImage(Bitmap) line: 404

Here is my code I am trying:

    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 < 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 & 0xFF000000) >>> 24); // Opacity level
    p[1] = (int) ((c & 0x00FF0000) >>> 16); // Red level
    p[2] = (int) ((c & 0x0000FF00) >>> 8); // Green level
    p[3] = (int) (c & 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] << 24 | p[1] << 16 | p[2] << 8 | p[3]);
    return gc;
}
private static byte[] int2byte(int[] src) {
    int srcLength = src.length;
    byte[]dst = new byte[srcLength << 2];

    for (int i=0; i<srcLength; i++) {
        int x = src[i];
        int j = i << 2;
        dst[j++] = (byte) ((x >>> 0) & 0xff);           
        dst[j++] = (byte) ((x >>> 8) & 0xff);
        dst[j++] = (byte) ((x >>> 16) & 0xff);
        dst[j++] = (byte) ((x >>> 24) & 0xff);
    }
    return dst;
}

Any help would be great!

Thanks,
Justin

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:

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 < 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:18:03+00:00Added an answer on May 24, 2026 at 5:18 pm

    Quoting from the EncodedImage javadoc:

    If the image format is not recognized, an IllegalArgumentException is thrown.

    Why are you fiddling with EncodedImage? It seems like you ought to be able to just create a second Bitmap and use setARGB().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to use the hibernate dialect for SQLite from http://code.google.com/p/hibernate-sqlite/ in
I have been trying to use array_unique to remove duplicates from the search results
I have been trying to use the document.getElementByID to pull information from an HTML
I'm trying to use a list of clients that have been created from my
I have been trying to use date_format , however as far as I know,
I have been trying to use routes.rb for creating a URL /similar-to-:product (where product
I have been trying to use/save the boolean value of a checkbox in other
I have been trying to use 3rd party tool called visiblox for rendering charts
Right now I have been trying to use Launchpad's API to write a small
Ok I'm stumped. I have been trying to use Simpletip to create a tooltip

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.