You are on page 1of 4

Image to Pixels

import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import java.awt.*; import javax.swing.*; public class Pixel { public static void main(String args[]) { File fileIn = new File("images.jpeg"); BufferedImage image = ImageIO.read(fileIn); public int[] getImagePixels(BufferedImage image) { PixelGrabber grabber; int[] pixels = new int[image.getWidth() * image.getHeight()]; try { grabber = new PixelGrabber(image, 0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth()); grabber.grabPixels(0); } catch (Exception e) { e.printStackTrace(); } return pixels; } } }

public int[][] compute(File file) { try { BufferedImage img= ImageIO.read(file); Raster raster=img.getData(); int w=raster.getWidth(),h=raster.getHeight(); int pixels[][]=new int[w][h]; for (int x=0;x<w;x++) { for(int y=0;y<h;y++) { pixels[x][y]=raster.getSample(x,y,0); } } return pixels;

} catch (Exception e) { e.printStackTrace(); } return null; }

This blog will show you how to export a two dimensional array of integers to a jpg (or png) image in Java. We assume that each int represents a single pixel and its value is the colour of that pixel. The main classes we will be using are are javax.imageio.ImageIOand java.awt.image.BufferedImage. I believe ImageIO is new in Java 1.4, so this wont work for earlier versions. public class ImageExport { /** * Export an image to a JPG file * * @param fileName The filename to export to * @param image The image to write to file * @throws IOException If problems occur during writing of file */ public static void exportImageToFile(String fileName, RenderedImage image)throws IOException{ File file = new File(fileName); //to export to png, change 2 parameter to "png" ImageIO.write(image, "jpg", file); } /** * Convert a two dimensional array of ints to a BufferedImage. * Each int represents a pixel of a certain colour. * The ints are expected to be calculated using * int color = (255 << 24 ) | (red << 16 ) | (green << 8) | blue; * where red,green and blue are values in [0-255] * * @param rgbValue The two dimensional int array representing the pixels * @return A BufferedImage with all the pixels drawn */ public static BufferedImage convertRGBImage(int[][] rgbValue){ int height = rgbValue.length; int width = rgbValue[0].length;

BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //we either have to loop through all values, or convert to 1-d array for(int y=0; y< height; y++){ for(int x=0; x< width; x++){ bufferedImage.setRGB(x,y,rgbValue[y][x]); } } return bufferedImage; } /** * Convert a two dimensional array of ints to a BufferedImage. * Each int represents a pixel of a certain colour. * The ints are expected to be calculated using * int color = (255 << 24 ) | (red << 16 ) | (green << 8) | blue; * where red,green and blue are values in [0-255] * * In addition this also draws a header text in white colour on a back background. * This increases the height of the image. * * @param rgbValue The two dimensional int array representing the pixels * @param strHeader The text to draw at the top of the image * @return A BufferedImage with all the pixels drawn */ public static BufferedImage convertRGBImageWithHeader(int[][] rgbValue,String strHeader){ //We add extra pixels on top of the image for the strHeader int headerHeight=13; int height = rgbValue.length+headerHeight; int width = rgbValue[0].length; BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //we either have to loop through all values, or convert to 1-d array for(int y=headerHeight; y< height; y++){ for(int x=0; x< width; x++){ bufferedImage.setRGB(x,y,rgbValue[y-headerHeight][x]); } } //Draw the text Graphics2D g=bufferedImage.createGraphics(); g.setFont(new Font("Monospaced", Font.BOLD, 14) ); g.setColor(Color.white); g.drawString(strHeader,0,10);

return bufferedImage; } } An example of calling this code is provided below and at the very bottom of the page you can see the resulting image //setup int array int width=200; int height=100; pixel = new int [height][width]; //generate some pattern int red = 0; int green = 0; int blue = 0; for(int y=0; y< height; y++){ for(int x=0; x< width; x++){ red++; if(red>255){ red=0; } int color = (255 << 24 ) | (red << 16 ) | (green << 8) | blue; pixel[y][x]=color; } } //Create image BufferedImage image = ImageExport.convertRGBImageWithHeader(pixel,"Iteration: 1"); //Write it to file ImageExport.exportImageToFile("test2.jpg",image);