Skip to content

Instantly share code, notes, and snippets.

@AvatarQing
Last active August 29, 2015 14:07
Show Gist options
  • Save AvatarQing/58efe4b29e11321cfb22 to your computer and use it in GitHub Desktop.
Save AvatarQing/58efe4b29e11321cfb22 to your computer and use it in GitHub Desktop.
Java图片大小重置
@echo off
echo ======
echo The Argument Specification of ImageResizer.jar
echo arg1: Original image file path
echo arg2: Output image file path
echo arg3: Output image format. Only support JPEG,PNG,BMP,WBMP,GIF.
echo arg4: Output image width
echo arg5: Output image height
echo ------
echo Start converting...
java -jar "%~dp0ImageResizer.jar" %1 %~dp0%~n1_192x192.png PNG 192 192
java -jar "%~dp0ImageResizer.jar" %1 %~dp0%~n1_168x168.png PNG 168 168
java -jar "%~dp0ImageResizer.jar" %1 %~dp0%~n1_136x136.png PNG 136 136
java -jar "%~dp0ImageResizer.jar" %1 %~dp0%~n1_90x90.png PNG 90 90
java -jar "%~dp0ImageResizer.jar" %1 %~dp0%~n1_100x100.png PNG 100 100
java -jar "%~dp0ImageResizer.jar" %1 %~dp0%~n1_16x16.png PNG 16 16
pause
package org.sexytools;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageResizer {
public static final String FORMAT_JPEG = "JPEG";
public static final String FORMAT_PNG = "PNG";
public static final String FORMAT_BMP = "BMP";
public static final String FORMAT_WBMP = "WBMP";
public static final String FORMAT_GIF = "GIF";
public static final String[] SUPPORT_IMAGE_FORMAT = { FORMAT_JPEG,
FORMAT_PNG, FORMAT_BMP, FORMAT_WBMP, FORMAT_GIF };
private static final String OUT_IMAGE_FORMAT = FORMAT_PNG;
/***
* 调整图片大小
*
* @param srcImgPath
* 原图片路径
* @param distImgPath
* 转换大小后图片路径
* @param width
* 转换后图片宽度
* @param height
* 转换后图片高度
*/
public static void resizeImage(String srcImgPath, String distImgPath,
String format, int width, int height) throws IOException {
File srcFile = new File(srcImgPath);
File outFile = new File(distImgPath);
if (!srcFile.exists()) {
System.out.println("Original image does not exist");
}
if (!isSupportedImageFormat(format)) {
System.out.println("Unsupported image format : " + format);
}
outFile.getParentFile().mkdirs();
Image srcImg = ImageIO.read(srcFile);
BufferedImage buffImg = null;
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
buffImg.getGraphics().drawImage(
srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0,
0, null);
ImageIO.write(buffImg, format, outFile);
System.out.println(distImgPath);
}
private static boolean isSupportedImageFormat(String format) {
boolean support = false;
if (format != null) {
for (String supportedFormat : SUPPORT_IMAGE_FORMAT) {
if (supportedFormat.toLowerCase().equals(format.toLowerCase())) {
return true;
}
}
}
return support;
}
public static void convertDir(String dirPath, int width, int height) {
File dirFile = new File(dirPath);
if (!dirFile.exists()) {
throw new IllegalArgumentException("The directory does not exist");
}
String outDirName = dirFile.getName() + "_new";
String outDirPath = dirFile.getParent() + outDirName + File.separator;
for (File picFile : dirFile.listFiles()) {
if (picFile.isFile()) {
String outFilePath = outDirPath + getNewFileName(picFile);
try {
resizeImage(picFile.getAbsolutePath(), outFilePath,
OUT_IMAGE_FORMAT, width, height);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static String getNewFileName(File picFile) {
int lastDotIndex = picFile.getName().lastIndexOf(".");
String fileName = picFile.getName().substring(0, lastDotIndex) + "."
+ OUT_IMAGE_FORMAT.toLowerCase();
return fileName;
}
private static void printlnInstruction() {
System.out.println("======");
System.out.println("arg1:" + "\t" + "Original image file path");
System.out.println("arg2:" + "\t" + "Output image file path");
System.out.println("arg3:" + "\t"
+ "Output image format. Only support JPEG,PNG,BMP,WBMP,GIF.");
System.out.println("arg4:" + "\t" + "Output image width");
System.out.println("arg5:" + "\t" + "Output image height");
System.out.println("------");
}
private static void resizeSingleImage(String[] args) {
if (args == null || args.length < 5) {
System.out.println("Please enter valid arguments");
return;
}
String inputPath = args[0];
String outputPath = args[1];
String format = args[2];
int width = Integer.valueOf(args[3]);
int height = Integer.valueOf(args[4]);
try {
resizeImage(inputPath, outputPath, format, width, height);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void resizeMutipleImages(String[] args) {
if (args == null || args.length < 5) {
System.out.println("Please enter valid arguments");
return;
}
String inputDirPath = args[0];
String outputDirPath = args[1];
String format = args[2];
int width = Integer.valueOf(args[3]);
int height = Integer.valueOf(args[4]);
File inDirFile = new File(inputDirPath);
if (!inDirFile.exists()) {
throw new IllegalArgumentException("The directory does not exist");
}
File[] imageFiles = inDirFile.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
if (isImageFile(getFileNameSuffix(file.getName()))) {
return true;
} else {
return false;
}
}
private boolean isImageFile(String suffix) {
if (suffix != null) {
if (suffix.equals(FORMAT_BMP) || suffix.equals(FORMAT_GIF)
|| suffix.equals(FORMAT_JPEG)
|| suffix.equals(FORMAT_PNG)
|| suffix.equals(FORMAT_WBMP)) {
return true;
}
}
return false;
}
});
File outDirFile = new File(outputDirPath);
outDirFile.mkdirs();
for (File imageFile : imageFiles) {
String inputPath = imageFile.getAbsolutePath();
String outFileName = getFileNameWithoutSuffix(imageFile.getName())
+ "." + format;
String outputPath = outDirFile.getAbsolutePath() + File.separator
+ outFileName;
try {
resizeImage(inputPath, outputPath, format, width, height);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String getFileNameWithoutSuffix(String name) {
String result = null;
int index = -1;
if (name != null) {
index = name.lastIndexOf(".");
}
if (index != -1) {
try {
result = name.substring(0, index).toLowerCase();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
private static String getFileNameSuffix(String name) {
String result = null;
int index = -1;
if (name != null) {
index = name.lastIndexOf(".") + 1;
}
if (index != -1) {
try {
result = name.substring(index).toUpperCase();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static void main(String[] args) {
// printlnInstruction();
resizeSingleImage(args);
// resizeMutipleImages(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment