Results 1 to 8 of 8
  1. #1
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default how to convert an image in to pixels

    want to convert an image in to pixels .. using pixelgrabber .. not working .. need a small code on this ....

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Make a class, Pixel, holding the argb value, and position, and make a 2d array. Fill it by looping through every pixel in the image. Not gonna write the code for you. Show us what you've got first.
    I die a little on the inside...
    Every time I get shot.

  3. #3
    jason wang is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    I remember that there is a Graphic class in java which you can use to finish this task.

  4. #4
    mystified is offline Member
    Join Date
    Sep 2008
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by Shyam Singh View Post
    want to convert an image in to pixels .. using pixelgrabber .. not working .. need a small code on this ....
    There is an example code in Java2: The complete reference fifth edition chapter 23.. works fine.. :)

  5. #5
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default Here wat i tried -- >

    class 1 -->



    import java.awt.MediaTracker;

    public class one extends Toolkit {

    Toolkit tk=Toolkit.getDefaultToolkit();

    imgage img;

    img=th.createimage("c:/ss.jpg");

    MediaTracker t= new MediaTracker(this);

    t.addImage(img,2);
    t.waitForID(2);

    int w= img.getWidth (null);
    int h= img.getHeight (null) ;

    int []pixels = new int[w * h] ;

    }


    class 2-->


    import java.awt.MediaTracker;
    import java.awt.image.PixelGrabber;

    public class imagetest2 extends PixelGrbber {

    public static void main(String [] a) {

    one n=new one();

    PixelGrabber pg = new PixelGrabber (n.img ,0 ,0 , n.w , n.h , n.pixels , 0 ,n.w);

    pg.grabPixels();

    }

    }

  6. #6
    oliemansm is offline Member
    Join Date
    Sep 2008
    Location
    Amsterdam
    Posts
    4
    Rep Power
    0

    Default

    Class one is not a valid class. It's missing a constructor and methods. Furthermore, I wouldn't create a separate class for this functionality.

    Take a look at API spec for PixelGrabber, it should help you in the right direction. While you're there, check class ImageIO for a more up to date way to load image files.

    Good luck,
    Michiel

  7. #7
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Default I tried some But unable to get back the Image --> Applet not Initialized

    /* <applet code=HistoGrab.class width=600 height=600> </applet> */


    import java.applet.*;
    import java.applet.Applet;
    import java.awt.* ;
    import java.awt.Toolkit;
    import java.awt.image.* ;


    public class HistoGrab extends Applet {

    Dimension d;
    Image img;
    int iw, ih;
    int pixels[];

    int w, h;
    int hist[] = new int[256];
    int max_hist = 0;

    public void init() {
    d = getSize();
    w = d.width;
    h = d.height;

    try {

    Toolkit tk = Toolkit.getDefaultToolkit(); // Binds the Compontes of dyanmic Changes
    img = getImage(getDocumentBase(), getParameter("c:/ss.Bitmap Image"));

    // img = getImage ("c:/ss.JPEG");

    MediaTracker t = new MediaTracker(this); // Keeps the Status of components


    t.addImage(img, 0); // Passing image obj. & index

    t.waitForID(0); // Method wait for all the images to be loaded

    t.isErrorID(0); // Returns error if any in loading ID 0

    iw = img.getWidth(null); // Media observer waiting for img to be Loaded & image width or -1 if not .
    ih = img.getHeight(null);
    pixels = new int[iw * ih];

    PixelGrabber pg = new PixelGrabber(img, 0, 0, iw, ih,pixels, 0, iw);
    pg.grabPixels();

    for(int i:pixels)
    System.out.println( pixels[i] );

    } catch (InterruptedException e) { };


    for (int i=0;i<iw*ih;i++ ) {

    int p = pixels[i];
    int r = 0xff & (p >> 16);
    int g = 0xff & (p >> 8);
    int b = 0xff & (p);
    int y = (int) (.33 * r + .56 * g + .11 * b); // Gray Scale conversition
    hist[y]++;
    }

    for (int i=0; i<256; i++) { // Binarization
    if (hist[i] > max_hist)
    max_hist = hist[i];

    }
    }

    // public void update() {}

    public void paint(Graphics g) {
    g.drawImage(img, 0, 0, null);
    int x = (w - 256) / 2;
    int lasty = h - h * hist[0] / max_hist;
    for (int i=0; i<256; i++, x++) {
    int y = h - h * hist[i] / max_hist;

    g.setColor(new Color(i, i, i));
    g.fillRect(x, y, 1, h);
    g.setColor(Color.red);
    g.drawLine(x-1,lasty,x,y);
    lasty = y;
    }
    }
    }

    /* public static void main(String []a) {

    HistoGrab g= new HistoGrab();
    }
    */


    pplet not Initialized ..

  8. #8
    oliemansm is offline Member
    Join Date
    Sep 2008
    Location
    Amsterdam
    Posts
    4
    Rep Power
    0

    Default

    1. Please put your code inside code blocks when you post; it makes it easier for us to read it.
    2. You have a call to method getImage(..) but where is that method defined?
    3. Never use an empty catch block, because now you don't know what's going wrong! Use ex.printStackTrace() inside your catch block, you'll get a hint where to look next...

Similar Threads

  1. Modified Pixels
    By monkey04 in forum Java 2D
    Replies: 1
    Last Post: 03-12-2009, 09:15 AM
  2. Replies: 0
    Last Post: 07-02-2008, 09:06 PM
  3. Blurring pixels
    By tim in forum New To Java
    Replies: 0
    Last Post: 01-01-2008, 03:06 PM
  4. Counting Pixels
    By shaungoater in forum Java 2D
    Replies: 5
    Last Post: 11-29-2007, 06:51 PM
  5. Deleting certain image pixels..
    By Brightside in forum New To Java
    Replies: 1
    Last Post: 05-22-2007, 10:21 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
  •