angelo
YaBB Newbies
Offline
Posts: 16
Re: save to web --- image export
Reply #16 - Mar 23rd , 2009, 12:49am
I just saw that phil also writed some upload code. I used seltar's one. now I getting this error; code: SAVING JPG START java.lang.StringIndexOutOfBoundsException: String index out of range: 8 at java.lang.String.substring(String.java:1765) at volledig.postData(volledig.java:246) at volledig.saveToWeb_saveJPG(volledig.java:204) at volledig.keyPressed(volledig.java:63) at processing.core.PApplet.handleKeyEvent(PApplet.java:1747) at processing.core.PApplet.dequeueKeyEvents(PApplet.java:1730) at processing.core.PApplet.handleDraw(PApplet.java:1435) at processing.core.PApplet.run(PApplet.java:1328) at java.lang.Thread.run(Thread.java:613) SAVING JPG STOP Is this easy to solve? import processing.video.*; import javax.print.*; import javax.print.attribute.*; import com.sun.image.codec.jpeg.*; Capture cam; int row; void setup() { size(256, 768, P2D); cam = new Capture(this, 256, 192,30); row = 0; background(0); } void draw() { if (cam.available()) { cam.read(); //set(0,192*3,cam); image(cam, 0,576); if(keyPressed) if (key == 's' || key == 'S') { copy(0, 576, cam.width, cam.height, 0, row*192, cam.width, cam.height); delay(500); row+=1; } // if(keyPressed && key == 'p') p.printJpg(get(0,0,width,height)); } } void keyPressed(){ if (key == 'b' || key == 'B') { println ("ok"); saveToWeb_saveJPG("mytitle","savedJPG",get(0,0,width,height)); } } byte[] bufferImage(PImage srcimg){ ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedImage img = new BufferedImage(srcimg.width, srcimg.height, 2); img = (BufferedImage)createImage(srcimg.width,srcimg.height); for(int i = 0; i < srcimg.width; i++) for(int j = 0; j < srcimg.height; j++) img.setRGB(i,j,srcimg.pixels[j*srcimg.width+i]); try{ JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam encpar = encoder.getDefaultJPEGEncodeParam(img); encpar.setQuality(1,false); encoder.setJPEGEncodeParam(encpar); encoder.encode(img); } catch(FileNotFoundException e){ System.out.println(e); } catch(IOException ioe){ System.out.println(ioe); } return out.toByteArray(); } String url = "http://users.fulladsl.be/~spb11461/processing/"; void saveToWeb_saveJPG(String title, String folder, PImage src) { println("SAVING JPG START"); postData(title+"_"+year()+nf(month(),2)+nf(day(),2),"jpg",folder,bufferImage(sr c),true); println("SAVING JPG 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, "_blank"); System.out.println(sIn); } sIn = in.readLine(); } } catch(Exception e){ e.printStackTrace(); } } catch(Exception e){ e.printStackTrace(); } }