如何从java中的图像中获取Raster?

时间:2023-02-09 14:39:10

I'm trying to load a gif image from a url into a java.util.image.Raster so I can manipulate it. The only method for loading and decompressing an image I could find was Toolkit.getImage, which returns a java.awt.Image. I need to turn that into a Raster so I can work with it. Suggestions?

我正在尝试将来自url的gif图像加载到java.util.image.Raster中,以便我可以操作它。加载和解压缩我可以找到的图像的唯一方法是Toolkit.getImage,它返回一个java.awt.Image。我需要将其转换为Raster,以便我可以使用它。建议?

2 个解决方案

#1


Load your Image into a Buffered Image and then get the data from it

将图像加载到缓冲图像中,然后从中获取数据

BufferedImage img = null;
try {
   img = ImageIO.read(new File ("c:/imageFile.gif"));
} catch(Exception e) {}

Raster R=img.getData();

#2


if you just want to draw on this image, you can get a Graphics2D context, from your BufferedImage.

如果你只是想在这个图像上绘图,你可以从你的BufferedImage获得一个Graphics2D上下文。

Graphics2D g=(Graphics2D) image.getGraphics();
//draw over your image
g.drawLine(1,1,100,100);
g.dispose();
//save your image, display it, etc...

#1


Load your Image into a Buffered Image and then get the data from it

将图像加载到缓冲图像中,然后从中获取数据

BufferedImage img = null;
try {
   img = ImageIO.read(new File ("c:/imageFile.gif"));
} catch(Exception e) {}

Raster R=img.getData();

#2


if you just want to draw on this image, you can get a Graphics2D context, from your BufferedImage.

如果你只是想在这个图像上绘图,你可以从你的BufferedImage获得一个Graphics2D上下文。

Graphics2D g=(Graphics2D) image.getGraphics();
//draw over your image
g.drawLine(1,1,100,100);
g.dispose();
//save your image, display it, etc...