1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class lol {
// ASSEMBLAGE des deux images
public static Image append(Image img1, Image img2) {
BufferedImage buf = null;
if(img1 != null && img2 != null) {
int w1 = img1.getWidth(null);
int h1 = img1.getHeight(null);
int w2 = img2.getWidth(null);
int h2 = img2.getHeight(null);
int hMax = 0;
int wMax = 0;
// hMax = h1 si h1>=h2 sinon hMax = h2
hMax = (h1 >= h2) ? h1 : h2;
wMax = w1+w2;
buf = new BufferedImage(wMax, hMax, BufferedImage.TYPE_3BYTE_BGR); // ligne 27
Graphics2D g2 = buf.createGraphics();
g2.drawImage(img1, 0, 0, null);
g2.drawImage(img2, w1, 0, null);
}
return buf;
}
public static void main(String[] args) throws InterruptedException {
ImageIcon img1 = new ImageIcon("x0y0b.jpg");
ImageIcon img2 = new ImageIcon("x0y1b.jpg");
ImageIcon image = new ImageIcon(append(img1.getImage(), img2.getImage())); // ligne 40
ImageToFile(image, "assemblage.jpg");
}
// CONVERSION de votre ImageIcon en BufferedImage et enregistrement
private static void ImageToFile(ImageIcon i, String file) {
BufferedImage output=ImageIconToBufferedImage(i.getImage());
File fic= new File(file);
try {
ImageIO.write(output, "jpg", fic);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// CONVERSION de votre ImageIcon en BufferedImage
private static BufferedImage ImageIconToBufferedImage(Image image) {
image = new ImageIcon(image).getImage();
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_RGB );
Graphics g = bufferedImage.createGraphics();
g.drawImage(image,0,0,null);
g.dispose();
return( bufferedImage );
}
} |
Partager