本文介绍了透明背景保存缓冲图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将签名的图像保存为.jpg图片。我使用graphic2d在图像上绘制签名的每个像素(使用签名平板电脑获得),并且它完美地工作,但我总是开始白色背景。
如果我想将签名放在PDF文档中,那么jpg图像的白色方框的边框将覆盖PDF的一些字词。



我想要得到的是将JPG图像保存为透明背景,所以当我将它放在PDF上时,没有任何用白色图像背景覆盖的单词,而只是签名行。



这是保存缓冲图像的代码。它用白色背景做它。

 使用 C> BufferedImage.TYPE_INT_RGB 。并保存到 PNG 图片, JPEG 不支持透明度。



UPD:



要设置背景透明,请使用它:

  g2d.setComposite(AlphaComposite.Clear); 
g2d.fillRect(0,0,w,h);

并绘制您的图片:

  g2d.setComposite(AlphaComposite.Src); 
drawPoints(Tablet.getPenPoints(),g2d,Color.BLACK);


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.

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.

解决方案

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);

And for draw your image:

g2d.setComposite(AlphaComposite.Src);
drawPoints(Tablet.getPenPoints(), g2d, Color.BLACK);

这篇关于透明背景保存缓冲图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:53