I made a script that converts screen to jpg, then to base64, then posts it to a php-file, which converts it back to jpg, and saves on server.
Remember to set folder with php-file in to writable (chmod 777)
Code:
usage:
Post p;
void setup()
{
size(200,200);
p = new Post();
}
void draw()
{
rect(20,20,40,50);
if(mousePressed) p.postIt();
}
// ----------- POST CLASS ----------- //
class Post {
/* Name of file to be sent. */
String[] parameter = { "img", };
String[] value = { "", };
String urlMail = ">INSERT URL TO PHP-FILE<";
void postIt() {
encode(bufferImage());
postNewItem(parameter,value,urlMail);
}
boolean postNewItem (String[] Par, String[] Val, String strURL) {
boolean ret = false;
try {
URL url;
URLConnection urlConn;
DataOutputStream dos;
DataInputStream dis;
String s = "";
String S = "";
url = new URL(strURL);
urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");
dos = new DataOutputStream (urlConn.getOutputStream());
// generate the post-string
for (int i=0; i<Par.length; i++)
{
if (S.length() > 0) S += "&";
S += Par[i] + "=" + Val[i];
}
// SEND
String message=S;
dos.writeBytes(message);
dos.flush();
dos.close();
// RECIEVE
dis = new DataInputStream(urlConn.getInputStream());
s = dis.readLine();
println(dis.readLine());
println(dis.readLine());
println(dis.readLine());
println(dis.readLine());
println(dis.readLine());
dis.close();
println(s); // if it outputs success, and nothing happens check this out..
if (s != "") { // Got Some reply
ret = true;
}
else {
ret = false;
}
} // end of "try"
catch (MalformedURLException mue) {
;
}
catch (IOException ioe) {
;
}
return ret;
} // end of postNewItem() method
String BaseTable[] = {
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
"Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f",
"g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v",
"w","x","y","z","0","1","2","3","4","5","6","7","8","9","+","/"
};
void encode(ByteArrayOutputStream bout) {
// read file into bytes
byte bytes[] = bout.toByteArray();
int n = bytes.length;
if (n < 1) return; // no bytes to encode!?!
byte buf[] = new byte[4]; // array of base64 characters
int n3byt = n / 3; // how 3 bytes groups?
int nrest = n % 3; // the remaining bytes from the grouping
int k = n3byt * 3; // we are doing 3 bytes at a time
int linelength = 0; // current linelength
int i = 0; // index
// do the 3-bytes groups ...
while ( i < k ) {
buf[0] = (byte)(( bytes[i] & 0xFC) >> 2);
buf[1] = (byte)(((bytes[i] & 0x03) << 4) |
((bytes[i+1] & 0xF0) >> 4));
buf[2] = (byte)(((bytes[i+1] & 0x0F) << 2) |
((bytes[i+2] & 0xC0) >> 6));
buf[3] = (byte)( bytes[i+2] & 0x3F);
value[0] = value[0]+BaseTable[buf[0]];
value[0] = value[0]+BaseTable[buf[1]];
value[0] = value[0]+BaseTable[buf[2]];
value[0] = value[0]+BaseTable[buf[3]];
if ((linelength += 4) >= 76) {
//value[0] = value[0]+"\n";
linelength = 0;
}
i += 3;
}
// deals with with the padding ...
if (nrest==2) {
// 2 bytes left
buf[0] = (byte)(( bytes[k] & 0xFC) >> 2);
buf[1] = (byte)(((bytes[k] & 0x03) << 4) |
((bytes[k+1] & 0xF0) >> 4));
buf[2] = (byte)(( bytes[k+1] & 0x0F) << 2);
}
else if (nrest==1) {
// 1 byte left
buf[0] = (byte)((bytes[k] & 0xFC) >> 2);
buf[1] = (byte)((bytes[k] & 0x03) << 4);
}
if (nrest > 0) {
// send the padding
//if ((linelength += 4) >= 76) value[0] = value[0]+"\n";
value[0] = value[0]+BaseTable[buf[0]];
value[0] = value[0]+BaseTable[buf[1]];
if (nrest==2) {
value[0] = value[0]+BaseTable[buf[2]];
}
else {
value[0] = value[0]+"=";
}
value[0] = value[0]+"=";
}
}
}
import com.sun.image.codec.jpeg.*;
ByteArrayOutputStream bufferImage(){
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedImage img = new BufferedImage(width, height, 2);
img = (BufferedImage)createImage(width, height);
loadPixels();
for(int i = 0; i < width; i++)
{
for(int j = 0; j < height; j++)
{
int id = j*width+i;
img.setRGB(i,j, pixels[id]);
}
}
try{
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(img);
}
catch(FileNotFoundException e){
System.out.println(e);
}
catch(IOException ioe){
System.out.println(ioe);
}
return out;
}