Results 1 to 2 of 2
  1. #1
    tOpach is offline Member
    Join Date
    Sep 2008
    Location
    Ankara-TURKEY
    Posts
    42
    Rep Power
    0

    Default how to get rgb value each pixel of an image file

    hi everyone,

    I am working with my grad. project. It is about data compression of an image file. I will use LWZ / HUFFMAN algorithm to do it. And first step is to get rgb value of each pixel. So, I have a sample code for this but it does not work.
    Could you please tell where the problem is

    thanks in advance,

    S.Ozan IRBIK.

    Java Code:
    import java.awt.Component;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    
    public class WalkImageTest1 extends Component {
    
      public static void main(String[] foo) {
        new WalkImageTest1();
      }
    
      public void printPixelARGB(int pixel) {
        int alpha = (pixel >> 24) & 0xff;
        int red = (pixel >> 16) & 0xff;
        int green = (pixel >> 8) & 0xff;
        int blue = (pixel) & 0xff;
        System.out.println("argb: " + alpha + ", " + red + ", " + green + ", " + blue);
      }
    
      private void marchThroughImage(BufferedImage image) {
        int w = image.getWidth();
        int h = image.getHeight();
        System.out.println("width, height: " + w + ", " + h);
    
        for (int i = 0; i < h; i++) {
          for (int j = 0; j < w; j++) {
            System.out.println("x,y: " + j + ", " + i);
            int pixel = image.getRGB(j, i);
            printPixelARGB(pixel);
            System.out.println("");
          }
        }
      }
    
      public WalkImageTest1() {
        try {
          // this is an image of a white spot on a black background.
          // with the smoothing in the image it's of course not all black
          // and white
          BufferedImage image = 
            ImageIO.read(this.getClass().getResource("C:\\1.jpeg"));
          marchThroughImage(image);
        } catch (IOException e) {
          System.err.println(e.getMessage());
        }
      }
    
    }
    I am getting these errors:

    Exception in thread "main" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)
    at WalkImageTest1.<init>(WalkImageTest1.java:41)
    at WalkImageTest1.main(WalkImageTest1.java:9)
    A stitch in time saves nine:D

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    getResource finds resources on the classpath, not on the file path.

Similar Threads

  1. Scanning Image Pixel by Pixel
    By the_transltr in forum Advanced Java
    Replies: 5
    Last Post: 08-28-2012, 04:01 PM
  2. Replies: 14
    Last Post: 04-10-2009, 08:46 AM
  3. Replies: 1
    Last Post: 08-25-2008, 08:48 PM
  4. How 2d Pixel Arrary Is Stored In .txt/.dat File
    By Mazharul in forum New To Java
    Replies: 1
    Last Post: 08-18-2008, 12:23 PM
  5. Replies: 1
    Last Post: 07-17-2007, 04:10 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
  •