Save buffered image with transparent background(保存具有透明背景的缓冲图像)
问题描述
我将签名图像保存为 .jpg 图片.我使用graphic2d在图像上绘制签名的每个像素(使用签名平板电脑获得),它工作得很好,但我总是得到一个白色的背景.如果我想在PDF文档上签名,jpg图片的白色方块的边框覆盖了PDF的部分文字.
I'm saving the image of a signature as a .jpg picture. I use graphic2d to paint on the image every pixel of the signature (gotten with a signature tablet) and it works perfectly but I'm always gettin a white background. If I want to put the signature on a PDF document, the borders of the white square of the jpg image covers some of the words of the PDF.
我想要得到的是用透明背景保存 jpg 图像,所以当我把它放在 PDF 上时,没有任何文字被白色图像背景覆盖,只有签名线.
What I want to get is to save the jpg image with a transparent background, so when I put it on the PDF there are no words covered with the white image background but just the signature lines.
这是保存缓冲图像的代码.它在白色背景下完成.
This is the code that saves the buffered image. It does it with the white background.
// This method refers to the signature image to save
private RenderedImage getImage() {
int width = tabletWidth;
int height = tabletHeight;
// Create a buffered image in which to draw
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// Create a graphics contents on the buffered image
Graphics2D g2d = bufferedImage.createGraphics();
// Draw graphics
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);
drawPoints(Tablet.getPenPoints(), g2d, Color.BLACK);
// Graphics context no longer needed so dispose it
g2d.dispose();
return bufferedImage;
}
我尝试将其设置为透明但没有成功,所以我发布了这个工作部分.
I tried to set it transparent but with no success, so I posted this working part.
推荐答案
使用 BufferedImage.TYPE_INT_ARGB
代替 BufferedImage.TYPE_INT_RGB
.并将其保存为PNG
图片,JPEG
不支持透明度.
Use BufferedImage.TYPE_INT_ARGB
instead of BufferedImage.TYPE_INT_RGB
. And save it to PNG
image, JPEG
does not support the transparency.
UPD:
为了设置背景透明,使用它:
For set the background transparent, use it:
g2d.setComposite(AlphaComposite.Clear);
g2d.fillRect(0, 0, w, h);
为了绘制你的图像:
g2d.setComposite(AlphaComposite.Src);
drawPoints(Tablet.getPenPoints(), g2d, Color.BLACK);
这篇关于保存具有透明背景的缓冲图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:保存具有透明背景的缓冲图像
基础教程推荐
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01