Getting Rid of Red Eye, Using Java

 

 

 

This picture is in the slide show on my homepage. It's also a picture of my little girl, Heidi, keeping me company while I read a book. My wife told me red eye is very common in animal pictures, and offered to photoshop the red out. I finally decided to get the red out, using Java. I mentioned in another blog (and programming) posting that an interviewer asked me about color changing in an application. I showed an easy way to do this in that blog (see Color Changing Car) but you need to write a program to make that a dynamic change. Here's the code for getting the red out. Note: this isn't the full blown program alluded to above, but it shows how you can make color changes to images in Java.

                                                                   Return To My Blog Page

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;

class ImageApp extends Component

   BufferedImage bufferedImage;

   public ImageApp(String fileName)
  {
     File file;
     try 
    {
       file = new File(fileName);
       bufferedImage = ImageIO.read(file);
    } 
    catch (Exception e) 
   {
      System.out.println("unable to load image file");
      System.exit(0);
   } 
  }

  public ImageApp(String fileName, int colorChange)
  {
     File file;
     try 
    {
       file = new File(fileName);
       bufferedImage = ImageIO.read(file);
    } 
    catch (Exception e) 
   {
      System.out.println("unable to load image file");
      System.exit(0);
    } 

   int A,R,G,B;
   for (int i = 0; i < bufferedImage.getWidth(); i++) 
  {
     for (int j = 0; j < bufferedImage.getHeight(); j++) 
    {
      A = (bufferedImage.getRGB(i,j)>>24);
      R = (bufferedImage.getRGB(i,j)&0x00FF0000)>>16;
      G = (bufferedImage.getRGB(i,j)&0x0000FF00)>>8;
      B = (bufferedImage.getRGB(i,j)&0x000000FF);
      if ( (R >= 150) && (G <= R/3) && (B <= R/3) ) 
     {
        //this is a red pixel, make it darkkhaki
        int rgb = A << 24 | 0x00bdb76b;
        bufferedImage.setRGB(i,j,rgb); 
     }
    }//end for each (j)
   }//end for each (i)
  }

   public void paint(Graphics g)
  {
     int width = 512;
     int height = width * (bufferedImage.getHeight())/(bufferedImage.getWidth());
     g.drawImage(bufferedImage, 0, 0, width, height, null);
   }

   public static void main(String s[]) 
  {
    JFrame f1 = new JFrame("Color Change");
    f1.addWindowListener(new WindowAdapter() 
              {
                  public void windowClosing(WindowEvent e) 
                                    {
                                       System.exit(0);
                                    }
              }
                                                              );
    f1.setPreferredSize(new Dimension(512, 384));
    f1.add(new ImageApp(s[0], 1));
    f1.pack();
    f1.setVisible(true);

    JFrame f = new JFrame("Straight Copy");
    f.addWindowListener(new WindowAdapter() 
      {
          public void windowClosing(WindowEvent e) 

                             {
                                System.exit(0);
                             }
      }
                                                     );
   f.setPreferredSize(new Dimension(512, 384));
   f.add(new ImageApp(s[0]));
   f.pack();
   f.setVisible(true);
  }
}

Return To My Programs