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

问题描述 投票:15回答:5

我将签名图像保存为.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;
}

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

java graphics background transparent bufferedimage
5个回答
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
投票

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

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

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


0
投票

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


0
投票

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


0
投票

准备好使用端到端的例子

它将创建具有透明度和2 x矩形的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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.