Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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");
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした