java 将图像转换为 BufferedImage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11271329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:28:33  来源:igfitidea点击:

Converting Image to BufferedImage

javabufferedimage

提问by kaysush

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?

我在磁盘上有一个图像。我想将其转换为 BufferedImage 以便我可以对其应用过滤器。有没有办法做到这一点?

回答by Pavel K.

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

使用ImageIO.read(File)。它返回 BufferedImage :

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

回答by Rahul Agrawal

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

试试这个,使用类“javax.imageio.ImageIO”

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

Also refer this link

另请参阅此链接

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

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

回答by Kierrow

The safest way to convert a regular Imageto a BufferedImageis just creating a new BufferedImageand painting the Imageon it, like so:

最安全的方式转换成一个普通ImageBufferedImage只是创建一个新的BufferedImage和画Image就可以了,就像这样:

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.

这可能不是关于性能的最佳方式,但它肯定会始终有效。

回答by K_Anas

Java 2D? supports loading these external image formats into its BufferedImageformat using its Image I/O APIwhich is in the javax.imageiopackage. Image I/O has built-in support for GIF, PNG, JPEG, BMP, and WBMP.

Java 2D?支持使用包中的BufferedImage格式将这些外部图像格式加载到其BufferedImage格式Image I/O APIjavax.imageio。Image I/O 内置了对 GIF、PNG、JPEG、BMP 和 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()
}

回答by Sumit Singh

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 
  }