Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
4
Help us understand the problem. What is going on with this article?

More than 5 years have passed since last update.

@nekonoprotocol

画像ファイルの形式を変更する

imageIOを使ってpngやgifをjpegに変更するプログラム。

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

String from = "変換前の画像パス";
String to   = "変換後の画像パス";
try {
    BufferedImage image = ImageIO.read(new File(from));
    BufferedImage tmp = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D off = tmp.createGraphics();
    off.drawImage(image, 0, 0, Color.WHITE, null);
    ImageIO.write(tmp, "jpg", new File(to));
} catch (Exception e) {
    System.out.println("error");
}

おまけ

ファイルの拡張子を変える方法(jpgに変更する例)。

import org.apache.commons.io.FilenameUtils;

String from; //変更前のパス
String to;   //変更後のパス

String to = String.format("%s%s.%s", FilenameUtils.getFullPath(to), FilenameUtils.getBaseName(from), "jpg");
4
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
4
Help us understand the problem. What is going on with this article?