BufferedImage setRGB: Image gets squished down to quarter size

时间:2016-07-11 20:46:51

标签: java bufferedimage javax.imageio

I have a one dimensional integer array of rgb values that I need to feed BufferedImage and then use that to generate a jpeg. I have 128*128 pixels, which I'm stepping over using setrgb (I tried passing buffered image the array in one go, ran into trouble and well, that's a separate question)

       int numRows = 128;
       int numCols = 128;
       int inputIndex = 0;
       BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
       for(int row = 0; row < numRows; row++){
           for(int col = 0; col < numCols; col ++){
               int rgb = rgbvals[inputIndex++];
               rgb = (rgb << 8) + rgbvals[inputIndex++]; 
               rgb = (rgb << 8) + rgbvals[inputIndex++];
               image.setRGB(col,row, rgb);
           }             
       }
       File outputFile = new File("output.jpg");
       ImageIO.write(image, "JPEG", outputFile);

This looks rather straightforward, but my image looks like this: 128*128 picture of an eye, except it takes up 1/4 the space it needs and the rest of the image is black

I feel like I might have overlooked a small detail. I'd appreciate any insight, thanks!

1 个答案:

答案 0 :(得分:0)

This was actually a silly mistake, I only needed to call the BufferedImage constructor with numRows and numCols correctly, instead of the other variables I used(width, height referred to something else).

This fixed it.