Reputation: 4849
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
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.
<dependency>
<groupId>org.im4java</groupId>
<artifactId>im4java</artifactId>
<version>1.4.0</version>
</dependency>
// 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);
// 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);
final boolean baseInfoOnly = true;
final Info info = new Info("/path/to/input/image.jpg",
baseInfoOnly);
System.out.println("Geometry: " + info.getImageGeometry());
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).
Here you can find the developers's guide with more examples.
Upvotes: 0