LoginSignup
4
3

More than 5 years have passed since last update.

im4javaで画像の幅高さを取得

Posted at

im4javaで画像のサイズを取得する
http://kyle-in-jp.blogspot.jp/2009/02/im4java_19.html

この記事を参考に実装したら処理が非常に重かったので改良。
重い原因はverboseオプションを付けて詳細情報を出しているからだと思われる。

public static int[] getImageSize(File src) {
    try {
        IMOperation op = new IMOperation();
        op.format("%wx%h");
        op.addImage(src.getAbsolutePath() + "[0]");

        IdentifyCmd identify = new IdentifyCmd();
        identify.setSearchPath(getImageMagickPath());

        ArrayListOutputConsumer output = new ArrayListOutputConsumer();
        identify.setOutputConsumer(output);
        identify.run(op);
        ArrayList<String> lines = output.getOutput();
        if (lines.size() > 0) {
            String line = lines.get(0);
            String sz[] = line.split("x");
            return new int[] { Integer.parseInt(sz[0]), Integer.parseInt(sz[1]) };
        }
    } catch (Exception e) {
    }
    return null;
}
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3