Javaで画像をリアルタイム加工する方法【BufferedImage活用】

はじめに

Javaで画像を扱う場合、BufferedImage を使えば簡単にリアルタイム加工が可能です。

本記事では、以下の処理を実用コード付きで解説します:

  • フィルタ処理(グレースケール/反転)
  • 明るさ・コントラスト調整
  • リアルタイム表示(Java Swing)

開発環境

  • Java 8 以上
  • JDKのみで動作(外部ライブラリ不要)
  • IDE:Eclipse / IntelliJ / VS Code どれでもOK

基本:画像を読み込み表示する

BufferedImage image = ImageIO.read(new File("sample.jpg"));
JLabel label = new JLabel(new ImageIcon(image));

1. グレースケール処理の実装

public BufferedImage toGrayScale(BufferedImage src) {
    BufferedImage output = new BufferedImage(
        src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB
    );

    for (int y = 0; y < src.getHeight(); y++) {
        for (int x = 0; x < src.getWidth(); x++) {
            Color color = new Color(src.getRGB(x, y));
            int gray = (int)(color.getRed() * 0.3 +
                             color.getGreen() * 0.59 +
                             color.getBlue() * 0.11);
            Color grayColor = new Color(gray, gray, gray);
            output.setRGB(x, y, grayColor.getRGB());
        }
    }
    return output;
}

2. 色を反転する処理

public BufferedImage invertColors(BufferedImage src) {
    BufferedImage output = new BufferedImage(
        src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB
    );

    for (int y = 0; y < src.getHeight(); y++) {
        for (int x = 0; x < src.getWidth(); x++) {
            Color color = new Color(src.getRGB(x, y));
            Color inverted = new Color(
                255 - color.getRed(),
                255 - color.getGreen(),
                255 - color.getBlue()
            );
            output.setRGB(x, y, inverted.getRGB());
        }
    }
    return output;
}

3. 明るさを調整する処理

public BufferedImage adjustBrightness(BufferedImage src, int delta) {
    BufferedImage output = new BufferedImage(
        src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB
    );

    for (int y = 0; y < src.getHeight(); y++) {
        for (int x = 0; x < src.getWidth(); x++) {
            Color color = new Color(src.getRGB(x, y));
            int r = Math.min(255, Math.max(0, color.getRed() + delta));
            int g = Math.min(255, Math.max(0, color.getGreen() + delta));
            int b = Math.min(255, Math.max(0, color.getBlue() + delta));
            output.setRGB(x, y, new Color(r, g, b).getRGB());
        }
    }
    return output;
}

4. リアルタイムプレビュー(SwingでGUI)

public void showImageInWindow(BufferedImage img) {
    JFrame frame = new JFrame("Image Viewer");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new JLabel(new ImageIcon(img)));
    frame.pack();
    frame.setVisible(true);
}

実行例:すべての処理を組み合わせて表示

public static void main(String[] args) throws IOException {
    BufferedImage src = ImageIO.read(new File("sample.jpg"));

    ImageProcessor processor = new ImageProcessor();

    BufferedImage gray = processor.toGrayScale(src);
    BufferedImage inverted = processor.invertColors(gray);
    BufferedImage bright = processor.adjustBrightness(inverted, 50);

    processor.showImageInWindow(bright);
}

応用:リアルタイムフィルター切り替えUI(発展)

  • JButtonで各処理を選択可能にする
  • スライダーで明るさ調整を行う
  • マウスイベントと連携した部分加工

まとめ

BufferedImageColor クラスを使えば、Javaだけで画像加工の基本処理をリアルタイムで行うことが可能です。

業務システムへの組み込みや、スクレイピング画像の前処理にも応用できます。

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