ImageUtility-VRobot2

この記事は約8分で読めます。

[java]
package com.varlal.VRobot.small;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Point;
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 javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ImageUtility {
// rgbに関するユーティリティ
public static int a(int c) {
return c >>> 24;
}

public static int r(int c) {
return c >> 16 & 0xff;
}

public static int g(int c) {
return c >> 8 & 0xff;
}

public static int b(int c) {
return c & 0xff;
}

public static int rgb(int r, int g, int b) {
return 0xff000000 | r << 16 | g << 8 | b;
}

public static int argb(int a, int r, int g, int b) {
return a << 24 | r << 16 | g << 8 | b;
}

/****************************************/
// 画像読み書きに関するユーティリティ
/****************************************/
// 画像ファイルを読み込むメソッド
public static BufferedImage imgRead(String file_pass) {
BufferedImage img = null;
try {
img = ImageIO.read(new File(file_pass));
return img;
} catch (Exception e) {
return null;
}
}

// 画像ファイルを2次元配列に変換するメソッド
public static int[][] imgToArray(BufferedImage img) {
int width = img.getWidth();
int height = img.getHeight();
int[][] imgA = new int[height][width];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
imgA[y][x] = img.getRGB(x, y); // 画像上の(x, y)におけるRGB値を取得
}
}
return imgA;
}

// RGB値からグレースケールに変換するメソッド
public static int[][] trans_grayscale(int[][] img) {
int width = img[0].length;
int height = img.length;

int[][] gray_img = new int[height][width];
for (int y = 0; y < img.length; y++) {
for (int x = 0; x < img[0].length; x++) {
int rgb = img[y][x] – 0xFF000000; // アルファ値を取り除く
int b = (rgb & 0xFF); // 青の成分を取得
int g = (rgb & 0xFF00) >> 8; // 緑の成分を取得
int r = (rgb & 0xFF0000) >> 16; // 赤の成分を取得
int gray = (b + g + r) / 3; // グレーの値に変換
gray_img[y][x] = gray;
}
}
return gray_img;
}

// スクリーンショット 4パターン – 戻り値としてBufferedImage
// 引数にsave_pathがあれば、保存する
// 引数にrectangleでの範囲指定があれば、すべてでなく範囲を選択する

// ①Rectangleサイズ指定:保存なし
public static BufferedImage screenshot(Rectangle rectangle) {
Robot robot;
try {
robot = new Robot();
BufferedImage image = robot.createScreenCapture(rectangle);
return image;
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

// ②Rectangleサイズ指定:保存あり
public static BufferedImage screenshot(Rectangle screenSize, String save_path) {
BufferedImage image = screenshot(screenSize);
save_image(save_path, image);
return image;
}

// ③画面全体キャプチャ:保存なし
public static BufferedImage screenshot() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
return screenshot(new Rectangle(new Point(0, 0), screenSize));
}

// ④画面全体キャプチャ:保存あり
public static BufferedImage screenshot(String save_path) {
BufferedImage image = screenshot();
save_image(save_path, image);
return image;
}

// 画像の保存
private static void save_image(String save_path, BufferedImage image) {
try {
ImageIO.write(image, "PNG", new File(save_path));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// 画像の表示(ファイルパス・BufferedImage・二次元配列(まだ))
public static void show_image(String file_pass) {
JFrame f = new JFrame("show_image " + file_pass);
JLabel label = new JLabel();
ImageIcon icon = new ImageIcon(file_pass);
label.setIcon(icon);
f.getContentPane().add(label);
f.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
f.setVisible(true);
}

public static void show_image(BufferedImage img) {
JFrame f = new JFrame("show_image – form BufferedImage");
JLabel label = new JLabel();
ImageIcon icon = new ImageIcon();
icon.setImage(img);
label.setIcon(icon);
f.getContentPane().add(label);
f.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
f.setVisible(true);
}

}
[/java]

コメント

タイトルとURLをコピーしました