Javaで画像ファイルをリサイズしたい場合はBufferedImageとGraphics2Dを使う。
http://www.mkyong.com/java/how-to-resize-an-image-in-java/
BufferedImage originalImage = null;
BufferedImage resizedImage = null;
Graphics2D g = null;
try {
int width = 200;
int height = 200;
originalImage = ImageIO.read(originalFile);
int type = originalImage.getType() == 0
? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
resizedImage = new BufferedImage(width, height, type);
g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, width, height, null);
g.dispose();
ImageIO.write(resizedImage, "jpg",
new File(imageFolder.getAbsolutePath()+"/"+iconName));
// post process
if( originalImage != null ) {
originalImage.flush();
originalImage = null;
}
// post process
if( resizedImage != null ) {
resizedImage.flush();
resizedImage = null;
}
} catch(Exception e) {
System.out.println("Can not generate " + originalFile.getName());
e.printStackTrace();
} finally {
if( originalImage != null ) {
originalImage.flush();
originalImage = null;
}
if( resizedImage != null ) {
resizedImage.flush();
resizedImage = null;
}
}
0 件のコメント:
コメントを投稿