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
| import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import javax.imageio.ImageIO;
public class Snippet
{
public static void main(String args[]) throws IOException, ClassNotFoundException
{
int width = 640, height = 480, bpp = 3;
// Ici je cree un buffer et je le remplis d'un beau degradé.
ByteBuffer buffer = ByteBuffer.allocate(width*height*bpp);
createImage(buffer, width, height);
DataBufferByte dbb = new DataBufferByte(buffer.array(), buffer.capacity());
WritableRaster raster = Raster.createInterleavedRaster(
dbb,
width, height,
width * bpp,
bpp,
new int []{ 0, 1, 2 },
null);
ColorModel colorModel = new ComponentColorModel(
ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] { 8, 8, 8 },
false,
false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
BufferedImage bfImage = new BufferedImage(colorModel, raster, false, null);
ImageIO.write(bfImage, "png", new File("output.png"));
}
/**
* @param buffer
* @param width
* @param height
*/
private static void createImage(ByteBuffer buffer, int width, int height)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
byte r = (byte)((float)j / (float)width * 255);
byte g = (byte)((float)i / (float)height * 255);
byte b = 0;//(float)i / (float)width * 255;
buffer.put(new byte[]{r,g,b});
}
}
buffer.rewind();
}
}
/**
*
*/ |