Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Reliably send images over net

$
0
0

Hello,

I’m trying to transfer images over net between Processing-sketches on different computers. I’ve been making making some small test sketches to test this out. I’m using built-in java library to convert PImage to JPEG-encoded byte-arrays (to keep size down) which I in turn send over the net.

However, I’ve never really done much networking programming before and I’m having trouble getting this to work reliably. Generally my problem is that very often I just get a part of the image, but not the whole thing.

I am not really looking for code help per se, just what approach I should take. Do I need to send the image in parts and then stitch it together (or the corresponding bytes together)? Will I then need some way to make sure the server know it received a whole part? Or should I just read from the buffer in a different way? Are there any ready-made solutions like libraries that can deal with this?

And generally my goal here is simply to transfer a image from one computer to another, am I going about this in the easiest and smartest way? Would be easier to just bounce this stuff off a web server somewhere?

Any pointers here would be hugely appreciated.

Here is my server code:

import processing.net.*;

Server server;
JPGEncoder jpg;
PImage img;

void setup() {
    size(1400, 800);
    jpg = new JPGEncoder();
    // Start a server at port 5204
    server = new Server(this, 5204);

    img = createImage(100, 100, RGB);
    println("Starting server");
}

void draw() {
    checkForIncomingImage();
    image(img, 0, 0);
}


void checkForIncomingImage() {
    Client nextClient = server.available();
    if (nextClient != null) {
        println("Client is available, reading bytes");

        byte[] byteBuffer = nextClient.readBytes();

        if (byteBuffer.length > 0) {
            println("Received data. Trying to decode.");
            try {
                img = jpg.decode(byteBuffer);
            } catch (IOException e) {
                println("IOException");
            } catch (NullPointerException e) {
                println("Probs incomplete image");
            } catch (ArrayIndexOutOfBoundsException e) {
                println("Probs also incomplete image (Out of Bounds)");
            }
        } else {
            println("Byte amount not above 0");
        }
    }
}

And here is my client code:

import processing.net.*;
import processing.video.*;

Client client;
Capture cam;
JPGEncoder jpg;

void setup() {
    jpg = new JPGEncoder();

    cam = new Capture(this, Capture.list()[1]);
    cam.start();

    // String server = "192.168.1.15";
    String server = "127.0.0.1";
    client = new Client(this, server, 5204);

    background(255);
    println("Starting client");
}

void draw() {

}

void keyTyped() {
    if (cam.available()) {
        println("Cam available. Going to read");
        cam.read();
        try {
            println("Getting image to memory");
            PImage img = cam.get();
            img.resize(500, 0);

            println("Encoding");
            byte[] encoded = jpg.encode(img, 0.1F);

            println("Writing to server");
            client.write(encoded);
        } catch (IOException e) {
            // Ignore failure to encode
            println("IOException");
        }


    }
}

You can also view the code at GitHub here: https://github.com/torbjornlunde/processing-experiments

Example of what an incomplete transfer of image looks like: proc-img-tranfser-example


Viewing all articles
Browse latest Browse all 2896

Trending Articles