import java.awt.image.BufferedImage; import java.io.*; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.net.*; import java.util.*; import javax.imageio.*; import processing.core.*; import javax.imageio.stream.*; /** * @author Daniel Shiffman May 2006 Class to create a JPG file from a PImage and post to a PHP script which uploads to server HTTP Post code based heavily on examples from * Vlad Patryshev (http://www.devx.com/Java/Article/17679/1954?pf=true) PHP Source available here: http://www.shiffman.net/ants/upload/upload.phps * Kyle Steinfeld (http://www.ksteinfe.net) Dec 2006 modified to use BufferedImage and ByteArrayOutputStream rather than a saved Jpg file, so that it's possible to run within a browser */ public class PNGMakerUploader { String uploadURL; // URL for php script Random generator; // generate random number for "boundary" BufferedImage bImg; boolean verbose; URL url; URLConnection connection; OutputStream os; int width; int height; // Constructor public PNGMakerUploader(PImage img, String url, int w, int h) { uploadURL = url; width = w; height = h; generator = new Random(); verbose = false; setImg(img); } public void setImg(PImage img) { if ((width != img.width) || (height != img.height)) { System.out.println("resizing image to ( " + width + " x " + height + " )"); PImage imgResized = new PImage(width, height); imgResized.copy(img, 0, 0, img.width, img.height, 0, 0, width, height); bImg = pImageToBufferedImage(imgResized); } else bImg = pImageToBufferedImage(img); } public void setVerbose(boolean b) { verbose = b; } public void upload() { try { System.out.println("posting to " + uploadURL); String boundary = "---------------------------" + randomString() + randomString() + randomString(); url = new URL(uploadURL); connection = url.openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); if (verbose) System.out.println("Opening connection. . ."); os = connection.getOutputStream(); writeParameter("form", "true", boundary); writeParameter("submit", "submit", boundary); writeFile("bytes", boundary); boundary(boundary); writeln("--"); if (verbose) System.out.println("Closing connection . . ."); os.close(); InputStream is = connection.getInputStream(); if (verbose) { BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; while ((line = rd.readLine()) != null) { System.out.println(line); } } } catch (MalformedURLException e) { System.out.println("Bad url: " + uploadURL); e.printStackTrace(); } catch (IOException e) { System.out.println("Problem posting to: " + uploadURL); e.printStackTrace(); } } // Functions for writing to OutputStream (thanks again to Vlad Patryshev (http://www.devx.com/Java/Article/17679/1954?pf=true) private String randomString() { return Long.toString(generator.nextLong(), 36); } private void write(String s) throws IOException { os.write(s.getBytes()); } private void writeln(String s) throws IOException { os.write(s.getBytes()); newline(); } private void writeParameter(String name, String value, String b) throws IOException { boundary(b); newline(); write("Content-Disposition: form-data; name=\""); write(name); write("\""); newline(); newline(); writeln(value); } private void pipe(InputStream in, OutputStream out) throws IOException { byte[] buf = new byte[500000]; int nread; int total = 0; // synchronized (in) { while ((nread = in.read(buf, 0, buf.length)) >= 0) { out.write(buf, 0, nread); total += nread; } // } out.flush(); in.close(); buf = null; } private void writeFile(String name, String b) throws IOException { boundary(b); newline(); write("Content-Disposition: form-data; name=\""); write(name); write("\""); write("; filename=\""); write("test.png"); write("\""); newline(); write("Content-Type: "); String type = "image/png"; writeln(type); // create ByteArrayInputStream from image contents ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bImg, "png", baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); newline(); pipe(bais, os); newline(); } private void newline() throws IOException { write("\r\n"); } private void boundary(String b) throws IOException { write("--"); write(b); } private BufferedImage pImageToBufferedImage(PImage pI) { BufferedImage bImage; if (pI.format == PConstants.ARGB) bImage = new BufferedImage(pI.width, pI.height, BufferedImage.TYPE_INT_ARGB); else bImage = new BufferedImage(pI.width, pI.height, BufferedImage.TYPE_INT_RGB); bImage.setRGB(0, 0, pI.width, pI.height, pI.pixels, 0, pI.width); return bImage; } }