/** * savetoweb adapted from http://processinghacks.com/hacks:savetoweb * @author Yonas Sandbok * * saveasjpg adapted from http://processinghacks.com/hacks:saveasjpg * @author Yonas Sandbok */ class framePoster{ String url = "your directory here"; String title; String folder; framePoster(String tt, String ff){ title=tt; folder=ff; } void postFrame() { println("SAVING PNG START"); String fileName =title+"_"+year()+nf(month(),2)+nf(day(),2)+"-"+nf(hour(),2)+nf(minute(),2)+nf(second(),2); postData(fileName,"png","gallery/"+folder,bufferImage(),true); println("SAVING PNG STOP"); println("SAVING PNG START"); postData(fileName,"png","gallery/"+ folder+"/thumbs",thumbImage(),false); println("SAVING PNG STOP"); } void postData(String title, String ext, String folder, byte[] bytes, boolean popup) { try{ URL u = new URL(url+"saveFile.php?title="+title+"&ext="+ext+"&folder="+folder); URLConnection c = u.openConnection(); // post multipart data c.setDoOutput(true); c.setDoInput(true); c.setUseCaches(false); // set request headers c.setRequestProperty("Content-Type", "multipart/form-data; boundary=AXi93A"); // open a stream which can write to the url DataOutputStream dstream = new DataOutputStream(c.getOutputStream()); // write content to the server, begin with the tag that says a content element is comming dstream.writeBytes("--AXi93A\r\n"); // discribe the content dstream.writeBytes("Content-Disposition: form-data; name=\"data\"; filename=\"whatever\" \r\nContent-Type: image/jpeg\r\nContent-Transfer-Encoding: binary\r\n\r\n"); dstream.write(bytes,0,bytes.length); // close the multipart form request dstream.writeBytes("\r\n--AXi93A--\r\n\r\n"); dstream.flush(); dstream.close(); // read the output from the URL try{ BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream())); String sIn = in.readLine(); boolean b = true; while(sIn!=null){ if(sIn!=null){ if(popup) { if(sIn.substring(0,folder.length()).equals(folder)) link(url+sIn, "gallery"); } System.out.println(sIn); } sIn = in.readLine(); } } catch(Exception e){ e.printStackTrace(); } } catch(Exception e){ e.printStackTrace(); } } byte[] bufferImage(){ ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedImage img = new BufferedImage(width-controlWidth,height, BufferedImage.TYPE_INT_RGB); loadPixels(); img.setRGB(0,0,width-controlWidth,height,g.pixels,0,width); try{ javax.imageio.ImageIO.write(img,"png",out); } catch(FileNotFoundException e){ System.out.println(e); } catch(IOException ioe){ System.out.println(ioe); } return out.toByteArray(); } import com.sun.image.codec.jpeg.*; byte[] thumbImage(){ PImage src=new PImage(96,96); src.copy(g,0,0,width-controlWidth,height,0,0,96,96); for (int i=0;i<96;i++){ for (int j=0;j<96;j++){ if ((i<2)||(i>93)||(j<2)||(j>93)) src.pixels[i+j*96]=color(100); } } ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedImage img = new BufferedImage(src.width, src.height, 1); loadPixels(); for(int i = 0; i < src.width; i++) for(int j = 0; j < src.height; j++) img.setRGB(i,j,src.pixels[j*src.width+i]); try{ JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam encpar = encoder.getDefaultJPEGEncodeParam(img); encpar.setQuality(.9,true); encoder.setJPEGEncodeParam(encpar); encoder.encode(img); } catch(FileNotFoundException e){ System.out.println(e); } catch(IOException ioe){ System.out.println(ioe); } return out.toByteArray(); } }