| Create grayscale image on the fly in Java |
|
|
|
| Written by Charles | |||||
| Thursday, 29 October 2009 13:54 | |||||
|
It's often useful to be able to create an image on the fly from raw pixel data. The code below (download HERE) does this to produced a grayscale image, but you can produce an image in another colour space by changing the ColorSpace parameter when you create the ColorModel.
import java.awt.*;
import java.awt.color.*; import java.awt.image.*; import java.io.*; import javax.imageio.ImageIO; public class BMP { /** * (Testing only) */ public static void main(String[] args) throws Exception { BMP.makeMeCross(args[0]); } /** * * @param filename The file name for the 'crosshair' image * * @throws IOException If the file could not be written to by ImageIO */ public static void makeMeCross(String filename) throws IOException { int sz = 101; byte[] buffer = new byte[sz * sz]; for (int i = 0; i < sz; i++) { for (int j = 0; j < sz; j++) { // Make a 'crosshair' pattern buffer[(i * sz) + j] = ((j == 50) || (i == 50)) ? (byte) 255 : 0; } } ImageIO.write(BMP.getGrayscale(101, buffer), "PNG", new File(filename)); } /** * * @param width The image width (height derived from buffer length) * @param buffer The buffer containing raw grayscale pixel data * * @return THe grayscale image */ public static BufferedImage getGrayscale(int width, byte[] buffer) { int height = buffer.length / width; ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); int[] nBits = { 8 }; ColorModel cm = new ComponentColorModel(cs, nBits, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); SampleModel sm = cm.createCompatibleSampleModel(width, height); DataBufferByte db = new DataBufferByte(buffer, width * height); WritableRaster raster = Raster.createWritableRaster(sm, db, null); BufferedImage result = new BufferedImage(cm, raster, false, null); return result; } }
Only registered users can write comments!
Powered by !JoomlaComment 3.26
3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |



