// SelectColor.java -- prints out first 10 x 10 blue pixel values to standard output. // Erica Asai // Time-stamp: <2004-01-23 12:46:18 @easai> // Comments: // Specify an image file name as command argument. The first 10 x 10 // blue pixel values will be sent to standard output. import java.awt.*; import java.awt.image.*; public class SelectColor extends Frame { Image image; int memory[]; String fileName="defaultImage.jpg"; int imageWidth=0; int imageHeight=0; public void readImageFile(String fileName) { try { image= getToolkit().getImage(fileName); MediaTracker tracker=new MediaTracker(this); tracker.addImage(image,0); tracker.waitForID(0); if(tracker.isErrorAny()) { throw new Exception(); } } catch(Exception e){e.printStackTrace();} } void captureImage() { imageWidth=image.getWidth(this); imageHeight=image.getHeight(this); memory=new int[imageWidth*imageHeight]; PixelGrabber pg= new PixelGrabber(image, 0, 0, imageWidth, imageHeight, memory, 0, imageWidth); try { pg.grabPixels(); } catch(Exception e){e.printStackTrace();} if(((pg.getStatus())&ABORT)!=0) { System.err.println("Can not capture the image"); return; } } void init() { readImageFile(fileName); captureImage(); for(int i=0;i<10;i++) { for(int j=0;j<10;j++) System.out.print((memory[i*imageWidth+j]&0xff)+" "); System.out.println(); } } public static void main(String args[]) { SelectColor selectColor=new SelectColor(); if(args.length>0) selectColor.fileName=args[0]; selectColor.init(); } }