This repository was archived by the owner on Mar 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 387
This repository was archived by the owner on Mar 6, 2018. It is now read-only.
Null pointer exception when emmiting to socket #131
Copy link
Copy link
Open
Description
This code throws an NPE for some reason , although I dont use any null values
`
import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import javax.imageio.ImageIO;
import org.json.JSONException;
import org.json.JSONObject;
import io.socket.*;
import io.socket.IOCallback;
public class Main{ //implements IOCallback {
/*public static byte[] snap(String name) throws HeadlessException, AWTException, IOException{
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "jpg", new File(name));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
}*/
public static String snap() throws IOException {
Rectangle screen = new
Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenCapture = null;
String base64Encoded = "";
try {
screenCapture = new Robot().createScreenCapture(screen);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(screenCapture, "jpg", baos);
baos.flush();
byte[] encodedBytes = Base64.getEncoder().encode(baos.toByteArray());
base64Encoded = new String(encodedBytes);
baos.close();
} catch (AWTException e) {
e.getMessage();
}
return base64Encoded;
}
public static int BasicExample(String img) throws IOException {
SocketIO socket = new SocketIO("http://127.0.0.1:5000/");
// Sends a string to the server.
// Emits an event to the server.
socket.emit(img);
return 3;
}
public static void main(String[] args) throws IOException, HeadlessException, AWTException {
for (int i = 0; i<10; i++){
//String name = "./ScreenShot" + Integer.toString(i) + ".jpg";
//System.out.println(snap());
int w = BasicExample(snap());
}
//SocketIO socket = new SocketIO("http://127.0.0.1:5000/");
}
}`
Metadata
Metadata
Assignees
Labels
No labels