Results 1 to 5 of 5
  1. #1
    sarah49 is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default extracting color from an image in java

    i'm working on this java code to extract red,green and blue colors from an image.
    Java Code:
            	public class Mainclass
        {
    	    public static void main(String args[]) throws IOException
    	    {    
             File e = new File( "image.jpg" );
    	        BufferedImage bi= ImageIO.read(e);
    	        int w = bi.getWidth();
    	        int h = bi.getHeight();  
            for (int x=0; x <= w; x++){
    	      for (int y = 0; y <= h; y++){ 
                 int pixelCol=bi.getRGB(x,y);              
               int r= (pixelCol>>16) & 0xff;
              int b = pixelCol & 0xff;
                 int g = (pixelCol>>8) & 0xff;
          System.out.println(r,b,g);
       }}
    I dont know why i'm getting this message.
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:Coordinat e out of bounds!
    at sun.awt.image.ByteInterleavedRaster.getDataElement s<ByteInterleavedRastere.java:301>
    at java.awt.image.bufferedImage.getRGB<BufferedImage. java:870>
    at Mainclass.main<Mainclass.java:27>

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: extracting color from an image in java

    upper bounds of Java arrays are 1 minus the length since arrays go from 0 to length - 1. So when you use a for loop that goes from 0 to <= w, you'll go out of bounds. Try going from 0 to < w, and same for y.

    Also, please improve your code formatting because your code is difficult to read.

    Oh, and welcome to the java-forums!

  3. #3
    sarah49 is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: extracting color from an image in java

    yea sure thanks a lot!!!

  4. #4
    rosedolly is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default Re: extracting color from an image in java

    please anybody guide me in my final year project that is "extract text from image".

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: extracting color from an image in java

    Quote Originally Posted by rosedolly View Post
    please anybody guide me in my final year project that is "extract text from image".
    Sure. Start your own thread rather than hijack an old thread, and ask specific questions and we'll likely be more than glad to help.

    Locking this thread.

Similar Threads

  1. Replies: 4
    Last Post: 11-08-2011, 06:04 AM
  2. Problem in extracting the image in netbeans
    By Esther in forum NetBeans
    Replies: 4
    Last Post: 09-05-2011, 12:16 PM
  3. Changing Image color
    By Frecow in forum Java 2D
    Replies: 0
    Last Post: 04-04-2011, 11:16 AM
  4. Extracting Text from Image
    By kish27 in forum Advanced Java
    Replies: 3
    Last Post: 03-08-2011, 04:42 AM
  5. SWT color creating and image using.
    By caryr in forum SWT / JFace
    Replies: 2
    Last Post: 12-21-2009, 06:49 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
  •