17

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

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

4

5 回答 5

52

使用BufferedImage.TYPE_INT_ARGB而不是BufferedImage.TYPE_INT_RGB. 并将其保存为PNG图像,JPEG不支持透明度。

升级版:

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

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

并绘制您的图像:

g2d.setComposite(AlphaComposite.Src);
drawPoints(Tablet.getPenPoints(), g2d, Color.BLACK);
于 2013-06-24T09:16:16.697 回答
2

准备使用端到端示例

它将创建具有透明度和 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);
    }
}
于 2019-04-10T11:16:09.913 回答
1

您正在设置缓冲图像的类型只有 RGB,它没有 Alpha 分量,您必须使用具有 Alpha 的图像来保持透明度。

于 2013-06-24T09:14:49.730 回答
1

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

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

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

于 2013-06-24T09:18:17.533 回答
0

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

于 2013-06-24T09:12:07.073 回答