I rewrote my entire "post jpg to web"-code, and got a significant increase in speed.
Usage:
Code:
void saveJPG()
{
println("SAVING JPG START");
postData(bufferImage(get(0,0,width,height)).toByteArray());
println("SAVING JPG STOP");
}
Code:
Code:
import com.sun.image.codec.jpeg.*;
void postData(byte[] bytes)
{
try{
String url = "http://YourWebURL/";
URL u = new URL(url+"saveJpg.php");
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, (in this case it's an image file)
dstream.writeBytes("Content-Disposition: form-data; name=\"img\"; 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{
DataInputStream in =
new DataInputStream(
new BufferedInputStream(c.getInputStream()));
String sIn = in.readLine();
boolean b = true;
while(sIn!=null){
if(sIn!=null){
if(sIn.substring(0,5).equals("saved")) link(url+sIn, "_new");
System.out.println(sIn);
}
sIn = in.readLine();
}
}
catch(Exception e){
e.printStackTrace();
}
}
catch(Exception e){
e.printStackTrace();
}
}
ByteArrayOutputStream 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++)
{
int id = j*srcimg.width+i;
img.setRGB(i,j, srcimg.pixels[id]);
}
}
try{
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam encpar = encoder.getDefaultJPEGEncodeParam(img);
encpar.setQuality(1,false); // 0.0-1.0, force baseline
encoder.setJPEGEncodeParam(encpar);
encoder.encode(img);
}
catch(FileNotFoundException e){
System.out.println(e);
}
catch(IOException ioe){
System.out.println(ioe);
}
return out;
}
PHP: (assumed saveJpg.php)
Code:
<?
$Title = "ImageTitle";
$savepath = dirname($_SERVER["PATH_TRANSLATED"]);
$filename = $Title."_".date("Ymd-His").".jpg";
while(file_exists("saved/".$filename))
$filename = $Title."_".date("Ymd-His")."-".rand(2,500).".jpg";
if (is_uploaded_file($img))
{
$newfile = $savepath."/saved/".$filename;
if (!copy($img, $newfile))
{
// if an error occurs the file could not
// be written, read or possibly does not exist
echo "Error Uploading File.#1";
exit();
}else{
echo "saved/".$filename;
}
}else{
echo "Error Uploading File.#2";
exit();
}
?>
This code assumes you have a folder named "saved" in the same folder you have saveJpg.php in.
The folder "saved" should also be set to writable (chmod 777).
hope you guys will enjoy!
-seltar