I'm having an image on disk. I want to convert it to a BufferedImage so that i can apply filters on it. Is there any way to do this?
Asked
Active
Viewed 1.5k times
4 Answers
5
use ImageIO.read(File) . It returns BufferedImage :
BufferedImage image = ImageIO.read(new File(filename));

Pavel K.
- 436
- 4
- 11
2
Try this, Use class "javax.imageio.ImageIO" like
BufferedImage originalImage = ImageIO.read(new File("c:\\image\\mypic.jpg"));
Also refer this link

Rahul Agrawal
- 8,913
- 18
- 47
- 59
-
Thanks Parvel :), But we both seems to be on same line. ā Rahul Agrawal Jun 30 '12 at 04:21
1
The safest way to convert a regular Image
to a BufferedImage
is just creating a new BufferedImage
and painting the Image
on it, like so:
Image original = ...;
BufferedImage b_img = new BufferedImage(original.getWith(), original.getHeight(), BufferedImage.TYPE_4BYTE_ARGB);
// or use any other fitting type
b_img.getGraphics().drawImage(original, 0, 0, null);
This may not be the best way regarding performance, but it is sure to always work.

Kierrow
- 685
- 1
- 7
- 14
0
Java 2D⢠supports loading these external image formats into its BufferedImage format using its Image I/O API
which is in the javax.imageio
package. Image I/O has built-in support for GIF, PNG, JPEG, BMP, and WBMP.
To load an image from a specific file use the following code:
BufferedImage img = null;
try {
img = ImageIO.read(new File("image.jpg"));
} catch (IOException e) {
e.printStackTrace()
}

K_Anas
- 31,226
- 9
- 68
- 81
-
Better to not mention exception handling, than botch it! Call `e.printStackTrace()` ā Andrew Thompson Jun 30 '12 at 06:38