creating buffered image

Discussion in 'Java' started by mark jason, Nov 12, 2010.

  1. mark jason

    mark jason Guest

    hi,
    I am working on an image processing application and thought of
    creating a class called FaceImage to represent a n image of a human
    face.I created the class like this

    public class FaceImage {
    private String fileName;
    private int width;
    private int height;
    double[] data;
    public FaceImage(File imageFile) throws MyException{
    if (isAnImageFile(imageFile.getPath())){
    try{
    BufferedImage b=ImageIO.read(imageFile);
    b=convertToGray(b);
    int w=b.getWidth();
    int h=b.getHeight();
    this.height=h;
    this.width=w;
    this.data=new double[h*w];
    b.getData().getPixels(0,0, w,h,data);
    }catch(IOException e){
    e.printStackTrace();
    }
    }else{
    throw new MyException("file"+imageFile.getPath()+" is not an image
    file");
    }
    }

    public FaceImage(String imageName,int width,int height,double[]
    data) throws MyException{
    if (data.length !=width*height){
    throw new MyException("data size must be equal to "+width*height);
    }
    this.fileName=imageName;
    this.width=width;
    this.height=height;
    this.data=data;
    }
    ...

    public boolean isAnImageFile(String fileName){
    //check if an image and return true or false
    .....
    }
    }

    I also wanted to implement a saveImage() method so that I can save the
    FaceImage object as an image file of given format.However when I tried
    MemoryImageSource constructor I found that it needs an int[] not
    double[].

    Is there some way I can use the double[] containing pixel to save a
    new image?do I need to convert the double[] to int[] as needed by
    MemoryImageSource ?I guess a for loop would be very inefficient.
    Any help or advice would be nice.
    thanks
     
    mark jason, Nov 12, 2010
    #1
    1. Advertising

  2. In article
    <>,
    mark jason <> wrote:

    > I am working on an image processing application and thought of
    > creating a class called FaceImage to represent a n image of a human
    > face. I created the class like this:


    Please format code so that it remains readable, as suggested in the
    article here: <http://sscce.org/>

    > I also wanted to implement a saveImage() method so that I can save
    > the FaceImage object as an image file of given format. However when I
    > tried MemoryImageSource constructor I found that it needs an int[]
    > not double[].


    WritableRaster has accessors for int, float and double.

    > Is there some way I can use the double[] containing pixel to save a
    > new image? Do I need to convert the double[] to int[] as needed by
    > MemoryImageSource? I guess a for loop would be very inefficient.


    I'd probably just use a BufferedImage; that way I can use ImageIO, as
    suggested in the tutorial:

    <http://download.oracle.com/javase/tutorial/2d/images/index.html>

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;

    public class FaceImage {

    private String fileName;
    private BufferedImage bi;
    private int w;
    private int h;
    double[] dData;
    int[] iData;

    public FaceImage(File imageFile) throws IOException {
    bi = ImageIO.read(imageFile);
    this.fileName = imageFile.getName();
    this.w = bi.getWidth();
    this.h = bi.getHeight();
    this.dData = bi.getData().getPixels(0, 0, w, h, dData);
    this.iData = bi.getData().getPixels(0, 0, w, h, iData);
    }

    public FaceImage(String fileName, int w, int h, double[] data) {
    if (data.length != 3 * w * h) {
    throw new IllegalArgumentException(
    "data size must be equal to " + w * h);
    }
    this.fileName = fileName;
    this.w = w;
    this.h = h;
    this.dData = data;
    this.bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    bi.getRaster().setPixels(0, 0, w, h, data);
    this.iData = bi.getData().getPixels(0, 0, w, h, iData);
    }

    public void save() throws IOException {
    ImageIO.write(bi, "JPEG", new File(fileName));
    }

    public static void main(String[] args) throws IOException {
    new FaceImage(new File("image.jpg"));
    new FaceImage("one.jpg", 1, 1, new double[]{0, 0, 0});
    }
    }

    > Any help or advice would be nice.


    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Nov 12, 2010
    #2
    1. Advertising

  3. mark jason

    mark jason Guest

    On Nov 12, 4:41 pm, "John B. Matthews" <> wrote:

    > Please format code so that it remains readable, as suggested in the

    sorry..will take care

    > I'd probably just use a BufferedImage; that way I can use ImageIO, as
    > suggested in the tutorial:
    >

    thanks for the help.That makes things a lot easier.
    regards,
    mark.
     
    mark jason, Nov 12, 2010
    #3
  4. In article
    <>,
    mark jason <> wrote:

    > On Nov 12, 4:41 pm, "John B. Matthews" <> wrote:

    [...]
    > > I'd probably just use a BufferedImage; that way I can use ImageIO,
    > > as suggested in the tutorial:
    > >

    > Thanks for the help. That makes things a lot easier.


    That's been my experience, too; but, should the need arise, you can
    also use a BufferedImage to render and save an Image from another
    source, such as the gradient seen in the MemoryImageSource API:

    <http://download.oracle.com/javase/6/docs/api/java/awt/image/MemoryImageSource.html>

    private void save(Image image) {
    int w = image.getWidth(null);
    int h = image.getHeight(null);
    BufferedImage bi = new BufferedImage(
    w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(image, 0, 0, null);
    try {
    ImageIO.write(bi, "JPEG", new File("new.jpg"));
    } catch (IOException ex) {
    ex.printStackTrace(System.err);
    }
    g2d.dispose();
    }

    I was curious to see what it looked like:

    <http://tinypic.com/r/2qk03zq/7>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Nov 12, 2010
    #4
  5. mark jason

    Daniel Pitts Guest

    On 11/11/2010 10:52 PM, mark jason wrote:
    > hi,
    > I am working on an image processing application and thought of
    > creating a class called FaceImage to represent a n image of a human
    > face.I created the class like this

    It seems to me that "Face" needn't be part of your type system.

    Perhaps what you want is:
    import java.awt.Image;
    public class Human {
    private Image face;
    //... Rest of class.
    }

    The "Face Image" concept is too specific for class, coupling the concept
    of "part of body" with the concept of "image". They are very distinct
    concepts.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Nov 14, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tochaotic4u
    Replies:
    2
    Views:
    4,056
    tochaotic4u
    Jul 3, 2003
  2. Sandman
    Replies:
    1
    Views:
    889
    Lynn Robinson
    Jul 14, 2003
  3. Isaac Gerg
    Replies:
    3
    Views:
    431
  4. Replies:
    9
    Views:
    686
    Michael Wojcik
    Aug 23, 2005
  5. Ex1991

    Image to Buffered Image?

    Ex1991, Mar 25, 2009, in forum: Java
    Replies:
    0
    Views:
    371
    Ex1991
    Mar 25, 2009
Loading...

Share This Page