0

I am taking screenshot of my desktop and i want to know how i would go if i want to send it to php site and then display it?

I have made this and no results about streaming.

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.imageio.ImageIO;
public class Stream{

static public void captureScreen() throws Exception {
    Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    Rectangle screenRectangle = new Rectangle(screenSize);
    Robot robot = new Robot();
    BufferedImage image = robot.createScreenCapture(screenRectangle);
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ImageIO.write(image, "png", buffer);
    byte[] data = buffer.toByteArray();
    try {
        // open a connection to the site
        URL url = new URL("http://futuretechs.eu/stream.php");
        URLConnection con = url.openConnection();
        // activate the output
        con.setDoOutput(true);
        PrintStream ps = new PrintStream(con.getOutputStream());
        // send your parameters to your site
        ps.print("image=" + encodeArray(data));   

        System.out.println(encodeArray(data));

        // we have to get the input stream in order to actually send the request
        con.getInputStream();

        // close the print stream
        ps.close();
    } catch (MalformedURLException e) {
            e.printStackTrace();
    } catch (IOException e) {
            e.printStackTrace();
    }
}

public static void main(String[] args) {
    try{
        System.out.println("[ Stream Started ]");

        while(true){
            Thread.currentThread().sleep(100);
            Stream.captureScreen();
        }

       // System.out.println("[ Stream Ended ]");
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

static String encodeArray(byte[] in) throws IOException {
      StringBuffer out = new StringBuffer();
      out.append(Base64Coder.encode(in, 0, in.length));
      return out.toString();
}
}

How now i would send from java the byte[] to php and play it?

So it would go like this

Java Client program sends to php site the byte[] content and then the php shows it to the user who is at the site?

Thank you!

EDIT: CODE UPDATED

2 Answers2

0

well its better to convert the image bytes into base64 before sending to php when you send to php you can use this function imagecreatefromstring($image_data) to display

display.php

<?php
$image = $_POST['image'];
$data = base64_decode($image);
$im = imagecreatefromstring($data);
if ($im !== false) {
    header('Content-Type: image/png');
    imagepng($im);
    imagedestroy($im);
}
else {
    echo 'An error occurred.';
}
?>

this should work with (PHP 4 >= 4.0.4, PHP 5)

Let me know if it works :)

Edit :

I am not really good with java try the below

As asked java code

try {
    // open a connection to the site
    URL url = new URL("http://www.yourdomain.com/yourphpscript.php");
    URLConnection con = url.openConnection();
    // activate the output
    con.setDoOutput(true);
    PrintStream ps = new PrintStream(con.getOutputStream());
    // send your parameters to your site
    ps.print("image=BASE64_ENCODED_STRING_HERE");   

    // we have to get the input stream in order to actually send the request
    con.getInputStream();

    // close the print stream
    ps.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

or Use HTTP Client in java

HttpClient client = new HttpClient();
PostMethod method = new PostMethod("http://www.yourdomain.com/yourphpscript.php");
method.addParamezter("image","BASE64_ENCODED_STRING"); // or whatever
int statusCode = client.executeMethod(method);
nani
  • 78
  • 7
0

What is that site you wanna upload the screenshot content? Is that site on the internet?

There are different approaches. - You could have a php page which waits for an HTTP-POST request, with the screenshot in the payload, while the site itself has a php-module running on that server and gets invoked by the web-request. - That server probably supports WebDav, then you could upload your screenshot via HTTP-PUT and invoke a php site with HTTP-GET (while sending the filename with your HTTP-GET-Args).

It's hard to tell, if we don't know the php-site, it's API and/or it's behaviour.

Ronald Duck
  • 323
  • 1
  • 11
  • Rented website, in the internet. I am trying to achieve like in twitch, but for start with pictures of screen. The managing is happening through CPanel and It also haves joomla but i am not sure if i will use it. – Stepan Fedotov Jan 25 '15 at 09:56