package image.processing;

import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.*;

/**
 * Convert a Byte[] to a BufferedImage
 * @author Aaron Hayes
 */
public class ByteArrayToBufferedImage {

    /**
     * Convert image
     * @param bytes Byte[] to be converted
     */
    public static BufferedImage Convert(byte[] bytes, int width, int height) {
        ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        int[] bits =  { 8 };
        ColorModel colorModel = new ComponentColorModel(colorSpace, bits, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
        SampleModel sampleModel = colorModel.createCompatibleSampleModel(width, height);
        DataBufferByte dataBufferByte = new DataBufferByte(bytes, width * height);
        WritableRaster writableRaster = Raster.createWritableRaster(sampleModel, dataBufferByte, null);
        return new BufferedImage(colorModel, writableRaster, false, null);
    }

}

@@ -233,16 +233,49 @@
 Convert 
+byte array into 8 bit gray scale 
 image%0A  
@@ -315,16 +315,117 @@
 nverted%0A
+     * @param width width of the image in pixels%0A     * @param height height of the immage in pixels%0A
      */%0A

Update Comments