Вот адаптация ответа @ 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
}