// An example of image reading/writing in Java // // Digitally remove Java mascot Duke from the cake // // REF: http://java.sun.com/developer/JDCTechTips/2004/tt0217.html#1 // // M. Branicky, 10/09/06, modified 10/11/06, more comments added 10/14/06 import java.awt.image.*; import javax.imageio.*; import java.io.*; public class ImageFun { // program constants for particular colors // // 0xNUM is how to input HEXadecimal number NUM (base 16: uses digits 0-F) in Java // See http://cloford.com/resources/colours/500col.htm for more HEX color codes // public static final int BLACK = 0, WHITE = 0xFFFFFF; // This is DIFFERENT THAN OTHER PROGRAMS BUT it is // NEEDED b/c I didn't use try-catch; see D&D, p. 647 // vvvvvvvvvvvvvvvvvv public static void main (String args[]) throws IOException { // open and read the input image, a .jpg file, available at // http://dora.case.edu/msb/131/code/bakeonceeatanywhere.jpg File inputFile = new File("bakeonceeatanywhere.jpg"); BufferedImage input = ImageIO.read(inputFile); // Grab the starting (upper-left) indices and size of the picture System.out.println("MinX: "+input.getMinX()); System.out.println("MinY: "+input.getMinY()); System.out.println("Width: "+input.getWidth()); System.out.println("Height: "+input.getHeight()); // An early attempt at modifying the image, now commented out // result at http://dora.case.edu/msb/131/code/imagemod3.jpg /* // Replace a rectangle of pixels with the color BLACK // i is measured along the image's width, j along its height // Both are indexed starting at the image's upper-left corner for (int i=150; i<250; i++) { for (int j=100; j<200; j++) { input.setRGB(i,j,BLACK); } } */ // More sophisticated processing: // i-j loops were switched b/c width is a (linear) function of height for (int j=95; j<208; j++) { for (int i=165-(j-95)/3; i<255; i++) { // grab a pixel at the same height in the orange rectangle, // but to the right of Duke, for the purpose of blending int cakecolor = input.getRGB(256,j); input.setRGB(i,j,cakecolor); } } // Write the output into a .jpg file // image available at http://dora.case.edu/msb/131/code/imagemod.jpg File outImage = new File("imagemod.jpg"); ImageIO.write(input, "jpg", outImage); } }