Sending images/JSON data over OSC w. oscP5?

Hi

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

Hi!
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) :
https://shiffman.net/processing.org/udp/2010/11/13/streaming-video-with-udp-in-processing/
To encode/decode images like this, you first need to import those :

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

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
  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();
  }
  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);
    img.loadPixels();
    try {
      // Make a BufferedImage out of the incoming bytes
      BufferedImage bimg = ImageIO.read(bais);
      // Put the pixels into the video PImage
      bimg.getRGB(0, 0, img.width, img.height, img.pixels, 0, img.width);
    } catch (Exception e) {
      e.printStackTrace();
    }
    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:

2 Likes

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.

http://opensoundcontrol.org/topic/247

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. https://joearms.github.io/published/2016-01-28-A-Badass-Way-To-Connect-Programs-Together.html

For images, related discussion:

https://resolume.com/forum/viewtopic.php?t=13654

2 Likes