We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › save to web --- image export
Pages: 1 2 3 
save to web --- image export (Read 15129 times)
Re: save to web --- image export
Reply #15 - Jul 23rd, 2008, 7:05pm
 
Done!
You can find the code in three parts:
Upload.php
ImageUpload.pde
DataUpload.java

Obviously the Upload.php must be on the server, and you have to adjust some parameters, mostly the $destDir path (should be dedicated to uploads, because of the auto-deletion feature) and the $maxFiles parameter.

ImageUpload is a sample / example Processing script using my image upload class, DataUpload.

I tested it a bit, it works quite well. But the Jpeg images I got are quite strange, the colors are wrong.
I can try a finer method to produce them (there is a bit of commented out code in this direction) but I didn't felt the urge... PNG is quite a fine format for such output.

[EDIT] I fixed the Jpeg images: must get rid of the alpha channel before writing. Also finished the writing with quality control.
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();
 }
}


Re: save to web --- image export
Reply #17 - Mar 23rd, 2009, 10:31am
 
You get a sIn string from the server, and apply sIn.substring(0,folder.length()). You should first check that the length of this string is at least equals to folder length...
Or use sIn.startsWith(folder) instead.
Re: save to web --- image export
Reply #18 - Mar 23rd, 2009, 10:54pm
 
tx phil it worked.

But after all I use this http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Integrate;action=display;num=1131152633
easy and just what I wanted.
Re: save to web --- image export
Reply #19 - Mar 23rd, 2009, 11:35pm
 
Ah, good, as long as you don't use this as an applet...
Re: save to web --- image export
Reply #20 - Aug 28th, 2009, 12:46pm
 
I get this error:

Code:

java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.substring(String.java:1934)
at org.seltar.Bytes2Web.PostToWeb._post(PostToWeb.java:127)
at org.seltar.Bytes2Web.ImageToWeb.post(ImageToWeb.java:73)
at OnlineSketch.mousePressed(OnlineSketch.java:34)
at processing.core.PApplet.handleMouseEvent(PApplet.java:1608)
at processing.core.PApplet.dequeueMouseEvents(PApplet.java:1545)
at processing.core.PApplet.handleDraw(PApplet.java:1437)
at processing.core.PApplet.run(PApplet.java:1328)
at java.lang.Thread.run(Thread.java:619)


with the following code:
Code:

import org.seltar.Bytes2Web.*;

ImageToWeb itw;

void setup(){
 size(800,600);
 itw = new ImageToWeb(this);
 itw.setType(ImageToWeb.JPEG);
}

void draw(){
 line(pmouseX, pmouseY, mouseX, mouseY);
}

void mousePressed() {
 itw.post("test", "domain/images/upload.php", "test.jpg", true, itw.getBytes());
}


domain is the domain i use

What's the problem?


I solved the problem by removing a lot of unused functionallity from the PHP file.
Re: save to web --- image export
Reply #21 - Mar 29th, 2010, 5:50am
 
So far I'm not having any luck using the example sketch. The URL is supposed to be to Upload.php right? Or should it be the folder containing it? Or something else? I tried both of those, anyway, and it seems a lot like the sketch is just not doing anything when I press a key to trigger uploading - no error messages or anything, unless I try to save a pdf, in which case I get Code:
java.lang.NoClassDefFoundError: processing/pdf/PGraphicsPDF 

Re: save to web --- image export
Reply #22 - Mar 30th, 2010, 4:57am
 
It is hard to help you without knowing exactly what you did.
You have to change a number of parameters on the Processing side and on the PHP side. Obviously, you must have a host allowing PHP scripts. You need to create the right directory structure. Etc.
Re: save to web --- image export
Reply #23 - May 26th, 2010, 3:22pm
 
i always wanted to test the upload to web script.
So i gave it a try. I made changes in the java file and entered the directory of the Upload.php . I didnt made any changes to the pde and the php although i know that i probably have to make some changes in the php file as well. Right now i am getting this error:


