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 68 69 70 71 72 73 74 75 76 77 78 79 80
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testl;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.imageio.ImageIO;
/**
*
* @author fabriceb
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
BufferedImage image = gc.createCompatibleImage(500, 500, Transparency.TRANSLUCENT);
Graphics2D g2d = image.createGraphics();
g2d.setPaint(Color.RED);
g2d.fillOval(0, 0, image.getWidth(), image.getHeight());
g2d.dispose();
ImageIO.write(image, "png", new File("image.png"));
//String codeBase="http://..........";
//URL url = new URL(codeBase);
File file = new File("image.png");
URL url = file.toURI().toURL();
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedImage imgRes = ImageIO.read(bis);
if (imgRes != null) {
BufferedImage bImg = gc.createCompatibleImage(imgRes.getWidth(), imgRes.getHeight(), imgRes.getTransparency());
Graphics2D g2d1 = bImg.createGraphics();
g2d1.drawImage(imgRes, 0, 0, null);
g2d1.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(bImg, "png", baos);
} finally {
baos.close();
}
byte[] buffer = baos.toByteArray();
ImageIO.write(bImg, "png", new File("image1.png"));
//
InputStream is2 = new ByteArrayInputStream(buffer);
BufferedInputStream bis2 = new BufferedInputStream(is2);
BufferedImage imgRes2 = ImageIO.read(bis2);
ImageIO.write(imgRes2, "png", new File("image22.png"));
BufferedImage bImg2 = gc.createCompatibleImage(imgRes2.getWidth(), imgRes2.getHeight(), imgRes2.getTransparency());
Graphics2D g2d2 = bImg2.createGraphics();
g2d2.drawImage(imgRes2, 0, 0, null);
g2d2.dispose();
ImageIO.write(bImg2, "png", new File("image2.png"));
}
} catch (Exception e) {
// ecritureErreurDansFichierLog(e);
}
}
} |