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

Java 使用透明背景保存缓冲图像,java,graphics,background,transparent,bufferedimage,Java,Graphics,Background,Transparent,Bufferedimage,我正在将签名图像保存为.jpg图片。我使用graphic2d在图像上绘制签名的每个像素(通过签名平板电脑获得),效果非常好,但我总是使用白色背景。 如果我想在PDF文档上签名,jpg图像的白色方框的边框会覆盖PDF中的一些单词 我想得到的是用一个透明的背景保存jpg图像,所以当我把它放在PDF上时,没有白色图像背景覆盖的单词,只有签名行 这是保存缓冲图像的代码。它是用白色背景做的 // This method refers to the signature image to save priv

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

我尝试将其设置为透明,但没有成功,因此我发布了此工作部件。

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

如果要将缓冲图像设置为没有Alpha分量的RGB类型,则必须使用具有Alpha分量的RGB来保持透明度。

使用
缓冲图像。键入\u INT\u ARGB
而不是
缓冲图像。键入\u INT\u RGB
。并将其保存到
PNG
image,
JPEG
不支持透明度

UPD:

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

g2d.setComposite(AlphaComposite.Clear);
g2d.fillRect(0, 0, w, h);
为绘制您的图像:

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

正如其他人提到的,您无法以透明方式保存JPEG

但是,可以像存储文件一样存储文件(在JPEG中,尽管我建议在本例中使用灰度JPEG),然后将白色部分解释为透明,黑色部分解释为不透明(即:将灰度图像用作alpha遮罩)。然后你可以简单地把不透明的部分涂成黑色或蓝色,看起来像钢笔墨水

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

准备使用端到端示例

它将创建具有透明度和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);
    }
}

如果一张图片里面有白色的部分不应该是透明的?@SeniorJD OP的问题是关于平板电脑的签名,所以我认为这无关紧要。将白色区域视为纸张,黑色部分视为墨水覆盖。如果你真的想要不透明的白色部分,它将无法工作。我使用了buffereImage.TYPE_INT_ARGB,并将其保存为png图像。但是现在我如何设置背景的alpha参数呢?很好的解决方案:)