将 Graphics2D 转换为 Image 或 BufferedImage [英] Convert a Graphics2D to an Image or BufferedImage
本文介绍了将 Graphics2D 转换为 Image 或 BufferedImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我这里有一个小问题.
我有一个小程序,用户可以在其中绘图".为此,我使用 java.awt.Graphics2D.但是,我该怎么做才能将用户绘制的图像保存为 JPEG 图像,或者至少将其转换为 BufferedImage 之类的?我不知道该怎么做.
I have an applet, where user can "draw" inside it. To do that, I use the java.awt.Graphics2D. But, how can I do to save the user draw image as a JPEG image, or at least, convert it to a BufferedImage or something? I don't know how to do that.
谢谢.
推荐答案
我就是这样做的,而且效果很好:
I do it that way, and works very well:
BufferedImage awtImage = new BufferedImage(drawPanel.getWidth(), drawPanel.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = awtImage.getGraphics();
drawPanel.printAll(g);
try
{
String caminhoImagem = System.getProperty("user.home") + "\\temps\\assinatura.jpg";
FileOutputStream fos = new FileOutputStream(caminhoImagem);
JPEGImageEncoderImpl j = new JPEGImageEncoderImpl(fos);
j.encode(awtImage);
fos.close();
} catch(e) {..... }
<小时>
仅此而已:)谢谢大家:)
That's all :) Thanks everyone :)
这篇关于将 Graphics2D 转换为 Image 或 BufferedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文