Results 1 to 1 of 1
  1. #1
    nwboy74 is offline Member
    Join Date
    Oct 2008
    Posts
    45
    Rep Power
    0

    Default Convert a color to gray

    I was trying to predict what a pixel's color would be after drawing the image in a gray colorspace, but it's nowhere near my prediction. The formula I was taught was 0.299R + 0.587G + 0.114B. I grabbed a pixel from a BufferedImage that was obtained from a screen capture. Its RGB values were (125, 155, 227). Using the preceding formula, I anticipated that the pixel in grayspace would be 154. But when I examined the pixel after drawing to BufferedImage.TYPE_BYTE_GRAY was 204. What am I doing wrong?
    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    
    public class ScreenCapture {
      private Robot r;
      private Rectangle screenRect;
      public ScreenCapture() throws AWTException {
        this.r = new Robot();
        Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.screenRect = new Rectangle(0,0,(int)sSize.getWidth(),(int)sSize.getHeight());
      }
      public void snap(int x, int y) {
        BufferedImage bi = this.r.createScreenCapture(this.screenRect);
        BufferedImage gbi = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        Graphics2D g2d = gbi.createGraphics();
        g2d.drawImage(bi, 0, 0, bi.getWidth(), bi.getHeight(), null);
        printColor(bi.getRGB(x, y));
        printColor(gbi.getRGB(x, y));
      }
      private void printColor(int pixColor) {
        System.out.print(pixColor + ": ");
        System.out.print(((pixColor >> 16) & 0xFF) + " ");
        System.out.print(((pixColor >> 8) & 0xFF) + " ");
        System.out.println((pixColor & 0xFF) + "");
      }
      public static void main(String[] args) {
        ScreenCapture sc = null;
        if (args.length == 2) {
          try {
            sc = new ScreenCapture();
          } catch (AWTException awte) {
            System.err.println("Could not create robot");
          }
        }
        if (sc != null) {
          int x = -1;
          int y = -1;
          try {
            x = Integer.parseInt(args[0]);
            y = Integer.parseInt(args[1]);
          } catch (ParseException pe) {
            System.err.println(args[0] + " or " + args[1] + " not integers");
          }
          if (x >= 0 && y >= 0) {
            sc.snap(x, y);
          }
        }
      }
    }
    Last edited by nwboy74; 04-03-2012 at 01:32 AM.

Similar Threads

  1. Convert image to RGB based on a color ramp
    By forumuser in forum Java 2D
    Replies: 0
    Last Post: 12-14-2011, 07:57 PM
  2. Java applet on web showing gray box
    By TBS in forum Java Applets
    Replies: 0
    Last Post: 12-02-2011, 08:16 PM
  3. Convert String To Advanced Color
    By anthropamorphic in forum New To Java
    Replies: 6
    Last Post: 09-21-2011, 05:10 AM
  4. what does the gray (x) means?
    By pandaman0212 in forum Eclipse
    Replies: 3
    Last Post: 06-28-2010, 04:24 PM
  5. error reported when binarization of gray image
    By Mazharul in forum Java 2D
    Replies: 1
    Last Post: 09-18-2008, 10:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •