creating buffered image

M

mark jason

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
 
J

John B. Matthews

mark jason said:
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: said:
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});
}
}
 
M

mark jason

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.
 
J

John B. Matthews

mark jason said:
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>
 
D

Daniel Pitts

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top