Search code examples
javaalgorithmcompressionbrotli

Using different compression algorithms in java code


I've got a quick question. I have several images, all in bmp format, and I want to incorporate different compression algorithm options into an application I'm writing. I.e., let's say the user selects "Image1.bmp" and wants to compress and export it with either Brotli or JP2K. How could I incorporate those algorithms into their own listener functions so that the image is compressed according to that algorithm? I have the listeners set up and everything, so the only aspect of this problem I need help with is to actually include the algorithms. Is there a plugin I'm unaware of? I've looked a little at GeoTools, but that seems to only be applicable for JP2k.

Any insights would be greatly appreciated. Thank you very much!

Edit: I just had a thought. Is there a way that I can effectively run a command line request for certain algorithms from within a java function?


Solution

  • You can create an Interface then create classes to implement the algorithms you want:

    public interface ICompressionMethods
    {
        public void cMethod();
    }
    

    And then:

    public class Brotli implements ICompressionMethods
    {
        @Override
        public void cMethod()
        {
            // Your compression code here
        }
    }
    

    Now you need to implement each new different algorithm in a new class. The benefit of this approach is that you can pass one simple and similiar argument like ICompressionMethod to your methods and yet get different outcomes based on their implementation!