Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > creating buffered image

Reply
Thread Tools

creating buffered image

 
 
mark jason
Guest
Posts: n/a
 
      11-12-2010
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
 
Reply With Quote
 
 
 
 
John B. Matthews
Guest
Posts: n/a
 
      11-12-2010
In article
<(E-Mail Removed)>,
mark jason <(E-Mail Removed)> 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>
 
Reply With Quote
 
 
 
 
mark jason
Guest
Posts: n/a
 
      11-12-2010
On Nov 12, 4:41*pm, "John B. Matthews" <(E-Mail Removed)> 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.
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      11-12-2010
In article
<(E-Mail Removed)>,
mark jason <(E-Mail Removed)> wrote:

> On Nov 12, 4:41*pm, "John B. Matthews" <(E-Mail Removed)> 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>
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      11-14-2010
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/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Image to Buffered Image? Ex1991 Java 0 03-25-2009 12:45 AM
whether is the standard input stream full buffered or line buffered after calling function setbuf()? kernelxu@hotmail.com C Programming 9 08-23-2005 02:24 PM
Displaying a buffered image with a predifined colormap Isaac Gerg Java 3 07-12-2004 05:38 PM
Going From Component to Buffered Image in headless environment. Sandman Java 1 07-14-2003 07:47 PM
JMF wanted to play a mpg video and capture an Image into a Buffered Image tochaotic4u Java 2 07-03-2003 09:50 AM



Advertisments