Newer
Older
Zemi01 / src / main / java / info / istlab / Zemi01 / ScreenCapture.java
package info.istlab.Zemi01;

import java.awt.Desktop;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import info.istlab.Zemi01.imgview.ImageEditorFrame;

public class ScreenCapture {
    public static void capture(Rectangle captureRect, String dirPath) {
        Toolkit.getDefaultToolkit().beep();

        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdir();
        }
        File file = FileUtil.createUniqueFile(dir, "capture.png");

        try {
            Robot robot = new Robot();
            // // メインウィンドウの右にあることを想定するため、メインウィンドウの位置とサイズを取得
            // Point pos = MainWindow.frame.getLocation();
            // Dimension dim = MainWindow.frame.getSize();
            // Rectangle captureRect = new Rectangle(pos.x + dim.width, pos.y, 564, 322);
            // System.out.println(captureRect.toString());
            // Rectangle screenSize = new
            // Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
            // Rectangle screenSize = new
            // Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
            BufferedImage screenShot = robot.createScreenCapture(captureRect);
            ImageIO.write(screenShot, "png", file);


            // short[][][] brgb = bufferedImage2ByteArray(screenShot);
            // replace(screenShot, brgb); // 水色を黒(0x030303)に変換
            // // うさこれエリアを探索(画像中心から、上下左右にボーダーを探索していく。)
            // Rectangle area = getArea(screenShot);
            // System.out.println(area.toString());
            // if (area.width == 480 && area.height == 240) {
            //     byte[][] map = bitmap2map(screenShot, area);
            //     MainWindow.app.mem.setAry(map);
            //     MainWindow.app.repaint();
            // }

        } catch (Exception e) {
            System.out.println("キャプチャの取得に失敗しました。" + e.getMessage());
        }
        System.out.println("画面キャプチャを取得しました。" + "\r\n" + file.getPath());
        try {
            Desktop.getDesktop().open(file.getParentFile());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 編集ウィンドウを表示する。
        ImageEditorFrame ief = ImageEditorFrame.createFromFile(file, Launcher.frame);
        // ief.setSize(captureRect.getSize());
        ief.setLocation(captureRect.getLocation());
    }


    // private static byte[][] bitmap2map(BufferedImage screenShot, Rectangle area) {
    //     int x = area.x;
    //     int y = area.y;
    //     byte[][] map = new byte[20][10];
    //     // static Color[] cellColor = { Color.white, Color.cyan, Color.orange,
    //     // Color.green, Color.yellow, Color.MAGENTA };
    //     // 代表色 ただし、0,255,0は無い。見つからなかったらGreen=3とする
    //     short[][] cellcolor = { { 180, 180, 180 }, { 1, 186, 255 }, { 255, 167, 0 }, { 0, 255, 0 }, { 255, 255, 0 },
    //             { 255, 0, 41 } };
    //     short[][][] brgb = bufferedImage2ByteArray(screenShot);
    //     for (int j = 0; j < 10; j++) {
    //         for (int i = 0; i < 20; i++) {
    //             int xx = x + i * 24;
    //             int yy = y + j * 24;
    //             // 探索する色を固定する
    //             boolean isFound = false;
    //             for (int itype = 0; itype < 6; itype++) {
    //                 short[] findColor = cellcolor[itype];
    //                 for (int h = 0; h < 24; h++) { // xは xx+h y はyy+12 固定、横は1列のみチェックする。理由は耳の赤色をスキャンしないため。
    //                     if (diffsum(findColor, brgb[xx+h][yy+12]) < 30){
    //                         map[i][j] = (byte)itype; isFound = true;
    //                         break;
    //                     }
    //                 }
    //                 if (isFound){
    //                     break;
    //                 }
    //             }
    //             if (!isFound) map[i][j] = 3;
    //         }
    //     }
    //     for (int j = 0; j < 10; j++) {
    //         for (int i = 0; i < 20; i++) {
    //             System.out.print(map[i][j] + " ");
    //         }
    //         System.out.println("");
    //     }
    //     return map;
    // }
    public static int diffsum(short[] reference, short[] data){
        int diff = 0;
        for(int i=0;i<3;i++){
            diff += Math.abs(data[i] - reference[i]);
        }
        return diff;
    }

    public static Rectangle getArea(BufferedImage bi) {
        int[] xywh = new int[4];
        int w = bi.getWidth();
        int h = bi.getHeight();
        // xywh[0] = xywh[2] = w/2;
        // xywh[1] = xywh[3] = h/2;

        int[] searchX = { -1, 0, 1, 0 };
        int[] searchY = { 0, -1, 0, 1 };
        for (int i = 0; i < 4; i++) {
            int[] xy = new int[2];
            xy[0] = w / 2;
            xy[1] = h / 2;
            while (true) {
                System.out.println("search x " + xy[0] + " y " + xy[1] + " " + bi.getRGB(xy[0], xy[1]));
                if (bi.getRGB(xy[0], xy[1]) == -16579837) {
                    xywh[i] = (i % 2 == 0) ? xy[0] : xy[1];
                    System.out.println("i = " + i + "  val = " + xywh[i] + " =============");
                    break;
                }
                xy[0] += searchX[i];
                xy[1] += searchY[i];
            }
        }
        xywh[0]++;
        xywh[1]++;
        return new Rectangle(xywh[0], xywh[1], xywh[2] - xywh[0], xywh[3] - xywh[1]);

    }

    public static int[][] bufferedImage2IntArray(BufferedImage bi) {
        int w = bi.getWidth();
        int h = bi.getHeight();
        int[][] ret = new int[w][h];
        for (int j = 0; j < h; j++) {
            for (int i = 0; i < w; i++) {
                ret[i][j] = bi.getRGB(i, j);
            }
        }
        return ret;
    }

    public static short[][][] bufferedImage2ByteArray(BufferedImage bi) {
        int w = bi.getWidth();
        int h = bi.getHeight();
        short[][][] ret = new short[w][h][3];
        for (int j = 0; j < h; j++) {
            for (int i = 0; i < w; i++) {
                int pixel = bi.getRGB(i, j);
                ret[i][j][0] = (short) (pixel >> 16 & 0xff);
                ret[i][j][1] = (short) (pixel >> 8 & 0xff);
                ret[i][j][2] = (short) (pixel & 0xff);
                // System.out.println("r "+ret[i][j][0]+" g "+ret[i][j][1]+" b "+ret[i][j][2]);
            }
        }
        return ret;
    }

    public static void replace(BufferedImage bi, short[][][] brgb) {
        int w = bi.getWidth();
        int h = bi.getHeight();
        for (int j = 0; j < h; j++) {
            for (int i = 0; i < w; i++) {
                if (Math.abs(brgb[i][j][0] - 148) < 30 && Math.abs(brgb[i][j][1] - 217) < 60
                        && Math.abs(brgb[i][j][2] - 0xff) < 40) {
                    bi.setRGB(i, j, (3 << 16) + (3 << 8) + 3);
                }
            }
        }
    }
}

class FileUtil {
    public static File createUniqueFile(File directory, String fileName) {
        String name;
        String extention;
        if (fileName.contains(".")) {
            name = fileName.split("\\.")[0];
            extention = "." + fileName.split("\\.")[1];
        } else {
            name = fileName;
            extention = "";
        }
        int counter = 0;
        do {
            String uniqueName = String.format("%s%03d%s", name, ++counter, extention);
            File f = new File(directory, uniqueName);
            if (!f.exists()) {
                return f;
            }
        } while (true);
    }
}