Results 1 to 3 of 3
  1. #1
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    93
    Rep Power
    0

    Default getRGB of pixel then return as getRed,...of color.

    I am taking in an image, trying to loop through the columns and rows of the image and then during each loop, print out the color of the pixel at that column and row. For some reason I appear to be getting nothing but 0's on my println. If there is someone that could point me in the right direction of what I am doing wrong I would greatly appreciate it. My expected result was to be getting outputs of green hued colors matching my greenbg.jpg image.

    Java Code:
    package com.imageadjuster.ryan;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.util.ArrayList;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class Program extends Container{
    
    	private JPanel mainPanel;
    	private Image i;
    	private JLabel label;
    	private BufferedImage bi;
    	private Color c;
    	
    	public Program(){
    		i = new ImageIcon(getClass().getResource("/greenbg.jpg")).getImage();
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(false);
    		frame.setMinimumSize(new Dimension(i.getWidth(null), i.getHeight(null)));
    		ImageIcon image = new ImageIcon(i);
    		bi = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_RGB);
    		
    		mainPanel = new JPanel();
    		mainPanel.setLocation(0, 0);
    		mainPanel.setSize(i.getWidth(null), i.getHeight(null));
    		
    		label = new JLabel(image);
    		mainPanel.add(label);
    		add(mainPanel);
    		
    		
    		for(int rows = 0; rows < bi.getHeight(); rows++){
    			for(int columns = 0; columns < bi.getWidth(); columns++){
    				c = new Color(bi.getRGB(columns, rows));
    				int red = c.getRed();
    				int green = c.getGreen();
    				int blue = c.getBlue();
    				System.out.println("COL:" + (columns+1) + " ROW:" + (rows+1));
    				System.out.println(red + "," + green + "," + blue);
    				
    			}
    		}
    	}
    	public static void main(String[] args){
    		EventQueue.invokeLater(new Runnable(){
    			public void run(){
    				new Program();
    			}
    		});
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: getRGB of pixel then return as getRed,...of color.

    There is nothing in BufferedImage bi; you should draw your Image i in bi before you can read those pixels.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    93
    Rep Power
    0

    Default Re: getRGB of pixel then return as getRed,...of color.

    you should draw your Image i in bi
    I dont think I am quite following what you mean..

    EDIT: Finally got the lightbulb, Thank you!

    Java Code:
    package com.imageadjuster.ryan;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class Program extends Container{
    
    	private JPanel mainPanel;
    	private Image i;
    	private JLabel label;
    	private BufferedImage bi;
    	private Color c;
    	
    	public Program(){
    		i = new ImageIcon(getClass().getResource("/greenbg.jpg")).getImage();
    		
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(false);
    		frame.setMinimumSize(new Dimension(i.getWidth(null), i.getHeight(null)));
    		
    		bi = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_RGB);
    		bi.getGraphics().drawImage(i,0, 0, null);
    		ImageIcon image = new ImageIcon(bi);
    		mainPanel = new JPanel();
    		mainPanel.setLocation(0, 0);
    		mainPanel.setSize(i.getWidth(null), i.getHeight(null));
    		label = new JLabel(image);
    		mainPanel.add(label);
    		add(mainPanel);
    		frame.setVisible(true);
    		
    		
    		for(int rows = 0; rows < bi.getHeight(); rows++){
    			for(int columns = 0; columns < bi.getWidth(); columns++){
    				
    				c = new Color(bi.getRGB(columns, rows));
    				int red = c.getRed();
    				int green = c.getGreen();
    				int blue = c.getBlue();
    				System.out.println("COL:" + (columns+1) + " ROW:" + (rows+1));
    				System.out.println(red + "," + green + "," + blue);
    				
    			}
    		}
    	}
    	public static void main(String[] args){
    		EventQueue.invokeLater(new Runnable(){
    			public void run(){
    				new Program();
    			}
    		});
    	}
    }
    Last edited by rru96; 09-22-2013 at 09:11 AM.

Similar Threads

  1. knowing color of a pixel in a window
    By javaaspr in forum AWT / Swing
    Replies: 1
    Last Post: 04-02-2012, 09:06 PM
  2. scanning an image for a certain pixel color
    By lostsoul28 in forum New To Java
    Replies: 1
    Last Post: 02-01-2012, 01:50 PM
  3. Replies: 4
    Last Post: 11-08-2011, 05:04 AM
  4. Find Pixel With RGB Color?
    By TyCox94 in forum JDBC
    Replies: 8
    Last Post: 09-23-2011, 04:20 AM
  5. can you ask the color of one pixel?
    By liluma in forum New To Java
    Replies: 7
    Last Post: 09-06-2011, 07:53 PM

Tags for this Thread

Posting Permissions

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