----- 200 -----
<br />
<b>Fatal error</b>:  Call to undefined function: scandir() in <b>/mnt/weba/10/32/5192632/htdocs/test/Upload.php</b> on line <b>147</b><br />
---------------


What are the changes i have to make to the php? i couldnt figure it out.
Thx!


Re: save to web --- image export
Reply #24 - May 26th, 2010, 10:31pm
 
Strange, scandir is a regular PHP function.
But I see it comes with PHP5, perhaps you have an obsolete installation still running PHP4

You can safely remove that lines up to the following loop, it is only to clean up the upload directory to avoid having files cumulating there and saturating the hard disk... (or disk quota!).
Note: you must have an 'Upload' dir at same level as the folder 'test' you use. You can change its name/location in the $destDir variable (but I see I also hard-coded it in the $imageURL variable, bad practice...) PHP must have write rights in this folder, too.
Re: save to web --- image export
Reply #25 - May 26th, 2010, 11:23pm
 
Thx, that was the problem
the server was running PHP Version 4.4.9 but, i can make use of the newest php version by changing it to php5. so thats fine.
Re: save to web --- image export
Reply #26 - Jun 8th, 2010, 1:57am
 
Hi PhiLho, your upload script still works great but beside uploading an image i also need to upload a text file at the same time, with some information in it. i tried to use seltars post2web library to do so, but i get the

"java.lang.StringIndexOutOfBoundsException: String index out of range: 4" error.

Deleting some unused functions of the php file like mentioned above it probably not the best way to solve it. So i am either asking for a way to upload a text file with your script, or doesnt anybody figured out, what the string error might be ?

Re: save to web --- image export
Reply #27 - Jun 8th, 2010, 4:43am
 
The library should work for text files with little changes.
I don't know on which code you get the exception you report, so it is hard to diagnose... It is likely to be a substring() call on a three letter string, trying to access a fourth position.
Re: save to web --- image export
Reply #28 - Jun 8th, 2010, 5:38am
 
i get the problem with the simple example provided by seltar.

Quote:
import processing.pdf.*;
import org.seltar.Bytes2Web.*;
PDFToWeb pdf;

void setup()
{
 size(800,600);
 pdf = new PDFToWeb(this);
}


void draw()
{
 background(0);
 stroke(255);
 line(random(width),random(height),random(width),random(height));
 pdf.addPage(); // if you want each frame to be on it's own page
}

void keyPressed()
{
 String url = "http://www.onformative.com";
 if(key == 'p'){
   if(!pdf.isRecording()){
     pdf.startRecording();
   }else{
     pdf.save("pdf");
     pdf.post("test",url,"pdf-test",true);
   }
 }
 if(key == 'j'){
   ImageToWeb img = new ImageToWeb(this);
   img.save("jpg",true);
   img.post("test",url,"jpg-test",true,img.getBytes(g));
 }
 if(key == 't'){
   ImageToWeb img = new ImageToWeb(this);
   img.setType(ImageToWeb.TIFF);
   img.save("tiff",true);
   img.post("test",url,"tiff-test",true);
 }
 if(key == 'n'){
   ImageToWeb img = new ImageToWeb(this);
   img.setType(ImageToWeb.PNG);
   img.save("png",true);
   img.post("test",url,"png-test",true);
 }
 if(key == 'g'){
   ImageToWeb img = new ImageToWeb(this);
   img.setType(ImageToWeb.GIF);
   img.save("gif",true);
   img.post("test",url,"gif-test",true);
 }
}



red part is where i get the problem and
http://www.onformative.com/Upload.php is where the file is. Not sure if i missed anything.
but i believe i did everything as described at seltars library page
http://libraries.seltar.org/postToWeb/
Re: save to web --- image export
Reply #29 - Jun 8th, 2010, 6:52am
 
You cannot have this error on this line, perhaps you have a line number for the generated Java file.
Note that's an issue with seltar's library, perhaps a post in the Library section is more appropriate.
Pages: 1 2 3