4

I need to take an int array and turn it into BufferImage. I really don't have any background on this subject and I learn it all from the internet so here's what I'm trying to do: Create an array from BufferedImage(done), turn this array into IntBuffer(done) - (Later i'll need to do some opertions on the image through the IntBuffer), put the changed values from the IntBuffer in new array(done), and turn this array into WritableRaster. (If something isn't right in my understading of the process please tell me)

Here's the line where I deal with the WritableRaster:

WritableRaster newRaster= newRaster.setPixels(0, 0, width, height, matrix);

Eclipse marks this as a mistake and says ''Type mismatch:Cannot convert from void to WritableRaster"

Please help! I'm a bit lost.

Also sorry for bad english.

EDIT: The matrix:

         int height=img.getHeight();
         int width=img.getWidth();
         int[]matrix=new int[width*height];

The part of the code where I try to insert values to the Raster:

    BufferedImage finalImg = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
    WritableRaster newRaster= (WritableRaster)finalImg.getData();
    newRaster.setPixels(0, 0, width, height, matrix);

The error message:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10769
at java.awt.image.SinglePixelPackedSampleModel.setPixels(Unknown Source)
at java.awt.image.WritableRaster.setPixels(Unknown Source)
user3917631
  • 119
  • 1
  • 9

3 Answers3

7

You can create a WritableRaster and/or BufferedImage from an int array like this:

int w = 300;
int h = 200;
int[] matrix = new int[w * h];

// ...manipulate the matrix...

DataBufferInt buffer = new DataBufferInt(matrix, matrix.length);

int[] bandMasks = {0xFF0000, 0xFF00, 0xFF, 0xFF000000}; // ARGB (yes, ARGB, as the masks are R, G, B, A always) order
WritableRaster raster = Raster.createPackedRaster(buffer, w, h, w, bandMasks, null);

System.out.println("raster: " + raster);

ColorModel cm = ColorModel.getRGBdefault();
BufferedImage image = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);

System.err.println("image: " + image);
Harald K
  • 26,314
  • 7
  • 65
  • 111
1
    ColorModel cm = ColorModel.getRGBdefault();
    int w = 300;
    int h = 200;
    WritableRaster raster = cm.createCompatibleWritableRaster(w, h);
    DataBufferInt buffer = (DataBufferInt) raster.getDataBuffer();
    int[] bufferData = buffer.getData();
    int[] array = new int[2400];
    Random random = new Random();
    for (int i = 0; i < 2400; i++) {
        array[i] = random.nextInt(2);
    }
    System.arraycopy(array, 0, bufferData, 0, (array.length < bufferData.length ? array.length : bufferData.length));
    BufferedImage image = new BufferedImage(cm, raster, false, null);
    FileOutputStream fos = new FileOutputStream("D:\\abc\\OCR\\" + "LearningRaster" + ".png");
    ImageIO.write(image, "PNG", fos);
    fos.close();
aavos
  • 137
  • 2
  • 8
0

setPixels returns void:

public static void setPixels(BufferedImage img,
                         int x, int y, int w, int h, int[] pixels)

so you need to create Raster and than set pixels to it:

WritableRaster newRaster= WritableRaster.createWritableRaster(…);
newRaster.setPixels(0, 0, width, height, matrix);

You need to put 4 int per pixel (it depends on color model, 4 for ARGB). So, matrix size must be

int[] matrix = new int[width * height * 4]

See more about WritableRaster here —

Oracle: WritableRaster

Code examples

constpetrov
  • 126
  • 5
  • First thank you for you help! Here's the lines where I try to do what you said, can you please tell me what is wrong here? IntBuffer data= IntBuffer.wrap(matrix); data.put(0,69); data.rewind(); Point coordinate=new Point(0,0); SampleModel sample=img.getSampleModel(); WritableRaster newRaster= WritableRaster.createWritableRaster(sample, coordinate); newRaster.setPixels(0, 0, width, height, matrix); It says the array is out of bounds,I think its something to do with the SampleModel but I don't know how to create it withput putting any values. Thanks you – user3917631 Aug 07 '14 at 10:31
  • Which type the img has? Where you got width and height? – constpetrov Aug 07 '14 at 10:54
  • Also, see the first answer here: http://stackoverflow.com/questions/9094620/how-to-create-image-from-array-of-pixel-values-and-known-width-and-height?rq=1 – constpetrov Aug 07 '14 at 11:03
  • I will look at the link... for you question - img is a BufferedImage, the width and height I got with the methods img.getHeight/width(); The matrix size is height*width – user3917631 Aug 07 '14 at 11:15
  • I did as they adviced and its having the same problem with this line: newRaster.setPixels(0, 0, width, height, matrix); ''Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException'' I just don't understand where is the problem – user3917631 Aug 07 '14 at 11:32
  • Any idea of where the problem might be? – user3917631 Aug 07 '14 at 12:06
  • And what type fits a binary image? – user3917631 Aug 07 '14 at 12:23
  • BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY) — one int per pixel – constpetrov Aug 07 '14 at 12:27
  • I need more ''reputation'' to do this, i've just signed up – user3917631 Aug 07 '14 at 12:47
  • http://meta.stackoverflow.com/questions/251078/how-to-update-and-accept-answers accept, not upvote – constpetrov Aug 07 '14 at 13:11