「Java サムネイルイメージの作成」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「==Java サムネイルイメージの作成== [Java]{{category コード片}} import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; impo…」) |
|||
(同じ利用者による、間の2版が非表示) | |||
1行目: | 1行目: | ||
− | ==Java サムネイルイメージの作成== | + | ==[[Java サムネイルイメージの作成]]== |
− | [Java] | + | [[Java]] | [[Category:コード片]] |
import java.awt.geom.AffineTransform; | import java.awt.geom.AffineTransform; | ||
27行目: | 27行目: | ||
* @param rate | * @param rate | ||
* @throws IOException | * @throws IOException | ||
− | * @throws | + | * @throws ImageSer[[vi]]ceException |
*/ | */ | ||
public void writeThumbNailImage(InputStream in, | public void writeThumbNailImage(InputStream in, | ||
43行目: | 43行目: | ||
* @param rate | * @param rate | ||
* @throws IOException | * @throws IOException | ||
− | * @throws | + | * @throws ImageSer[[vi]]ceException |
*/ | */ | ||
public void writeThumbNailImage(InputStream in, | public void writeThumbNailImage(InputStream in, | ||
58行目: | 58行目: | ||
* @param height | * @param height | ||
* @throws IOException | * @throws IOException | ||
− | * @throws | + | * @throws ImageSer[[vi]]ceException |
*/ | */ | ||
public void writeThumbNailImage(InputStream in, | public void writeThumbNailImage(InputStream in, | ||
75行目: | 75行目: | ||
* @param height | * @param height | ||
* @throws IOException | * @throws IOException | ||
− | * @throws | + | * @throws ImageSer[[vi]]ceException |
*/ | */ | ||
public void writeThumbNailImage(InputStream in, | public void writeThumbNailImage(InputStream in, | ||
84行目: | 84行目: | ||
BufferedImage image = ImageIO.read(in); | BufferedImage image = ImageIO.read(in); | ||
− | double rate = | + | double rate = calc[[R]]atio(width, height, image); |
writeThumbNailImage(out, image, formatName, rate); | writeThumbNailImage(out, image, formatName, rate); | ||
} | } | ||
96行目: | 96行目: | ||
* @return | * @return | ||
*/ | */ | ||
− | private double | + | private double calc[[R]]atio(int width, int height, BufferedImage image) { |
− | double | + | double w[[R]]ate = (double) width / (double) image.getWidth(); |
− | double | + | double h[[R]]ate = (double) height / (double) image.getHeight(); |
− | return ( | + | return (w[[R]]ate < h[[R]]ate) ? w[[R]]ate : h[[R]]ate; |
} | } | ||
108行目: | 108行目: | ||
* @param rate | * @param rate | ||
* @throws IOException | * @throws IOException | ||
− | * @throws | + | * @throws ImageSer[[vi]]ceException |
*/ | */ | ||
public void writeThumbNailImage( OutputStream out, | public void writeThumbNailImage( OutputStream out, | ||
135行目: | 135行目: | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
− | if (args.length | + | if (args.length <= 3) { |
printUsage(); | printUsage(); | ||
} | } | ||
146行目: | 146行目: | ||
if (srcFilename.equals(dstFilename)) { | if (srcFilename.equals(dstFilename)) { | ||
− | System.out.println(" | + | System.out.println("E[[R]][[R]]O[[R]] : 変換前のファイル名と、変換後のファイル名が同じです"); |
} | } | ||
153行目: | 153行目: | ||
double ratio = 0.0; | double ratio = 0.0; | ||
− | for (int i = 0; i | + | for (int i = 0; i < args.length; i++) { |
if ("-w".equalsIgnoreCase(args[i].trim())) { | if ("-w".equalsIgnoreCase(args[i].trim())) { | ||
width = Integer.parseInt(args[i + 1]); | width = Integer.parseInt(args[i + 1]); | ||
167行目: | 167行目: | ||
ThumnailImageCreater me = new ThumnailImageCreater(); | ThumnailImageCreater me = new ThumnailImageCreater(); | ||
− | if (width | + | if (width > 0 || height > 0) { |
me.writeThumbNailImage(in, out, width, height); | me.writeThumbNailImage(in, out, width, height); | ||
− | } else if (ratio | + | } else if (ratio > 0) { |
me.writeThumbNailImage(in, out, ratio); | me.writeThumbNailImage(in, out, ratio); | ||
} else { | } else { | ||
− | System.out.println(" | + | System.out.println("E[[R]][[R]]O[[R]] : サイズが指定されていません"); |
} | } | ||
System.out.println("完了しました"); | System.out.println("完了しました"); |
2020年2月16日 (日) 04:27時点における最新版
Java サムネイルイメージの作成
Java |
import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.imageio.ImageIO; /** * サムネイルJpegを生成する * @author Yagi Hiroto */ public class ThumnailImageCreater { /** * InputSteramに関連付けられたイメージ * * @param in * @param out * @param formatName * @param rate * @throws IOException * @throws ImageServiceException */ public void writeThumbNailImage(InputStream in, OutputStream out, String formatName, double rate) throws Exception { BufferedImage image = ImageIO.read(in); writeThumbNailImage(out, image, formatName, rate); } /** * @param in * @param out * @param rate * @throws IOException * @throws ImageServiceException */ public void writeThumbNailImage(InputStream in, OutputStream out, double rate) throws Exception { writeThumbNailImage(in, out, "jpeg", rate); } /** * @param in * @param out * @param width * @param height * @throws IOException * @throws ImageServiceException */ public void writeThumbNailImage(InputStream in, OutputStream out, int width, int height) throws Exception { writeThumbNailImage(in, out, "jpeg", width, height); } /** * @param in * @param out * @param formatName * @param width * @param height * @throws IOException * @throws ImageServiceException */ public void writeThumbNailImage(InputStream in, OutputStream out, String formatName, int width, int height) throws Exception { BufferedImage image = ImageIO.read(in); double rate = calcRatio(width, height, image); writeThumbNailImage(out, image, formatName, rate); } /** * 指定されたサイズに収まる縮小率を算出する * * @param width * @param height * @param image * @return */ private double calcRatio(int width, int height, BufferedImage image) { double wRate = (double) width / (double) image.getWidth(); double hRate = (double) height / (double) image.getHeight(); return (wRate < hRate) ? wRate : hRate; } /** * @param out * @param image * @param formatName * @param rate * @throws IOException * @throws ImageServiceException */ public void writeThumbNailImage( OutputStream out, BufferedImage image, String formatName, double rate) throws Exception { BufferedImage shrinkImage = new BufferedImage( (int) (image.getWidth() * rate), (int) (image.getHeight() * rate), image.getType()); AffineTransformOp atOp = new AffineTransformOp( AffineTransform.getScaleInstance(rate, rate), null); atOp.filter(image, shrinkImage); boolean ret = ImageIO.write(shrinkImage, formatName, out); if (!ret) { throw new Exception("writer not found."); } } /** * @param args */ public static void main(String[] args) { if (args.length <= 3) { printUsage(); } FileInputStream in = null; FileOutputStream out = null; try { String srcFilename = args[0].trim(); String dstFilename = args[1].trim(); if (srcFilename.equals(dstFilename)) { System.out.println("ERROR : 変換前のファイル名と、変換後のファイル名が同じです"); } int width = 0; int height = 0; double ratio = 0.0; for (int i = 0; i < args.length; i++) { if ("-w".equalsIgnoreCase(args[i].trim())) { width = Integer.parseInt(args[i + 1]); } else if ("-h".equalsIgnoreCase(args[i].trim())) { height = Integer.parseInt(args[i + 1]); } else if ("-r".equalsIgnoreCase(args[i].trim())) { ratio = Double.parseDouble(args[i + 1]); } } in = new FileInputStream(new File(srcFilename)); out = new FileOutputStream(new File(dstFilename)); ThumnailImageCreater me = new ThumnailImageCreater(); if (width > 0 || height > 0) { me.writeThumbNailImage(in, out, width, height); } else if (ratio > 0) { me.writeThumbNailImage(in, out, ratio); } else { System.out.println("ERROR : サイズが指定されていません"); } System.out.println("完了しました"); } catch (Exception e) { e.printStackTrace(); printUsage(); } finally { try { if (in != null) in.close(); if (out != null) out.close(); } catch (Exception e) { } } } /** * */ private static void printUsage() { String usage = "usage ThumNailImage " + "originalfilename " + "shrinkfilename " + "[-w width]" + "[-h height]" + "[-r ratio]"; System.out.println(usage); System.exit(0); } }
© 2006 矢木浩人