Converting Image to BufferedImage

荒凉一梦 提交于 2019-12-11 02:56:01

问题


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?


回答1:


use ImageIO.read(File) . It returns BufferedImage :

BufferedImage image = ImageIO.read(new File(filename));



回答2:


Try this, Use class "javax.imageio.ImageIO" like

BufferedImage originalImage = ImageIO.read(new File("c:\\image\\mypic.jpg"));

Also refer this link

http://www.dzone.com/snippets/converting-images




回答3:


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.




回答4:


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()
}



回答5:


To load an image from a specific file use the following code:
read more Reading/Loading an Image.
Working with Images

BufferedImage img = null;
 try {
   img = ImageIO.read(new File("your/image/path/name.jpg"));
  } catch (IOException e) { 
   // handle exception 
  }


来源:https://stackoverflow.com/questions/11271329/converting-image-to-bufferedimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!