使用透明背景保存缓冲图像

时间:2013-06-24 09:09:49

标签: java graphics background transparent bufferedimage

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

我想要的是用透明背景保存jpg图像,所以当我把它放在PDF上时,没有白色图像背景覆盖的文字,只有标记线。

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

 // 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;
}

我试图将其设置为透明但没有成功,所以我发布了这个工作部分。

5 个答案:

答案 0 :(得分:48)

使用BufferedImage.TYPE_INT_ARGB代替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);

答案 1 :(得分:1)

正如其他人所提到的,您无法保存具有透明度的JPEG。

然而,可以像你一样存储你的文件(在JPEG中,虽然我建议在这种情况下使用灰度JPEG),然后解释白色部分为透明,黑色部分为不透明(即:使用灰度图像作为alpha遮罩)。然后你可以简单地将不透明的部分着色为黑色或蓝色,看起来像钢笔墨水。

将白色区域视为纸张,将黑色部分视为墨水覆盖。请注意,此技术仅适用于所有白色像素都应透明的用例。在一般情况下,此主题中的其他答案将更有效。

答案 2 :(得分:0)

JPEG不支持透明度。您必须使用不同的目标格式,例如png。

答案 3 :(得分:0)

您正在设置缓冲图像的类型只是RGB没有Alpha组件,您必须使用具有alpha的一个以保持透明度。

答案 4 :(得分:0)

  

准备使用端到端示例

它将创建具有透明度和2个矩形的png图片

编译时间-2019_04_10__00_12_03_236

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

// ready to use end to end example
// it will create png picture with transparency and 2 x rectangles
// compilation time - 2019_04_10__00_12_03_236
public class java_create_png_image_with_transparency_end_to_end_example {

    public static void main(String[] args) throws IOException {
        Path outPath = Paths.get("C:\\_tmp_out_\\");
        if (!Files.exists(outPath)) {
            Files.createDirectory(outPath);
        }

        String timeNow = DateTimeFormatter
                .ofPattern("yyyy_MM_dd__HH_mm_ss_SSS")
                .format(LocalDateTime.now());
        String filename = "test_png_pic__" + timeNow + "__.png";
        File absOutFile = outPath.resolve(filename).toFile();

        int width = 300;
        int height = 300;

        BufferedImage bufferedImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = bufferedImage.createGraphics();
        g2d.setComposite(AlphaComposite.Clear);
        g2d.fillRect(0, 0, width, height);

        g2d.setComposite(AlphaComposite.Src);
        int alpha = 127; // 50% transparent
        g2d.setColor(new Color(255, 100, 100, alpha));
        g2d.fillRect(100, 100, 123, 123);

        g2d.setColor(new Color(0, 0, 0));
        g2d.fillRect(30, 30, 60, 60);

        g2d.dispose();

        ImageIO.write(bufferedImage, "png", absOutFile);
        System.out.println("File saved to:");
        System.out.println(absOutFile);
    }
}
相关问题