import java.io.*; import java.util.*; import java.awt.*; import java.awt.image.*; import javax.imageio.*; public class IO { public double[][] readImage(File f) { Panel panel = new Panel(); Image image = Toolkit.getDefaultToolkit().getImage(f.getPath()); MediaTracker mTracker = new MediaTracker(panel); mTracker.addImage(image,1); try { mTracker.waitForID(1); } catch (InterruptedException ie) { ie.printStackTrace(); } int width = image.getWidth(null); int height = image.getHeight(null); double[][] tempArray = new double[height][width]; int pixels[] = new int [width * height]; PixelGrabber pg = new PixelGrabber(image,0,0,width,height,pixels,0,width); try{ pg.grabPixels(); } catch (InterruptedException ie) { ie.printStackTrace(); } for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { Color tempColor = new Color(pixels[j + (i*width)]); tempArray[i][j] = (tempColor.getRed() + tempColor.getGreen() + tempColor.getBlue())/ 3; } } return tempArray; } public void writeJPEG(Image image, File f) { Panel panel = new Panel(); // Make a Buffered image in memory. BufferedImage bi = new BufferedImage(image.getWidth(panel), image.getHeight(panel), BufferedImage.TYPE_INT_RGB); // Get the BufferedImage's 2D Graphics Context. Graphics2D g2d = bi.createGraphics(); // Draw the surface image to the buffered image. g2d.drawImage(image,0,0,panel); // Get a rendered image. RenderedImage ri = (RenderedImage) bi; // Kill off the unneeded graphics context. g2d.dispose(); // Now write the Jpeg. String format = f.getPath(); format = format.substring(format.lastIndexOf(".") + 1); // File format set depending on how the user has named the file to be // saved. try { ImageIO.write(ri, format, f); } catch (Exception e) { System.out.println("Format not recognised: " + format); } } }