sikuliX javaAPI
package Sample;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JFrame;
import org.sikuli.basics.Settings;
import org.sikuli.script.App;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Location;
import org.sikuli.script.Match;
import org.sikuli.script.Region;
import org.sikuli.script.Screen;
public class Main{
public static Screen screen;
public static Region region;
static {
screen = new Screen();
region = screen;
}
public static void main(String[] args)throws IOException, InterruptedException {
clickCapture();
}
/**
* マウスポインタの移動速度を変更する。
* 高速:0
*数字が大きくなるにつれ遅くなる。
* @param count
*/
public static void setMoveMouseDelay(float count) {
Settings.MoveMouseDelay = count;
}
/**
* マウスポインタの移動速度を取得する。
*/
public static float getMoveMouseDelay() {
return Settings.MoveMouseDelay;
}
/**
*画像認識時の類似度を取得する。
*/
public static double getMinSimilarity() {
return Settings.MinSimilarity;
}
/**
*画像認識時の類似度を変更する。
* 0 ~ 0.99
*/
public static void setMinSimilarity(double count) {
Settings.MinSimilarity = count;
}
/**
*ディレクトリの作成
*@param 作成するディレクトリのパス
*@return 作成したディレクトリのパス
*/
public static String createDirectory(String path) throws RuntimeException{
try {
Files.createDirectories(Paths.get(path));
} catch (IOException e) {
throw new RuntimeException();
}
return path;
}
/**
* 時間文字列取得
* yyyy_MM_dd_HH_mm_ss
*/
public static String getTimeString() {
return getTimeString("yyyy_MM_dd_HH_mm_ss");
}
/**
* 時間文字列取得
*
* @param format
*/
public static String getTimeString(String format) {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(format));
}
/**
* 画像認識時の認識範囲を表示
* @param r
* @return
*/
public static Region highlight(Region r) {
return r.highlight();
}
/**
* 指定した画像が画面内に複数ある場合、すべて取得する。
* @param path
* @return
*/
public static Iterator<Match> findAll(String path ) {
Iterator<Match> m = null;
try {
m = screen.findAll(path);
}catch(FindFailed e) {
e.printStackTrace();
}
return m;
}
/**
* 指定した複数画像の中で最も類似度の高い画像のMatchオブジェクトを返す。
* @param paths
* @return
*/
public static Match findBest(String... paths) {
return screen.findBest(paths);
}
/**
* メインディスプレイをキャプチャし、pathへ保存する。
* @param path
*/
public static void capture(String path) {
screen.capture().save(path,"{"+getTimeString()+"}");
}
//==================================================================
//=====================
=キャプチャツール========================== //==================================================================
public static JFrame jframe;
public static Location location;
public static boolean executeFlg = false;
public static String capureSavePath = "E:\\Log\\capture\\";
/**
* キャプチャツール(メイン)
*/
private static void clickCapture() {
jframe = getFrame(0,screen.h-50,200,50);
setJFrameButton();
jframe.setBackground(new Color(0,0,0,1f));
jframe.setVisible(true);
executeFlg = true;
try {Thread.sleep(1000);} catch (InterruptedException e) {}
while(executeFlg){
if(executeFlg==false) {
System.out.println("test");
break;
}
}
jframe.setVisible(false);
jframe.dispose();
return;
}
/**
* ボタン設定(キャプチャツール)
*/
private static JFrame setJFrameButton() {
JButton b1 = new JButton("キャプチャ");
b1.addActionListener(new captureActionListener());
JButton b2 = new JButton("閉じる");
b2.addActionListener(new captureActionListener());
JButton b3 = new JButton("保存先");
b3.addActionListener(new captureActionListener());
//コンテンツ区画の取得
Container cont = jframe.getContentPane();
FlowLayout flow = new FlowLayout();
cont.setLayout(flow);
cont.add(b1);
cont.add(b2);
cont.add(b3);
return jframe;
}
/**
* JFrameを設定、x軸、y軸、横幅、縦幅
* @param locationX
* @param locationY
* @param sizeX
* @param sizeY
* @return
*/
private static JFrame getFrame(int locationX,int locationY,int sizeX,int sizeY) {
JFrame anchor = new JFrame();
anchor.setAlwaysOnTop(true); //常に最前線へ
anchor.setUndecorated(true); //ヘッダを消去
anchor.setSize(sizeX,sizeY); //立幅、横幅
anchor.setLocation(locationX,locationY); //x,y軸
anchor.setVisible(true); //表示
return anchor;
}
/**ボタンアクション(キャプチャツール)*/
public static class captureActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("キャプチャ")) {
jframe.setVisible(false);
try {Thread.sleep(10);} catch (InterruptedException e1) {}
createDirectory(capureSavePath);
capture(capureSavePath);
try {Thread.sleep(10);} catch (InterruptedException e1) {}
jframe.setVisible(true);
}else if(e.getActionCommand().equals("保存先")) {
new Thread(()->{
App.run("EXPLORER.EXE E:\\Log\\capture");
}).start();
}else {
jframe.setVisible(false);
executeFlg = false;
jframe.dispose();
System.exit(1); //裏で動作し続ける挙動を防ぐ
}
}
}
//================================================================== //==================================================================
}