Last active
December 18, 2015 07:19
-
-
Save tomoyamkung/5746194 to your computer and use it in GitHub Desktop.
[Java]ImageMagick を使って画像の縦横サイズを縮小するスニペット
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 縮小後の画像の縦横サイズ(単位はピクセル) | |
image.geometry = 240x240 | |
# ImageMagick の convert コマンドの絶対パス | |
imagemagick.path = C:/xxx/ImageMagick-6.8.3-Q16/convert.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.tomoyamkung; | |
import java.io.IOException; | |
import java.util.Properties; | |
public class AppProperties { | |
private static AppProperties instance; | |
/** 参照するプロパティファイル(クラスパス上のあること) */ | |
private static final String PROP_FILE_NAME = "app.properties"; | |
private Properties prop; | |
/** | |
* コンストラクタ。 | |
* | |
* シングルトン。 | |
* | |
* @throws IOException | |
*/ | |
private AppProperties() throws IOException { | |
prop = new Properties(); | |
prop.load(this.getClass().getClassLoader().getResourceAsStream(PROP_FILE_NAME)); | |
} | |
/** | |
* インスタンスを生成する。 | |
* | |
* @return AppProperties クラスのインスタンス | |
* @throws IOException インスタンスの取得に失敗した場合 | |
*/ | |
public static synchronized AppProperties getInstance() throws IOException { | |
if(instance == null) { | |
instance = new AppProperties(); | |
} | |
return instance; | |
} | |
public String get(String key) { | |
return prop.getProperty(key).trim(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.tomoyamkung; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ImageUtil { | |
/** | |
* ImageMagick を使って画像の縦横サイズを縮小する。 | |
* | |
* @param srcPath 縮小したい画像ファイルパス | |
* @param destPath 縮小した画像ファイルパス(このパスに画像ファイルが生成される) | |
* @throws IOException プロパティファイルの読み込みに失敗した場合 | |
* @throws InterruptedException ImageMagick の実行に失敗した場合 | |
*/ | |
public static void scaleDownFileSize(String srcPath, String destPath) throws IOException, InterruptedException { | |
AppProperties instance = AppProperties.getInstance(); | |
String imageMagickPath = instance.get("imagemagick.path"); | |
String geometry = instance.get("image.geometry"); | |
ProcessBuilder pb = new ProcessBuilder( | |
imageMagickPath, | |
"-geometry", geometry, | |
srcPath, destPath); | |
Process p = pb.start(); | |
p.waitFor(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment