You Are Here:

Community: Developer Discussion Boards

#1 Old illegalArguementException - 2008-12-10, 05:24

Join Date: Nov 2008
Posts: 47
intheworldofmyown
Offline
Registered User
Hi, i want to combine my background and text into one image by converting it into bytes then back to an image, but by doing as the codes below, i encounter a illegalArguementException at the this line " im = Image.createImage(rawByte, 0, rawByte.length); "

is there any idea what happen?



Code:
    public Image createImageMethod() {
       
        Image blankImage = Image.createImage(getWidth(), getHeight());
        Graphics g = blankImage.getGraphics();
        g.setColor(0,0,0);
        try
        {
        Image backgroundImage= Image.createImage("/picture/background1.png");
         g.drawImage(backgroundImage, 0, 0, Graphics.HCENTER | Graphics.VCENTER);
        }catch(IOException e)
        {}
        
        g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL));
        g.drawString(text, x, y,Graphics.TOP|Graphics.LEFT);
       
       
        int width = blankImage.getWidth();
        int height = blankImage.getHeight();
        int y = 0;
 
           
        int raw[] = new int[height * width];
       blankImage.getRGB(raw, 0, blankImage.getWidth(), 0, 0, blankImage.getWidth(), blankImage.getHeight());
        byte rawByte[] = new byte[blankImage.getWidth() * blankImage.getHeight() * 4];
        int n = 0;
        for (int i = 0; i < raw.length; i++) {
            int ARGB = raw[i];
            int a = (ARGB & 0xff000000) >> 24;
            int r = (ARGB & 0xff0000) >> 16;
            int green = (ARGB & 0xff00) >> 8;
            int b = ARGB & 0xff;
            rawByte[n] = (byte) b;
            rawByte[n + 1] = (byte) green;
            rawByte[n + 2] = (byte) r;
            rawByte[n + 3] = (byte) a;
            n += 4;
        }
            

            
               // Create the image from the byte array
       im = Image.createImage(rawByte, 0, rawByte.length);    

        return im;
    }
Reply With Quote

#2 Old Re: illegalArguementException - 2008-12-10, 12:31

Join Date: Jun 2003
Posts: 4,322
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Online
Forum Nokia Champion
IllegalArgumentException from createImage() indicates that the data is not in a format that createImage() recognizes. createImage() is guaranteed to understand data in PNG format, and might understand other formats, like JPEG.

If you want to draw RGB data from getRGB(), you need to use Graphics.drawRGB().
Reply With Quote

#3 Old Re: illegalArguementException - 2008-12-10, 12:46

Join Date: Nov 2008
Posts: 47
intheworldofmyown
Offline
Registered User
hi grahamhughes ,

so this means my im=Image.createImage(...) shud change to g.drawRGB() ?
Reply With Quote

#4 Old Re: illegalArguementException - 2008-12-10, 12:57

Join Date: Jun 2003
Posts: 4,322
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Online
Forum Nokia Champion
I have no idea what you are trying to achieve. Why do you convert the image to a byte array, and then back to an Image?

Why not just:

Code:
public Image createImageMethod() {
    Image blankImage = Image.createImage(getWidth(), getHeight());
    Graphics g = blankImage.getGraphics();
    g.setColor(0, 0, 0);
    try {
        Image backgroundImage= Image.createImage("/picture/background1.png");
        g.drawImage(backgroundImage, 0, 0, Graphics.HCENTER | Graphics.VCENTER);
    } catch (IOException e) {
    }
        
    g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL));
    g.drawString(text, x, y, Graphics.TOP | Graphics.LEFT);

    return blankImage;
}
Graham.
Reply With Quote

#5 Old Re: illegalArguementException - 2008-12-10, 13:22

Join Date: Nov 2008
Posts: 47
intheworldofmyown
Offline
Registered User
if i just do the way you mention, add the above will become one image?
Reply With Quote

#6 Old Re: illegalArguementException - 2008-12-10, 13:29

Join Date: Jun 2003
Posts: 4,322
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Online
Forum Nokia Champion
It is already one image.

You are creating an image, then drawing another image onto it, then writing some text onto the image. End result: one image.
Reply With Quote

#7 Old Re: illegalArguementException - 2008-12-10, 19:10

Join Date: Nov 2008
Posts: 47
intheworldofmyown
Offline
Registered User
Hi, thanks for the help, i mange to do that and implement to my codes below by settig the three images frameImage1 -3 and allow it to implement to the gif encoder, base on the codes below am i doing the right way?

I am stuck till this point, as i allow the user to save this gif image into the local memory of the phone, how should i able to do it?

Thank you in advance =)

Code:
 public byte[] saveMethod(Image frameImage1,Image frameImage2,Image frameImage3) {
        System.out.println("HELLO");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        
        AnimatedGifEncoder encoder = new AnimatedGifEncoder();
        encoder.start(bos);
        encoder.addFrame(frameImage1);
        encoder.addFrame(frameImage2);
        encoder.addFrame(frameImage3);
        encoder.finish();
        
        return bos.toByteArray();
    }
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia