Menu

Java API for "compare" command yields CommandExceptoin: 16

Help
Anonymous
2014-11-05
2014-11-06
  • Anonymous

    Anonymous - 2014-11-05

    I'm getting an "org.im4java.core.CommandException: 16" error attempting run a compare command as a Java application. It works fine when I run from the command line and from the generated script produced by calling ImageCommand.createScript().

    I'm running jdk1.7.0_67 on a MBPro OS X 10.9.5

    I'm using this dependency:
    <dependency>
    <groupId>org.im4java</groupId>
    <artifactId>im4java</artifactId>
    <version>1.4.0</version>
    </dependency>

    Here is my simple test Java app:

    public static void main(String[] args) {
    
        File image1 = new File("X_100x100.png");
        File image2 = new File("Green_X_100x100.png");
        File diffImage = new File("diff.png");
    
        ProcessStarter.setGlobalSearchPath("/ImageMagick-6.8.9/bin");
    
        IMOperation imOperation = new IMOperation();
        imOperation.metric("AE");
        imOperation.addImage(image1.getAbsolutePath());
        imOperation.addImage(image2.getAbsolutePath());
        imOperation.addImage("png:-");
    
        ImageMagickCmd compare = new ImageMagickCmd("compare");
        Stream2BufferedImage outputConsumer = new Stream2BufferedImage();
        compare.setOutputConsumer(outputConsumer);
    
        try {
            compare.createScript("myscript.sh", imOperation);
            compare.run(imOperation);
            System.out.println("Image compare output: " + compare.getErrorText().toString());
        } catch (IOException | InterruptedException | IM4JavaException e) {
            e.printStackTrace();
        }
    }
    

    I have no idea why I'm getting this exception. I do find it interesting that the value "16" reported in the exception is the exact number of different pixels between the test images.

    Also, does anyone know if there is a source.jar?

    Any help would be greatly appreciated.

     
  • Anonymous

    Anonymous - 2014-11-05

    I think maybe I'm misunderstanding how compare.run() should behave. It seems like it will "always" throw an exception which is just the return status of the underlying command-line compare command? Is this true?

     
  • Bernhard Bablok

    Bernhard Bablok - 2014-11-06

    Yes, you are right. If im4java detects output to stderr, it will automatically create a CommandException. The compare command writes the output of the -metric operator to stderr, so it will always throw an exception. Writing "normal" output to stderr is highly questionable, but this allows you to pipe the diff-image to stdout like you do in your code.

    If you really want robust code with the compare command, you have to catch the exception and inspect the error text. If it is just a number, you are fine, if it is more than that there is probably a real problem.

    BTW: full source code is available from this project site.

    Bernhard

     
  • Anonymous

    Anonymous - 2014-11-06

    Thank-you Bernhard!

    Kendall

     

Log in to post a comment.