Newbie
Newbie

Reputation: 4849

Image Manipulation - Java

I recently developed a Java Servlet running on Tomcat7 that is supposed to accept POST of pictures from different formats (PNG, JPG, BMP). And perform the following tasks:

As fastest solution I rely on ImageIO that was producing a decent result before i ran into more "new" formats. There are two main issues that I can not find a working solution for:

I evaluated different solutions but none of them seems to solve both solutions (i will list best two):

Has anyone of you been able to implement a solution that works with both formats?

Upvotes: 0

Views: 875

Answers (1)

ltlBeBoy
ltlBeBoy

Reputation: 1312

You could try im4java which also uses ImageMagick under the hood like JMagick. But instead of being a wrapper for the native libraries it uses the command line to communicate with ImageMagick.

ImageMagick has the operation -auto-orient which converts the image to the "correct" orientation automatically. For detailed information take a look at the documentation.

Advantages

  • Very fast, especially if ImageMagick was compiled with optimized libraries like libjpeg-turbo (standard e.g. in Debian distro)
  • Supports more ImageMagick operations than the JMagick versions included in distros like Debian (as of writing).

Disadvantages

  • Uses command line interface to communicate with ImageMagick which may be not allowed due to security restrictions.

Maven Dependency

    <dependency>
        <groupId>org.im4java</groupId>
        <artifactId>im4java</artifactId>
        <version>1.4.0</version>
    </dependency>

Example 1 (with files)

        // prepare operations
        final int width = 2000;
        final double quality = 85.0;
        final IMOperation op = new IMOperation();
        op.addImage("/path/to/input/image.jpg"); // input
        op.autoOrient();
        op.resize(width);
        op.quality(quality);
        op.addImage("/path/to/output/image.jpg"); // output (rotated image @ 85% quality)

        // create and execute command
        final ConvertCmd convertCmd = new ConvertCmd();
        convertCmd.run(op);

Example 2 (with input/output streams)

        // prepare operations
        final String outputFormat = "jpeg";
        final int width = 2000;
        final double quality = 85.0;
        final IMOperation op = new IMOperation();
        op.addImage("-"); // input: stdin
        op.autoOrient();
        op.resize(width);
        op.quality(quality);
        op.addImage(outputFormat + ":-"); // output: stdout

        // create and execute command
        final ConvertCmd convertCmd = new ConvertCmd();
        final Pipe inPipe = new Pipe(inputStreamWithOriginalImage,
                null); // no output stream, using stdin
        convertCmd.setInputProvider(inPipe);
        final Pipe outPipe = new Pipe(null, // no input stream, using stdout
                outputStreamForConvertedImage);
        convertCmd.setOutputConsumer(outPipe);
        convertCmd.run(op);

Example 3 (image information)

        final boolean baseInfoOnly = true;
        final Info info = new Info("/path/to/input/image.jpg",
                baseInfoOnly);
        System.out.println("Geometry: " + info.getImageGeometry());

Performance

You can also take a look at this question for performance optimizations (but beware that there was a -colorspace rgb and an unnecessary, because not supported for JPEG images, -coalesce operation which both increased the processing time).

Documentation

Here you can find the developers's guide with more examples.

Upvotes: 0

Related Questions