Created
May 12, 2019 15:26
-
-
Save LeRoiDesKiwis/26b3369b1a19f6aba1cfdb4f61e5bd8e to your computer and use it in GitHub Desktop.
Programme pour découper une image
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 fr.leroideskiwis.gifcreator; | |
import javax.imageio.ImageIO; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Scanner; | |
public class Main { | |
private Scanner scan = new Scanner(System.in); | |
private Main() throws IOException { | |
File file; | |
do { | |
String path = readString("Indiquez le chemin de l'image : "); | |
file = new File(path); | |
}while(!file.exists()); | |
int decX = readInt("Decalage en largeur : "); | |
int decY = readInt("Decalage en hauteur : "); | |
BufferedImage image = ImageIO.read(file); | |
List<BufferedImage> images = new ArrayList<>(); | |
int i = 0; | |
for(int yStart = 0; yStart < image.getHeight(); yStart+=decY) { | |
for(int xStart = 0; xStart < image.getWidth(); xStart+=decX) { | |
BufferedImage tmpImage = new BufferedImage(decX, decY, image.getType()); | |
i++; | |
System.out.println("Create image number " + i); | |
for (int x = 0; x < tmpImage.getWidth(); x++) { | |
for (int y = 0; y < tmpImage.getHeight(); y++) { | |
try { | |
tmpImage.setRGB(x, y, image.getRGB(xStart + x, yStart + y)); | |
} catch (Exception e) { | |
break; | |
} | |
} | |
} | |
tmpImage.flush(); | |
images.add(tmpImage); | |
} | |
} | |
flush(images); | |
} | |
private void flush(List<BufferedImage> images){ | |
images.forEach(img -> { | |
try { | |
ImageIO.write(img, "png", getFile("./name")); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
}); | |
} | |
public static void main(String[] args) throws IOException { | |
new Main(); | |
} | |
private File getFile(String name){ | |
File file = new File(name+".png"); | |
for(int i = 0; file.exists(); i++){ | |
file = new File(name+i+".png"); | |
} | |
return file; | |
} | |
private int readInt(String string){ | |
String str; | |
do{ | |
str = readString(string); | |
}while(!isNumber(str)); | |
return Integer.parseInt(str); | |
} | |
private boolean isNumber(String string){ | |
try { | |
Integer.parseInt(string); | |
return true; | |
}catch(Exception exception){ | |
return false; | |
} | |
} | |
private String readString(String string){ | |
System.out.print(string); | |
String readed = scan.nextLine(); | |
System.out.println(); | |
return readed; | |
} | |
} |
Évite de passer par un try catch pour tester si le paramètre est un nombre, et de toutes façons tu devrais utiliser Float.parseFloat. Utilise plutôt une regex
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exemple : découpage de cette image
: