この記事の例では、Java でサムネイルを作成し、画像の比率を拡大縮小する方法を説明します。皆さんの参考に共有してください。具体的な実装方法は以下の通りです。
このインスタンスは、イメージの幅と高さを指定した幅と高さに拡大縮小し、指定したディレクトリに保存することをサポートしています。イメージの縮小率は、ターゲット オブジェクトのサイズと標準 (指定した) イメージのサイズに基づいて計算されます。スケーリング品質を設定することができ、指定された画像の幅と高さに応じて画像のスケーリング品質を設定できます。
具体的なコードは次のとおりです。
次のようにコードをコピーします。
パッケージcom.hoo.util;
java.awt.Imageをインポートします。
インポート java.awt.image.BufferedImage;
java.io.ファイルをインポートします。
java.io.FileOutputStreamをインポートします。
インポート java.io.IOException;
インポート java.net.MalformedURLException;
java.net.URLをインポートします。
インポートjavax.imageio.ImageIO;
インポートcom.sun.image.codec.jpeg.ImageFormatException;
com.sun.image.codec.jpeg.JPEGCodec をインポートします。
com.sun.image.codec.jpeg.JPEGEncodeParam をインポートします。
com.sun.image.codec.jpeg.JPEGImageEncoder をインポートします。
/**
* <b>機能:</b> 画像のズーム ツール クラス、サムネイルの作成、画像の縦横比の伸縮
*@著者hoojo
* @createDate 2012-2-3 10:08:47 AM
* @file ScaleImageUtils.java
* @パッケージcom.hoo.util
* @バージョン1.0
*/
パブリック抽象クラス ScaleImageUtils {
プライベート静的最終フロート DEFAULT_SCALE_QUALITY = 1f;
private static Final String DEFAULT_IMAGE_FORMAT = ".jpg";
プライベート静的最終文字列 DEFAULT_FILE_PATH = "C:/temp-";
/**
* <b>機能:</b> 画像圧縮品質の列挙クラスを設定します。
* いくつかのガイドライン: 高品質 0.75、中品質 0.5、低品質 0.25
*@著者hoojo
* @createDate 2012-2-7 11:31:45 AM
* @file ScaleImageUtils.java
* @パッケージcom.hoo.util
* @プロジェクトJQueryMobile
* @バージョン1.0
*/
パブリック列挙型 ImageQuality {
最大(1.0f)、高(0.75f)、中(0.5f)、低(0.25f);
プライベートフロートの品質。
public Float getQuality() {
this.quality を返します。
}
ImageQuality(Float 品質) {
this.quality = 品質;
}
}
プライベート静的画像イメージ。
/**
* <b>機能:</b> 対象オブジェクトのサイズと標準 (指定された) サイズに基づいて画像の縮小率を計算します。
*@著者hoojo
* @createDate 2012-2-6 04:41:48 午後
* @param targetWidth ターゲットの幅
* @param targetHeight ターゲットの高さ
* @param standardWidth 標準(指定)幅
* @param standardHeight 標準(指定)の高さ
* @return 最小の適切な比率
*/
public static double getScaling(double targetWidth, double targetHeight, double standardWidth, double standardHeight) {
double widthScaling = 0d;
double heightScaling = 0d;
if (ターゲット幅 > 標準幅) {
widthScaling = standardWidth / (targetWidth * 1.00d);
} それ以外 {
widthScaling = 1d;
}
if (ターゲットの高さ > 標準の高さ) {
heightScaling = 標準高さ / (ターゲット高さ * 1.00d);
} それ以外 {
高さスケーリング = 1d;
}
return Math.min(widthScaling, heightScaling);
}
/**
* <b>機能:</b> 画像の幅と高さを指定された幅と高さに拡大縮小し、savePath ディレクトリに保存します。
*@著者hoojo
* @createDate 2012-2-6 04:54:35 PM
* @param width スケーリングの幅
* @param height ズーム高さ
* @param savePath 保存ディレクトリ
* @param targetImage ズーム対象の画像
* @return 画像保存パスと名前
* @throws ImageFormatException
* @throwsIOException
*/
public static String raise(int width, int height, String savePath, Image targetImage) throws ImageFormatException, IOException {
width = Math.max(width, 1);
高さ = Math.max(高さ, 1);
BufferedImage 画像 = new BufferedImage(幅, 高さ, BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(targetImage, 0, 0, width, height, null);
if (savePath == null || "".equals(savePath)) {
savePath = DEFAULT_FILE_PATH + System.currentTimeMillis() + DEFAULT_IMAGE_FORMAT;
}
FileOutputStream fos = new FileOutputStream(new File(savePath));
JPEGImageEncoder エンコーダ = JPEGCodec.createJPEGEncoder(fos);
エンコーダ.エンコード(画像);
画像.flush();
fos.flush();
fos.close();
保存パスを返します。
}
/**
* <b>機能:</b> 画像のスケーリング品質を設定し、指定された幅と高さに応じて画像を拡大縮小できます。
*@著者hoojo
* @createDate 2012-2-7 11:01:27 AM
* @param width スケーリングの幅
* @param height ズーム高さ
* @paramquality 画像圧縮品質、最大値は 1 です。列挙値を使用します: {@link ImageQuality}
* いくつかのガイドライン: 高品質 0.75、中品質 0.5、低品質 0.25
* @param savePath 保存ディレクトリ
* @param targetImage ズーム対象の画像
* @return 画像保存パスと名前
* @throws ImageFormatException
* @throwsIOException
*/
public static Stringsize(int width, int height, Floatquality, String savePath, Image targetImage) throws ImageFormatException, IOException {
width = Math.max(width, 1);
高さ = Math.max(高さ, 1);
BufferedImage 画像 = new BufferedImage(幅, 高さ, BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(targetImage, 0, 0, width, height, null);
if (savePath == null || "".equals(savePath)) {
savePath = DEFAULT_FILE_PATH + System.currentTimeMillis() + DEFAULT_IMAGE_FORMAT;
}
FileOutputStream fos = new FileOutputStream(new File(savePath));
JPEGImageEncoder エンコーダ = JPEGCodec.createJPEGEncoder(fos);
JPEGEncodeParam encodeParam = JPEGCodec.getDefaultJPEGEncodeParam(image);
if (品質 == null || 品質 <= 0) {
品質 = DEFAULT_SCALE_QUALITY;
}
/**画像圧縮品質を設定します*/
encodeParam.setQuality(品質、true);
encoder.encode(画像, encodeParam);
画像.flush();
fos.flush();
fos.close();
保存パスを返します。
}
/**
* <b>機能:</b> サイズと画像のサイズを指定して、画像の縮小に適したサイズを計算します。
*@著者hoojo
* @createDate 2012-2-6 05:53:10 PM
* @param width 指定幅
* @param height 指定された高さ
* @param image 画像ファイル
* @return 幅と高さの int 配列を返します
*/
public static int[] getSize(int width, int height, Image image) {
int targetWidth = image.getWidth(null);
int targetHeight = image.getHeight(null);
ダブルスケーリング = getScaling(targetWidth, targetHeight, width, height);
long standardWidth = Math.round(targetWidth * scaling);
long standardHeight = Math.round(targetHeight * scaling);
return new int[] { Integer.parseInt(Long.toString(standardWidth)), Integer.parseInt(String.valueOf(standardHeight)) };
}
/**
* <b>関数:</b> 指定された比率と画像オブジェクトを通じて拡大または縮小された幅と高さを返します。
*@著者hoojo
* @createDate 2012-2-7 10:27:59 AM
* @param スケールのスケーリング率
* @param 画像画像オブジェクト
* @return 幅、高さを返します
*/
public static int[] getSize(float スケール, Image image) {
int targetWidth = image.getWidth(null);
int targetHeight = image.getHeight(null);
long standardWidth = Math.round(targetWidth *scale);
長い標準高さ = Math.round(ターゲット高さ * スケール);
return new int[] { Integer.parseInt(Long.toString(standardWidth)), Integer.parseInt(String.valueOf(standardHeight)) };
}
public static int[] getSize(int width, Image image) {
int targetWidth = image.getWidth(null);
int targetHeight = image.getHeight(null);
長い高さ = Math.round((targetHeight * width) / (targetWidth * 1.00f));
return new int[] { width, Integer.parseInt(String.valueOf(height)) };
}
public static int[] getSizeByHeight(int height, Image image) {
int targetWidth = image.getWidth(null);
int targetHeight = image.getHeight(null);
長い幅 = Math.round((ターゲット幅 * 高さ) / (ターゲット高さ * 1.00f));
return new int[] { Integer.parseInt(String.valueOf(width)), height };
}
/**
*
* <b>機能:</b> 指定された targetFile 画像ファイルの、指定された幅と高さよりも大きい幅と高さを縮小し、savePath ディレクトリに保存します。
*@著者hoojo
* @createDate 2012-2-6 04:57:02 PM
* @param width 縮小幅
* @param height 縮小された高さ
* @param savePath 保存ディレクトリ
* @param targetImage はターゲット画像を変更しました
* @return 画像保存パスと名前
* @throws ImageFormatException
* @throwsIOException
*/
public static String raise(int width, int height, String savePath, File targetFile) throws ImageFormatException, IOException {
画像 = ImageIO.read(targetFile);
int[] size = getSize(幅, 高さ, 画像);
returnsize(size[0], size[1], savePath, image);
}
/**
*
* <b>機能:</b> 指定された targetURL ネットワーク イメージ ファイルの、指定された幅と高さより大きい幅と高さを縮小し、savePath ディレクトリに保存します。
*@著者hoojo
* @createDate 2012-2-6 04:57:07 PM
* @param width 縮小幅
* @param height 縮小された高さ
* @param savePath 保存ディレクトリ
* @param targetImage はターゲット画像を変更しました
* @return 画像保存パスと名前
* @throws ImageFormatException
* @throwsIOException
*/
public static String raise(int width, int height, String savePath, URL targetURL) throws ImageFormatException, IOException {
画像 = ImageIO.read(ターゲットURL);
int[] size = getSize(幅, 高さ, 画像);
returnsize(size[0], size[1], savePath, image);
}
/**
* <b>機能:</b> 指定された比率に従ってローカル画像ファイルを拡大縮小します。
*@著者hoojo
* @createDate 2012-2-7 10:29:18 AM
* @param スケールのスケーリング率
* @param savePath ファイルのパスと名前を保存します
* @param targetFile ローカル画像ファイル
* @return 新しいファイル名
* @throws ImageFormatException
* @throwsIOException
*/
public static Stringsize(floatscale,StringsavePath,FiletargetFile) throws ImageFormatException, IOException {
画像 = ImageIO.read(targetFile);
int[] サイズ = getSize(スケール, 画像);
returnsize(size[0], size[1], savePath, image);
}
/**
* <b>機能:</b> 指定された比率に従ってネットワーク イメージ ファイルを拡大縮小します。
*@著者hoojo
* @createDate 2012-2-7 10:30:56 AM
* @param スケールのスケーリング率
* @param savePath ファイルのパスと名前を保存します
* @param targetFile ローカル画像ファイル
* @return 新しいファイル名
* @throws ImageFormatException
* @throwsIOException
*/
public static Stringsize(floatscale,StringsavePath,URLtargetURL) throws ImageFormatException, IOException {
画像 = ImageIO.read(ターゲットURL);
int[] サイズ = getSize(スケール, 画像);
returnsize(size[0], size[1], savePath, image);
}
/**
* <b>機能:</b> 固定幅に従ってローカル画像を比例的に拡大縮小します。
*@著者hoojo
* @createDate 2012-2-7 10:49:56 AM
* @param width 固定幅
* @param savePath パスと名前を保存します
* @param targetFile ローカル ターゲット ファイル
* @return 保存パスを返します
* @throws ImageFormatException
* @throwsIOException
*/
public static String sinners(int width, String savePath, File targetFile) throws ImageFormatException, IOException {
画像 = ImageIO.read(targetFile);
int[] サイズ = getSize(幅, 画像);
returnsize(size[0], size[1], savePath, image);
}
/**
* <b>機能:</b> 固定幅に従ってネットワーク イメージを比例的に拡大縮小します。
*@著者hoojo
* @createDate 2012-2-7 10:50:52 AM
* @param width 固定幅
* @param savePath パスと名前を保存します
* @param targetFile ローカル ターゲット ファイル
* @return 保存パスを返します
* @throws ImageFormatException
* @throwsIOException
*/
public static String raise(int width, String savePath, URL targetURL) throws ImageFormatException, IOException {
画像 = ImageIO.read(ターゲットURL);
int[] サイズ = getSize(幅, 画像);
returnsize(size[0], size[1], savePath, image);
}
/**
*
* <b>機能:</b> 固定された高さに応じてローカル画像を比例的に拡大縮小します。
*@著者hoojo
* @createDate 2012-2-7 10:51:17 AM
* @param height 固定高さ
* @param savePath パスと名前を保存します
* @param targetFile ローカル ターゲット ファイル
* @return 保存パスを返します
* @throws ImageFormatException
* @throwsIOException
*/
public static String assignByHeight(int height, String savePath, File targetFile) throws ImageFormatException, IOException {
画像 = ImageIO.read(targetFile);
int[] サイズ = getSizeByHeight(高さ, 画像);
returnsize(size[0], size[1], savePath, image);
}
/**
* <b>機能:</b> 固定の高さに応じてネットワーク イメージを比例的に拡大縮小します。
*@著者hoojo
* @createDate 2012-2-7 10:52:23 AM
* @param height 固定高さ
* @param savePath パスと名前を保存します
* @param targetFile ローカル ターゲット ファイル
* @return 保存パスを返します
* @throws ImageFormatException
* @throwsIOException
*/
public static String assignByHeight(int height, String savePath, URL targetURL) throws ImageFormatException, IOException {
画像 = ImageIO.read(ターゲットURL);
int[] サイズ = getSizeByHeight(高さ, 画像);
returnsize(size[0], size[1], savePath, image);
}
/**
* <b>機能:</b>
*@著者hoojo
* @createDate 2012-2-3 10:08:47 AM
* @param 引数
* @throwsIOException
* @throws MalformedURLException
* @throws ImageFormatException
*/
public static void main(String[] args) throws ImageFormatException、MalformedURLException、IOException {
System.out.println(ScaleImageUtils.resize(140, 140, null, 新しい URL("http://www.open-open.com/lib/images/logo.jpg")));
ScaleImageUtils.resize(100, 100, ImageQuality.high.getQuality(), null, ImageIO.read(new URL("http://www.open-open.com/lib/images/logo.jpg")));
}
}
この記事が皆さんの Java プログラミングに役立つことを願っています。