Sending images/JSON data over OSC w. oscP5?


Perhaps a bit of a weird question, but are there any tutorials on how to send images and/or data in JSON-format over OSC using the oscP5 library?

Or anyone here who has experience doing that kind of thing?

1 Like

I am currently doing something similar using the UDP library, the method should be the same with oscP5. To send images you first need to encode your image into a byte array, send it, and decode it on the other side! The following link helped me a lot (you can download the zip at the end) :
To encode/decode images like this, you first need to import those :

import javax.imageio.*;
import java.awt.image.*; 

Here is a function to encode images as byte arrays :

byte[] encodeIMG(PImage img){
  BufferedImage bimg = new BufferedImage( img_edit.width,img_edit.height, BufferedImage.TYPE_INT_RGB );

  // Transfer pixels from localFrame to the BufferedImage
  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) {
  byte[] b = baStream.toByteArray();
  return b;

And then here is the one to decode it :

PImage decodeIMG(byte[] b){
  //Read incoming data into a ByteArrayInputStream
    ByteArrayInputStream bais = new ByteArrayInputStream( b );
    // We need to unpack JPG and put it in the PImage video
    PImage img = createImage(240, 240, RGB);
    try {
      // Make a BufferedImage out of the incoming bytes
      BufferedImage bimg =;
      // Put the pixels into the video PImage
      bimg.getRGB(0, 0, img.width, img.height, img.pixels, 0, img.width);
    } catch (Exception e) {
    img.updatePixels(); // Update the PImage pixels
    return img;

Now you will probably need a few more steps if you are using oscP5, with UDP I just send the byte array as is…
As for sending JSON data I don’t know. :slight_smile:


For JSON, just send it as an OSC string.

If it is a very long string then you may need to break it into multiple strings and reassemble. This depends a lot on what your JSON is – if there are logical sub-units, if the order of transmission matters, if you want to set a start and stop check et cetera.

Think carefully about your use case. Some grumpy programmers argue that in general piping JSON like that is a Bad Idea, and there might be a better way of transmitting your data.

For images, related discussion: