loading images into memory and converting into array

Step 1: Create class Run:

import java.awt.*;
import java.awt.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
 
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
 
public class Run {
 
	public static void main(String[] a) throws Throwable {
 
		String path = "1.jpg";//our file to process
		Image I = Toolkit.getDefaultToolkit().createImage(path); //pointer to the image
		BufferedImage J = toBufferedImage(I);//upload image into memory
 
 
		ImageColoredConcept color = new ImageColoredConcept();
		color.GrubImage(J);//grub
 
		ImageBlackWhiteConcept bw = new ImageBlackWhiteConcept();
		bw.GrubImage(J);//grub
 
		ImageIO.write(J, "jpg", new File("nochange.jpg"));//write back
		ImageIO.write(color.GetBufferedImage(), "jpg", new File("color.jpg"));//write back the colored concept
		ImageIO.write(bw.GetBufferedImage(), "jpg", new File("bw.jpg"));//write back the black-white concept
 
	}
 
	public static BufferedImage toBufferedImage(Image image) {
		if (image instanceof BufferedImage) {
			return (BufferedImage) image;
		}
 
		image = new ImageIcon(image).getImage();
 
		BufferedImage bimage = null;
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		try {
 
			int transparency = Transparency.OPAQUE;
 
			GraphicsDevice gs = ge.getDefaultScreenDevice();
			GraphicsConfiguration gc = gs.getDefaultConfiguration();
			bimage = gc.createCompatibleImage(image.getWidth(null), image
					.getHeight(null), transparency);
		} catch (HeadlessException e) {
			e.printStackTrace();
			System.exit(-1);
		}
		if (bimage == null) {
 
 
				int type = BufferedImage.TYPE_INT_ARGB;
 
			bimage = new BufferedImage(image.getWidth(null), image
					.getHeight(null), type);
		}
 
		Graphics g = bimage.createGraphics();
 
		g.drawImage(image, 0, 0, null);
		g.dispose();
 
		return bimage;
	}
 
}

Step 2: Create classes:

import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
 
public class ImageBlackWhiteConcept {
 
	public int[][] F = null;
	int H = 0;
	int W = 0;
 
	public BufferedImage GetBufferedImage() {
 
		int[] M = new int[this.H * this.W];
		int t = 0;
 
		for (int i = 0; i < this.H; i++) {
			for (int j = 0; j < this.W; j++) {
				M[i * this.W + j] = ((0xff) << 24) + ((F[i][j] & 0xff) << 16)
						+ ((F[i][j] & 0xff) << 8) + ((F[i][j] & 0xff));
			}
		}
		return Run.toBufferedImage(Toolkit.getDefaultToolkit()
				.createImage(
						new MemoryImageSource(this.W, this.H, M, 0, this.W)));
 
	}
 
	public void GrubImage(BufferedImage I) {
		this.H = I.getHeight();
		this.W = I.getWidth();
 
		this.F = new int[H][W];
 
		int[] m = new int[H * W];
		PixelGrabber pg = new PixelGrabber(I, 0, 0, W, H, m, 0, W);
		try {
			pg.grabPixels();
		} catch (InterruptedException e) {
			System.err.println("interrupted waiting for pixels!");
 
		}
		int t = 0;
		int r = 0;
		int g = 0;
		int b = 0;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				t = m[i * W + j];
				r = (int) ((t & 0x00ff0000) >> 16);
				g = (int) ((t & 0x0000ff00) >> 8);
				b = (int) ((t & 0x000000ff));
				F[i][j] = (int) ((r + g + b) / 3.0);
 
			}
		}
 
	}
}
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
 
public class ImageColoredConcept {
 
	public int[][] R = null;
	public int[][] G = null;
	public int[][] B = null;
	int H = 0;
	int W = 0;
 
	public BufferedImage GetBufferedImage() {
 
		int[] M = new int[this.H * this.W];
		int t = 0;
 
		for (int i = 0; i < this.H; i++) {
			for (int j = 0; j < this.W; j++) {
				M[i * this.W + j] = ((0xff) << 24) + ((R[i][j] & 0xff) << 16)
						+ ((G[i][j] & 0xff) << 8) + ((B[i][j] & 0xff));
			}
		}
		return Run.toBufferedImage(Toolkit.getDefaultToolkit()
				.createImage(
						new MemoryImageSource(this.W, this.H, M, 0, this.W)));
 
	}
 
	public void GrubImage(BufferedImage I) {
		this.H = I.getHeight();
		this.W = I.getWidth();
 
		this.R = new int[H][W];
		this.G = new int[H][W];
		this.B = new int[H][W];
 
		int[] m = new int[H * W];
		PixelGrabber pg = new PixelGrabber(I, 0, 0, W, H, m, 0, W);
		try {
			pg.grabPixels();
		} catch (InterruptedException e) {
			System.err.println("interrupted waiting for pixels!");
 
		}
		int t = 0;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				/*
				 * r = (char)((pixels[y*w+x] >> 16) & 0xff); g =
				 * (char)((pixels[y*w+x] >> 8) & 0xff); b = (char)(pixels[y*w+x]
				 * & 0xff); gray = (char)(0.30f*r + 0.59f*g + 0.11f*b);
				 * pixels[y*w+x] = …;
				 */
				t = m[i * W + j];
				R[i][j] =  ((t & 0x00ff0000) >> 16);
				G[i][j] =  ((t & 0x0000ff00) >> 8);
				B[i][j] =  ((t & 0x000000ff));
 
			}
		}
 
 
	}
 
}

Step 4: Run the Run.java file;
See files color.jpg, nochange.jpg, bw.jpg for the result;

Comments (1)

ImageProcessing « FieralSeptember 8th, 2010 at 5:10 pm

[...] Теперь напишем нехитрый java-код который загрузит файл 1.jpg и превратит его в массив int[H][W] или в тройку массивов такого типа – для работы с цветом изображения. http://robotics.icstweb.org/java-do-diez/2008/10/11/loading-images-into-memory-and-converting-into-a... [...]

Leave a comment

Your comment