I have been experimenting with Image processing for a while.I came across a whole range of problems while doing the same.Most of them were concerned with the limited memory.I read a lot on best practises for memory management,but none of them were specific to image processing.Here are some of the problems I encountered.
1.) Reading a JPEG file
The 2-4 MB heap space made it almost impossible to decompress a JPEG image.I was forced to use a scaled down version of the original.
2.) Getting Snapshot from the camera
The snapshot obtained from the camera is of a lower resolution than the maximum resolution possible.
3.) Manipulating the image(Image filters)
For performing image filters, I had to store two versions of the image in memory.One was the original snapshot obtained.Other was the image obtained after applying a particular effect(say grayscale).The memory problem became very prominent from here.For simple effects,it was okay.But for complex effects, retaining even two images in memory became impossible.
Most of the techniques for efficient memory management in J2ME were not useful in this situation.Weak references were useless as I need to keep both images alive in the memory.I have set all the useless variables to null as soon as their purpose is done.I inclued calls to System.gc() in many places.It was still not enough.The only option I had was to scale down the images so that there was enough memory to store both of them
4.) Storing the image in file system.
Even if there is enough memory to store two images,storing the image posed another problem.I had to use either a PNG or a JPEG encoder.The encoding process required some amount of memory.The application crashed due to lack of memory.
Once again, I had to scale down the image in order to make enough room for the encoding process to happen.
For all the problems , only solution I was able to find was to scale down the image in a huge way.This is really disappointing as the quality of the final image is very very bad when compared to the original.The users of my app are very unhappy because of this.
Is there any other way to solve this problem? Is there any best practises I should follow while handling camera images?