This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Java Micro Edition and the fly likes Failed to load byte array of an png image into an midp application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Failed to load byte array of an png image into an midp application" Watch "Failed to load byte array of an png image into an midp application" New topic

Failed to load byte array of an png image into an midp application

Rishi Tyagi
Ranch Hand

Joined: Feb 14, 2002
Posts: 100
Hi All,
I am facing problem as heavy png files increases the size of my J2me application. No i tried to convert these png files to an byte array and then load this byte array at run time using the method
Image.createImage(byte[] imageData, int imageOffset, int imageLength)
But failed as when i use this image into my app it shows nothing in place of image. means i go blank screen.
Is the anybody who has worked on the same and can help me for the above problem.

Waiting for reply.
Rishi Tyagi
[ January 24, 2003: Message edited by: Rishi Tyagi ]
Paul Kelcey
Ranch Hand

Joined: Jan 26, 2003
Posts: 45
I don't completely understand your problem. You can either bundle a PNG file with your MIDlet suite (thus increasing the size of your JAR file) or you can use a byte array. This byte array must be the same format as a PNG file.
The question becomes, how are you creating this byte array? There are 2 obvious ways that I can think of including loading it versus creating it manually in memory. Loading it is just a variation of loading the PNG file however and so doesn't solve your problem. Creating it manually is a little painful and involves a little research.
I've gone through this later process and recommend you look at the PNG specification (version 1.0), RFC 1950 and RFC 1951 (for the compression stuff you'll need to understand for the IDAT chunk). It is possible to create a PNG file in memory on the devices and since one of the compression methods of the "deflate" algorithm is "uncompressed", you can use raw image data to populate your byte array.
You can use a pallette chunk (for indexed colour models) or not (for direct colour models). I suggest using a pallette colour model since it allows you to also use a transparency chunk (some devices support image transparency). Plus the image data is much smaller (one byte per pixel if you use 256 colours).
The big negative thing about creating the PNG image data in memory however is that you have to create a CRC (checksum) for each chunk. Whilst programming it is easy, it takes time to perform each time and it slows your app down. Also for IDAT chunk you need to create an Addler-32 checksum.
It's a pretty nasty and round about way of doing it but unless your using something like the Nokia gaming API, there's no other way to getting direct pixel access to the screen.
Vivek Viswanathan
Ranch Hand

Joined: Mar 03, 2001
Posts: 350
If you are reading the PNG image data from a HTTP stream make sure that you open a DataInputStream using
dis = httpCon.openDataInputStream();
// And the call the method to read the png image data
I faced a similar problem since I used to use the method 'read(b)' rahter than readFully(). You can look up the difference in both these methods in the doc.

Vivek Viswanathan SCJP 1.2, SCJP 1.6,SCJD,SCEA,SCWCD,IBM-484,IBM-486,IBM-141,Ms.NET C# 70-316,SCMAD, LPIC-I
Rishi Tyagi
Ranch Hand

Joined: Feb 14, 2002
Posts: 100
Thanks Paul & Vivek,
A very warm thankyou for the replies.
Now i am describing the application
1- My Application does not use HTTP Connection Means i does not interact with any back-end application.
2- What i want to do make a simple animated graphic-rich game (like car race or space war etc)
Now if i want to make this type of graphic rich application then i will have to use images because using j2me api i can't generate good quality of graphics through programming.

Now problem is
1- If i adds png images to the application then the size of the whole application increase to 10 times of the simple application as each png image file is of atleast 4kb to 10 kb, This big size of the application is not supported by most of the handsets.
2- I thought to read these png images into byte array then i write them into a txt file in the series of 0's and 1's , planned to read the same at runtime whenever i will have to put an image into the application.
For Reading Images :- I used simple java program to read the png image files and converted that into the array or bits.
Putting Image :- Now at run time i read the txt file and picked the whole stream of 0's and 1's (saved in txt file) in a byte array and used
Image.createImage(byte[] imageData, int imageOffset, int imageLength) method
to convert this byte array into the image
But failed as it shows nothing on the screen neither it gives any error.

I would be highly obliged if anybody can solve my this problem
can tell me the way how can i make Graphic rich and animated application in j2me more efficiently.
What is the right way to add a graphic in the j2me application without increasing the size of the applicatiopn too much.
With Best Regards,
[ March 10, 2003: Message edited by: Rishi Tyagi ]
Vivek Viswanathan
Ranch Hand

Joined: Mar 03, 2001
Posts: 350
That sounds like a really interesting and nice problem, you want to take this one Paul ?

Rishi Tyagi
Ranch Hand

Joined: Feb 14, 2002
Posts: 100
Yes Vivek,
You are right, its interesting, although i am in habit of enjoying the problems but right now bulbs in my mind has been pretended to be switched-off ,
That is why either i am thinking in a wrong way to solve the problem or there is some pitfals in my code and i am not able to find that.
Is there anybody who can guide me on the same

Looking forward for a positive response..
With Best Regards
Have you checked out Aspose?
subject: Failed to load byte array of an png image into an midp application
It's not a secret anymore!