I am using im4java version 1.4.0 to access ImageMagick functionality from Java. It is working well for processing images to and from files.
The Developers Guide has a section on using Buffered Images instead of writing output to a file, and there is a test (TestCase13) that demonstrate using Buffered Images as output. However, when I run any action with a Buffered Image I receive an org.im4java.core.CommandException
stating: no ImageReader for given format
.
I have tried a number of different things (including adding the jai_imageio.jar
to provide additional formats), but nothing seems to work. A basic test-code that shows the problem, (based on im4java.jar's TestCase13) is:
@Test
public void shouldWorkWithBufferedImageTest() throws InterruptedException, IOException, IM4JavaException {
ProcessStarter.setGlobalSearchPath("C:\\Program Files\\ImageMagick-6.8.9-Q8");
String iImageDir = "C:\\images";
String var1 = "png";
IMOperation imOp = new IMOperation();
imOp.addImage(new String[]{iImageDir + "sample-image=6.png"});
imOp.blur(Double.valueOf(2.0D)).paint(Double.valueOf(10.0D));
imOp.addImage(new String[]{var1 + ":-"});
ConvertCmd convertCmd = new ConvertCmd();
Stream2BufferedImage stream2BufferedImage = new Stream2BufferedImage();
convertCmd.setOutputConsumer(stream2BufferedImage);
convertCmd.run(imOp, new Object[0]);
BufferedImage outImage = stream2BufferedImage.getImage();
ImageIO.write(outImage, "PNG", new File(iImageDir + "tmpfile.png"));
DisplayCmd.show(iImageDir + "tmpfile.png");
}
Running this throws the following error:
org.im4java.core.CommandException: java.lang.IllegalStateException: no ImageReader for given format
at org.im4java.core.ImageCommand.run(ImageCommand.java:219)
at test.groovy.services.ImageManipulation.JavaBufferedImageManipulationTest.shouldWorkBufferedImageTest(JavaBufferedImageManipulationTest.java:31)
Caused by: java.lang.IllegalStateException: no ImageReader for given format
at org.im4java.core.Stream2BufferedImage.consumeOutput(Stream2BufferedImage.java:82)
at org.im4java.process.ProcessStarter.processOutput(ProcessStarter.java:276)
at org.im4java.process.ProcessStarter.access$200(ProcessStarter.java:54)
at org.im4java.process.ProcessStarter$2.call(ProcessStarter.java:433)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.lang.Thread.run(Thread.java:745)
The error occurs on the the convertCmd.run
line, however, the problem seems to be with setting the outputConsumer as the stream2BufferedImage. How do I fix this? Is there a known bug with im4java and BufferedImages? Is there a better work-around than exporting to a temp-file and then reading it back in to a BufferedImage? I am aware of JMagick (as an alternative to im4java) but have not found this a good solution for other reasons.
Thanks, in advance, for any assistance or ideas.
see in this example i have the input source as buffered Image and output is also as buffered image . I hope this can help you.
public static void main(String... args) throws Exception {
IMOperation op = new IMOperation();
op.addImage();
op.resize(350)
op.addImage("png:-")
BufferedImage images = ImageIO.read(new File("image.jpg"));
// set up command
ConvertCmd convert = new ConvertCmd();
Stream2BufferedImage s2b = new Stream2BufferedImage();
convert.setOutputConsumer(s2b);
// run command and extract BufferedImage from OutputConsumer
convert.run(op,images);
BufferedImage img = s2b.getImage();
}