――Lors de la publication d'une photo sur Instagram, je veux en faire un carré avec une marge
f
--w
pour les marges blanches, b
pour les marges noires
--Si vous donnez des options et le répertoire d'image d'origine sur la ligne de commande, la sortie en PNG
--S'il s'agit d'un dossier, ciblez tous les fichiers image qu'il contient
--La sortie est dans le même répertoire que l'image d'origine, préfixée par resized_
image-resizer.java
package main;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.AreaAveragingScaleFilter;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.imageio.ImageIO;
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application {
public static boolean makeSquare = true;;
public static ArrayList<File> dirFiles = new ArrayList<File>();
public static BufferedImage inputImage;
public static double maxWidth = 1080;
public static double maxHeight = 1350;
public static int outputWidth;
public static int outputHeight;
public static int[] startCoodinate = {0, 0};
public static Color background;
public static String outputPreffix = "resized_";
public static File output;
public static String extension = "png";
public static List<String> readble = Arrays.asList("jpeg", "jpg", "gif", "png", "bmp");
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parameters params = getParameters();
List<String> args = params.getRaw();
switch(args.get(0)) {
case "f":
makeSquare = false;
break;
case "w":
background = Color.white;
break;
case "b":
background = Color.black;
break;
default:
System.out.println("Input 'f', 'w' or 'b' as 1st argument.");
System.exit(0);
}
if (args.size() > 1) {
for (int i = 1; i < args.size(); i++) {
System.out.println("Processing " + args.get(i) + ".");
if (checkDirectory(args.get(i))) {
for (File f: dirFiles) {
System.out.println("Processing " + f.getName() + ".");
initialize(f);
scaleImage(inputImage, output, outputWidth, outputHeight);
}
dirFiles.clear();
} else {
try {
initialize(new File(args.get(i)));
scaleImage(inputImage, output, outputWidth, outputHeight);
} catch (Exception e){
System.out.println(new File(args.get(i)).getName() + " could not be loaded.");
}
}
}
System.exit(0);
} else {
System.out.println("Input file or directory name as 2nd and later arguments.");
System.exit(0);
}
}
public static void initialize(File input) throws Exception {
inputImage = ImageIO.read(input);
calculateSize(inputImage);
String fileNameWithoutExtension = input.getName().substring(0, input.getName().lastIndexOf(".") + 1);
output = new File(input.getParent(), outputPreffix + fileNameWithoutExtension + extension);
}
public static void calculateSize(BufferedImage org) {
if (makeSquare) {
maxHeight = maxWidth;
}
double scale = Math.min(maxWidth / org.getWidth(), maxHeight / org.getHeight());
outputWidth = (int)(org.getWidth() * scale);
outputHeight = (int)(org.getHeight() * scale);
if (makeSquare) {
startCoodinate[0] = (int)Math.max(0, (maxWidth - outputWidth) / 2);
startCoodinate[1] = (int)Math.max(0, (maxHeight - outputHeight) / 2);
}
}
public static boolean checkDirectory(String dir) {
File inputDir = new File(dir);
if(inputDir.isDirectory()) {
for (File f: inputDir.listFiles()) {
if (readble.contains(f.getName().substring(f.getName().lastIndexOf(".") + 1).toLowerCase())) {
dirFiles.add(f);
} else {
System.out.println(f.getName() + " was skipped. Only JPG, GIF, PNG, BMP are supported.");
}
}
}
return inputDir.isDirectory();
}
public static void scaleImage(BufferedImage org, File out, int width, int height){
try {
ImageFilter filter = new AreaAveragingScaleFilter(width, height);
ImageProducer p = new FilteredImageSource(org.getSource(), filter);
Image dstImage = Toolkit.getDefaultToolkit().createImage(p);
if (makeSquare) {
outputWidth = (int)maxWidth;
outputHeight = (int)maxHeight;
}
BufferedImage dst = new BufferedImage((int)outputWidth , (int)outputHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = dst.createGraphics();
g.setColor(background);
g.fillRect(0, 0, dst.getWidth(), dst.getHeight());
g.drawImage(dstImage, Math.max(0, startCoodinate[0]), Math.max(0, startCoodinate[1]), null);
g.dispose();
ImageIO.write(dst, extension, out);
System.out.println(out.getName() + " was successed.");
} catch (IOException e) {
System.out.println(out.getName() + " could not be written.");
}
}
}
--Facile à utiliser les fichiers de commandes suivants sur les machines Windows
cmd / c" C: \ * \ image-resizer.bat "
dans la barre des tâchesimage-resizer.bat
@echo off
setlocal
set /p dir="Fichier ou dossier cible (plusieurs peuvent être spécifiés): "
set /p makeSqare="Souhaitez-vous ajouter une marge pour le rendre carré?(y/n): "
if %makeSqare%==y (
set /p firstAug="La couleur de la marge est blanche ou noire(w/b): "
)
if %makeSqare%==n (
set firstAug=f
)
echo Démarre le traitement
java -jar image-resizer.jar %firstAug% %dir%
echo Le PNG redimensionné est enregistré dans le même dossier que l'image d'origine
pause
Commentaire de saka1029 de Image Resize --Qiita