| Reply | « Previous Thread | Next Thread » |
|
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;
}
|
| intheworldofmyown |
| View Public Profile |
| Find all posts by intheworldofmyown |
|
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(). |
| grahamhughes |
| View Public Profile |
| Find all posts by grahamhughes |
|
hi grahamhughes ,
so this means my im=Image.createImage(...) shud change to g.drawRGB() ? |
| intheworldofmyown |
| View Public Profile |
| Find all posts by intheworldofmyown |
|
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;
}
|
| grahamhughes |
| View Public Profile |
| Find all posts by grahamhughes |
|
if i just do the way you mention, add the above will become one image?
|
| intheworldofmyown |
| View Public Profile |
| Find all posts by intheworldofmyown |
|
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. |
| grahamhughes |
| View Public Profile |
| Find all posts by grahamhughes |
|
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();
}
|
| intheworldofmyown |
| View Public Profile |
| Find all posts by intheworldofmyown |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|