Pitaliumの画像比較で一部を除外する
Pitaliumではスクリーンショットを撮影して、二つの画像を比較することで更新された部分があるかどうかをチェックします。しかし画像だけでの比較の場合、バナーであったりソーシャル系の情報は利用したユーザやタイミングによって情報が異なるために毎回差分が出てしまいます。
そこで使いたいのがチェック対象外にする設定です。この手の仕組みとして良く行われるのが除外領域を黒く塗りつぶすといった方法なのですが、Pitaliumでは除外したい部分の透明度を変更すればOKです。
準備するもの
必要なライブラリは以下の通りです。
これらのライブラリが必要です。すべて同じディレクトリに配置するものとします。
テストコード
テストコードは次のようになります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import javax.imageio.ImageIO; | |
import java.awt.image.BufferedImage; | |
import com.htmlhifive.pitalium.core.*; | |
import com.htmlhifive.pitalium.image.util.*; | |
import com.htmlhifive.pitalium.image.model.*; | |
class Diff { | |
public static void main(String args[]){ | |
try { | |
// 正とする画像 | |
BufferedImage Image1 = ImageIO.read(new File(args[0])); | |
// 比較対象の画像 | |
BufferedImage Image2 = ImageIO.read(new File(args[1])); | |
// 比較実行 | |
ImageComparedResult result = ImageUtils.compare( | |
Image1, null, Image2, null, | |
new CompareOption[] { | |
new CompareOption( | |
CompareOptionType.IGNORE_CLEAR_PIXELS | |
) | |
} | |
); | |
// 結果の確認 | |
if (result.isFailed()) { | |
// 違いがある場合 | |
// 差分を書き出し | |
DiffPoints diffPoints = (DiffPoints) result; | |
final BufferedImage diffImage = ImageUtils.getDiffImage( | |
Image1, | |
Image2, | |
diffPoints | |
); | |
// ファイルに書き出し | |
ImageIO.write(diffImage, "png", new File(args[2])); | |
} | |
} catch(IOException e) { | |
System.err.println(e.getMessage()); | |
} | |
} | |
} |
注意点としては比較時のオプションです。 CompareOptionType.IGNORE_CLEAR_PIXELS
を使います。
コンパイル
コンパイル時には pitalium-*.jar がクラスパスに追加されている必要があります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ javac -cp ".:./pitalium-1.2.1.jar" diff.java |
実行
実行時には slf4j をクラスパスに追加して実行する必要があります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ java -cp ".:./pitalium-1.2.1.jar:./slf4j-simple-1.7.25.jar:./slf4j-api-1.7.25.jar" \ | |
Diff diff1.png diff2.png diff3.png |
今回のファイルは以下の二つです。
二つ目の画像の右側のアイコンだけ透明度を変更しています。
実行すると一瞬GUIアプリケーションが立ち上がった後、差分ファイルが書き出されます。右の部分は色が明らかに違いますが、差分から除外されているのが分かるかと思います。
このようにして二つの画像から一部を除いた比較も簡単にできます。ぜひ皆さんの開発に役立ててください。
コメントは受け付けていません。