BufferedImage has a constructor where you can specify a WriteableRaster.
When choosing a default buffered image, saving each pixel in an int, it uses IntegerInterleavedRaster.
In ColorModel, you can use ColorModel.getRGBDefault ().
int imageWidth = 638, imageHeight = 480;
int dataImageWidth = 640;
SampleModel sm = new SinglePixelPackedSampleModel(TYPE_INT, imageWidth, imageHeight, dataImageWidth, new int[] { 0xff0000, 0xff00, 0xff });
DataBuffer db = new DataBufferInt(dataImageWidth * imageHeight);
WritableRaster r = Raster.createWritableRaster(sm, db, new Point());
BufferedImage image = new BufferedImage(ColorModel.getRGBDefault(), r, false, null);
Pay attention to scanlineStride in SinglePixelPackedSampleModel (second last parameter).
Another simpler approach is to use the BufferedImage getSubimage method.
BufferedImage fullImage = new BufferedImage(dataImageWidth, imageHeight);
BufferedImage subImage = fullImage.getSubimage(0, 0, imageWidth, imageHeight);
source
share