Right now I have an image and I want to convert it into a BufferedImage...
This is my code -
private BufferedImage toBufferedImage(Image img, int width, int height){
// Create a buffered image with transparency
BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics bGr = bimage.getGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
// Return the buffered image
return bimage;
}
But it doesn't work. Can someone explain what's wrong?
Try the new JRE that includes JavaFX.
You could use SwingFXUtils. There is a method which does what you ask for:
BufferedImage image = SwingFXUtils.fromFXImage(fxImage, null);
or
BufferedImage image = ImageIO.read(new File(filename));
BufferedImage extends Image, so you can cast it:
BufferedImage buffered = (BufferedImage) image;
But you must check the type of your image object in runtime, because it can contain other kind of Image.
UPDATE
To convert an Image to a BufferedImage you should check this class and try it:
http://www.dzone.com/snippets/converting-images
Related
I have made a project like Paint from windows and now I want to make the save/open buttons. I have found out how to save the bufferedImage, but the problem is how do I open it back in the correct location and be able to draw on it again?
To read in an image, use ImageIO.
File myPath = new FIle("path to image");
BUfferedImage img = ImageIO.read(myPath);
Also what you could (should) do is load the image into your user space so that you don't edit the original image:
public static BufferedImage userSpace(BufferedImage image)
{
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graphics = newImage.createGraphics();
graphics.drawRenderedImage(image, null);
graphics.dispose();
return newImage;
}
I would like scale image. I am trying use getScaledInstance() method by doesn't work for me. Image which I write is the same size as the funniest...
It is my code:
Image image = ImageIO.read(file.getInputStream());
image.getScaledInstance(100, -1, Image.SCALE_SMOOTH);
BufferedImage bImage = (BufferedImage) image;
ImageIO.write(bImage, "jpg", new File(urlImage));
Image.getScaledInstance() returns an instance of the same image, scaled to the desired size without modifying your existing image. Assuming you are trying to save the scaled instance to file, you merely need to move the call to the line below.
E.g:
Image image = ImageIO.read(file.getInputStream());
BufferedImage bImage = (BufferedImage) image.getScaledInstance(100, -1, Image.SCALE_SMOOTH);
ImageIO.write(bImage, "jpg", new File(urlImage));
I need to convert an ImageIcon into an Image for a program how should I go about doing it? I there a method of casting I can use or a built in method to do it?
Building on the answer from the question I linked. I'm not sure which Image you mean, save it as a file or get a java Image instance, but you can use just the second method if you mean the latter, or use the former (which depends on the latter) if you need that.
// format should be "jpg", "gif", etc.
public void saveIconToFile(ImageIcon icon, String filename, String format) throws IOException {
BufferedImage image = iconToImage(icon);
File output = new File(filename);
ImageIO.write(bufferedImage, format, outputfile);
}
public BufferedImage iconToImage(ImageIcon icon) {
BufferedImage bi = new BufferedImage(
icon.getIconWidth(),
icon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
// paint the Icon to the BufferedImage.
icon.paintIcon(null, g, 0,0);
g.dispose();
return bi;
}
I have a method which purpose is to receive an image and return it scaled down.
The reason I'm using canvas is that I believe that it will scale the image automatically for me.
After the conversion the outputimage is completely black. Anyone have any clue on how to fix this?
try {
InputStream in = new ByteArrayInputStream(f.getBytes());
BufferedImage image = ImageIO.read(in);
File beforescale = new File("beforescale.jpg");
ImageIO.write(image, "jpg", beforescale); //works
Canvas canvas = new Canvas();
canvas.setSize(100, 100);
canvas.paint(image.getGraphics());
image = canvasToImage(canvas);
File outputfile = new File("testing.jpg");
ImageIO.write(image, "jpg", outputfile); //all black
response.getWriter().print(canvas);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private BufferedImage canvasToImage(Canvas cnvs) {
int w = cnvs.getWidth();
int h = cnvs.getHeight();
int type = BufferedImage.TYPE_INT_RGB;
BufferedImage image = new BufferedImage(w,h,type);
Graphics2D g2 = image.createGraphics();
cnvs.paint(g2);
g2.dispose();
return image;
}
The problem is, here you use canvas#paint(Graphics) to paint the image on the canvas:
canvas.paint(image.getGraphics());
And here you canvas#paint(Graphics) again to paint the canvas on the image:
cnvs.paint(g2);
Obviously one of these two fails. You can only use this method to paint the canvas on the image.
The solution is to use getScaledInstance() on image.
BufferedImage image = ImageIO.read(in);
Image smallerImg = image.getScaledInstance(100,100,Image.SCALE_SMOOTH);
ImageIO.write(smallerImg, "jpg", outputfile);
I would like to create a gif image of a filled red circle on a green background. What is the easiest way to do it in Java?
If you already have an image file or image URL, then you can use Toolkit to get an Image:
Image img = Toolkit.getDefaultToolkit().createImage(filename);
If you need to construct a new image raster and paint into the image, then you can use BufferedImage. You can paint onto a BufferedImage, by calling its createGraphics function and painting on the graphics object. To save the BufferedImage into a GIF, you can use the ImageIO class to write out the image.
The best way is to generate a BufferedImage:
BufferedImage img = new BufferedImage(int width, int height, int imageType)
// you can find the Types variables in the api
Then, generated the Graphics2D of this image, this object allows you to set a background and to draw shapes:
Graphics2D g = img.createGraphics();
g.setBackground(Color color) ; //Find how to built this object look at the java api
g.draw(Shape s);
g.dispose(); //don't forget it!!!
To built the image:
File file = new File(dir, name);
try{
ImageIO.write(img, "gif", file);
}catch(IOException e){
e.printStackTrace();
}
Create a BufferedImage and then write it to a file with ImageIO.write(image, "gif", fileName).