动态生成透明跟踪像素

发布于 2024-10-09 23:01:12 字数 291 浏览 4 评论 0原文

我试图在 Java 中动态生成清晰的跟踪像素,但遇到了一些问题。我可以毫无问题地将其返回给用户,但我似乎无法获得正确的像素。我做错了什么?

这就是我所拥有的,它给了我一个 1x1 的白色像素。如何使其尽可能小(文件大小)并使其透明?

BufferedImage singlePixelImage = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_GRAY_TYPE);
singlePixelImage.setRGB(0, 0, 0xFFFFFF);

I'm trying to generate a clear tracking pixel dynamically in Java, but running into some issues. I have no problem returning this to the user, but I can't seem to get the pixel right. What am I doing wrong?

This is what I have, which gives me a 1x1 white pixel. How do I make this as small as possible (file size) and make it transparent?

BufferedImage singlePixelImage = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_GRAY_TYPE);
singlePixelImage.setRGB(0, 0, 0xFFFFFF);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花开雨落又逢春i 2024-10-16 23:01:12

我相信灰色图像类型不支持透明度。只是修改了 Łukasz 的答案以准确显示发生了什么。当您创建新图像时,它的所有像素的初始值都设置为 0。这意味着它是完全透明的。在下面的代码中我明确地做到了这一点:

    BufferedImage singlePixelImage = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
    Color transparent = new Color(0, 0, 0, 0);
    singlePixelImage.setRGB(0, 0, transparent.getRGB());

    File file = new File("pixel.png");
    try {
        ImageIO.write(singlePixelImage, "png", file);
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

I believe the GRAY image type doesn't support transparency. Only modified Łukasz's answer to show exactly what's going on. When you create new image all of it's pixels have initial value set to 0. So that means it's completely transparent. In following code I'm making it explicitly:

    BufferedImage singlePixelImage = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
    Color transparent = new Color(0, 0, 0, 0);
    singlePixelImage.setRGB(0, 0, transparent.getRGB());

    File file = new File("pixel.png");
    try {
        ImageIO.write(singlePixelImage, "png", file);
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
贱人配狗天长地久 2024-10-16 23:01:12

这是 @Rekin 的答案的改编,用于写入字节数组而不是文件。

动态生成的跟踪图像

public static byte[] get1x1PixelImage() throws IOException{

    // The following code was used to generate the tracking pixel.
    BufferedImage singlePixelImage = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
    Color transparent = new Color(0, 0, 0, 0);
    singlePixelImage.setRGB(0, 0, transparent.getRGB());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(singlePixelImage, "png", baos);
    byte[] imageInBytes = baos.toByteArray();
    baos.close();

    return imageInBytes;
}

获取静态跟踪图像

// Use either format, tracking gif is smaller then the png.
static byte[] trackingGif = { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x1, 0x0, 0x1, 0x0, (byte) 0x80, 0x0, 0x0, (byte)  0xff, (byte)  0xff,  (byte) 0xff, 0x0, 0x0, 0x0, 0x2c, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x2, 0x2, 0x44, 0x1, 0x0, 0x3b };
static byte[] trackingPng = {(byte)0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A,0x00,0x00,0x00,0x0D,0x49,0x48,0x44,0x52,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x08,0x06,0x00,0x00,0x00,0x1F,0x15,(byte)0xC4,(byte)0x89,0x00,0x00,0x00,0x0B,0x49,0x44,0x41,0x54,0x78,(byte)0xDA,0x63,0x60,0x00,0x02,0x00,0x00,0x05,0x00,0x01,(byte)0xE9,(byte)0xFA,(byte)0xDC,(byte)0xD8,0x00,0x00,0x00,0x00,0x49,0x45,0x4E,0x44,(byte)0xAE,0x42,0x60,(byte)0x82};

public static byte[] get1x1PixelImage() {
    return trackingGif;   // trackingGif is about 38 bytes where trackingPng is 68
}

Here's an adaptation of @Rekin's answer to write to a byte array instead of a file.

Tracking Image Generated Dynamically

public static byte[] get1x1PixelImage() throws IOException{

    // The following code was used to generate the tracking pixel.
    BufferedImage singlePixelImage = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
    Color transparent = new Color(0, 0, 0, 0);
    singlePixelImage.setRGB(0, 0, transparent.getRGB());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(singlePixelImage, "png", baos);
    byte[] imageInBytes = baos.toByteArray();
    baos.close();

    return imageInBytes;
}

Get Static Tracking Image

// Use either format, tracking gif is smaller then the png.
static byte[] trackingGif = { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x1, 0x0, 0x1, 0x0, (byte) 0x80, 0x0, 0x0, (byte)  0xff, (byte)  0xff,  (byte) 0xff, 0x0, 0x0, 0x0, 0x2c, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x2, 0x2, 0x44, 0x1, 0x0, 0x3b };
static byte[] trackingPng = {(byte)0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A,0x00,0x00,0x00,0x0D,0x49,0x48,0x44,0x52,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x08,0x06,0x00,0x00,0x00,0x1F,0x15,(byte)0xC4,(byte)0x89,0x00,0x00,0x00,0x0B,0x49,0x44,0x41,0x54,0x78,(byte)0xDA,0x63,0x60,0x00,0x02,0x00,0x00,0x05,0x00,0x01,(byte)0xE9,(byte)0xFA,(byte)0xDC,(byte)0xD8,0x00,0x00,0x00,0x00,0x49,0x45,0x4E,0x44,(byte)0xAE,0x42,0x60,(byte)0x82};

public static byte[] get1x1PixelImage() {
    return trackingGif;   // trackingGif is about 38 bytes where trackingPng is 68
}
不疑不惑不回忆 2024-10-16 23:01:12

如果这是一个跟踪像素,为什么要每次都生成它?生成一次,将其编码为 GIF 或 PNG 图像,然后仅将这些字节发送回 HTTP 客户端。这样就容易多了。

If this is a tracking pixel, why would you want to generate it each time again? Generate it once, encode it as a GIF or PNG image and only send those bytes back to the HTTP client. That's much easier.

谈场末日恋爱 2024-10-16 23:01:12

我认为创建具有正确类型的 BufferedImage 就足够了。没有必要调用setRGB方法。试试这个:

BufferedImage singlePixelImage = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
   
// then I'm saving the generated image in file:
File file = new File("pixel.png");
try {
   ImageIO.write(singlePixelImage, "png", file);
} catch (IOException e) {
   e.printStackTrace();
}

我在 Adob​​e PhotoShop 中检查了 Pixel.png 文件,它是透明的。

BufferedImage.TYPE_4BYTE_ABGR 指示添加一个额外的第四个字节,用于存储有关 Alpha 通道值(像素透明度)的信息。

I think that it is enough to create a BufferedImage with proper type. It's not necessary to call setRGB method. Try this:

BufferedImage singlePixelImage = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
   
// then I'm saving the generated image in file:
File file = new File("pixel.png");
try {
   ImageIO.write(singlePixelImage, "png", file);
} catch (IOException e) {
   e.printStackTrace();
}

I've checked pixel.png file in Adobe PhotoShop and it was transparent.

BufferedImage.TYPE_4BYTE_ABGR tells to add an additional, fourth byte which stores information about alpha channel value (transparency of a pixel).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文