2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Javaによる写真自動リサイズメソッド

Posted at

Javaでiconとして画像を使用するときに画像サイズとiconコンテンツのサイズが違う!そんなことはありませんか?

今回はiconとリサイズしたいサイズを指定すると自動的に変換した画像を返してくれるメソッドをご紹介します
まずはコード全体を

PictureBuilder.java

public class PictureBuilder {
	public static ImageIcon resizeIcon(ImageIcon icon, int w, int h){
		Image CGresize = icon.getImage();
		 BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		    Graphics2D g2 = resizedImg.createGraphics();
		    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		    g2.drawImage(CGresize, 0, 0, w, h, null);
		    g2.dispose();
		    ImageIcon resized = new ImageIcon();
		    resized.setImage(resizedImg);
		    return resized;
	}
}

使用方法としてはstatic関数なので外部のクラスなどからでも

ImageIcon icon = new ImageIcon("image.jpg");
Imageicon resized = PictureBuilder.resizeIcon(icon,100,200);

のようにして使用できます。

・とりあえず手軽にリサイズしたい。
・iconに画像を使いたいんだけどサイズがバラバラで使いにくい
・同じ画像ををいろいろなサイズのiconで使いたい

そういった方におすすめです。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?