转换一个Graphics2D在Image或BufferedImage的 [英] Convert a Graphics2D to an Image or BufferedImage
本文介绍了转换一个Graphics2D在Image或BufferedImage的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里有一个小问题。
I have a little problem here.
我有一个小程序,在这里用户可以画在里面。要做到这一点,我用的是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屋!
查看全文