The code is hard to read, because it's not indented properly, but it seems you're processing one image at a time in memory, so if that causes an OutOfMemoryError, then you don't have much choice except increase the memory of your VM, or find a library that uses less memory to do the same thing.
To increase the heap memory, use the -Xmx
flag to launch your program. For example, for a heap of 512 MBs, you would use
java -Xmx512m my.MainClass