Based on // https://shiffman.net/processing.org/udp/2010/11/13/streaming-video-with-udp-in-processing/ I just implemented what you are looking for.
This is the sender. Don’t forget to place a picture in the data directory of the sketch.
Save this as sender.pde
// Send Images over UDP
import java.net.*;
import javax.imageio.*;
import java.awt.image.*;
import java.io.*;
// This is the port we are sending to
int clientPort = 11111;
// This is our object that sends UDP out
DatagramSocket ds;
// This is the image we are sending
PImage photo;
void setup() {
size(320,240);
photo = loadImage("data//space_shuttle.jpg");
// Setting up the DatagramSocket, requires try/catch
try {
ds = new DatagramSocket();
}
catch (SocketException e) {
e.printStackTrace();
}
noLoop();
}
void draw() {
image(photo, 0, 0);
broadcast(photo);
}
// Many Thanks to Daniel Shiffmann for the initial idea
// See https://shiffman.net/processing.org/udp/2010/11/13/streaming-video-with-udp-in-processing/
// Function to broadcast a PImage over UDP
// Special thanks to: http://ubaa.net/shared/processing/udp/
// (This example doesn't use the library, but you can!)
void broadcast(PImage img) {
// We need a buffered image to do the JPG encoding
BufferedImage bimg = new BufferedImage( img.width,img.height, BufferedImage.TYPE_INT_RGB );
// Transfer pixels from localFrame to the BufferedImage
img.loadPixels();
bimg.setRGB( 0, 0, img.width, img.height, img.pixels, 0, img.width);
// Need these output streams to get image as bytes for UDP communication
ByteArrayOutputStream baStream = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baStream);
// Turn the BufferedImage into a JPG and put it in the BufferedOutputStream
// Requires try/catch
try {
ImageIO.write(bimg, "jpg", bos);
}
catch (IOException e) {
e.printStackTrace();
}
// Get the byte array, which we will send out via UDP!
byte[] packet = baStream.toByteArray();
// Send JPEG data as a datagram
// println("Sending datagram with " + packet.length + " bytes");
try {
ds.send(new DatagramPacket(packet,packet.length, InetAddress.getByName("localhost"),clientPort));
}
catch (Exception e) {
e.printStackTrace();
}
}type or paste code here
And the receiver below.
Save as receiver.pde. Run both sketeches and it will work. More work is required if you place sender and receiver in different system.
// https://shiffman.net/processing.org/udp/2010/11/13/streaming-video-with-udp-in-processing/
import java.awt.image.*;
import javax.imageio.*;
import java.net.*;
import java.io.*;
// Port we are receiving.
int port = 11111;
DatagramSocket ds;
// A byte array to read into (max size of 65536, could be smaller)
byte[] buffer = new byte[65536];
PImage video;
void setup() {
size(400,300);
try {
ds = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
video = createImage(320,240,RGB);
}
void draw() {
// checkForImage() is blocking, stay tuned for threaded example!
checkForImage();
// Draw the image
background(0);
imageMode(CENTER);
image(video,width/2,height/2);
}
void checkForImage() {
DatagramPacket p = new DatagramPacket(buffer, buffer.length);
try {
ds.receive(p);
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = p.getData();
println("Received datagram with " + data.length + " bytes." );
// Read incoming data into a ByteArrayInputStream
ByteArrayInputStream bais = new ByteArrayInputStream( data );
// We need to unpack JPG and put it in the PImage video
video.loadPixels();
try {
// Make a BufferedImage out of the incoming bytes
BufferedImage img = ImageIO.read(bais);
// Put the pixels into the video PImage
img.getRGB(0, 0, video.width, video.height, video.pixels, 0, video.width);
} catch (Exception e) {
e.printStackTrace();
}
// Update the PImage pixels
video.updatePixels();
}