Reputation: 1717
I am trying to take a screenshot in minecraft (binded to F2), convert it to base64, then send it to my website, but none of my screenshots are returning an image, but the dimensions are there, here is an example http://minebook.co.uk/screenshot/48462846
Rectangle screenRectangle = new Rectangle(width, height);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ByteArrayOutputStream os = new ByteArrayOutputStream();
OutputStream b64 = new Base64.OutputStream(os);
if( ImageIO.write(image, "png", b64) ) {
String base64String = os.toString("UTF-8");
// Send screenshot Base 64 to website
String postData = "account=" + ModLoader.getMinecraftInstance().thePlayer.username + "&screenshot=" + base64String;
URL url = new URL("http://minebook.co.uk/ingame/screenshot");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
//write parameters
writer.write(postData);
writer.flush();
// Get the response
StringBuffer answer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
answer.append(line);
}
writer.close();
reader.close();
ModLoader.getMinecraftInstance().thePlayer.sendChatToPlayer(answer.toString());
}
Any assistance will be greatly appreciated, and if you require more information let me know
Vinny
Upvotes: 0
Views: 2860
Reputation: 62411
Best Screen Capture Program using Robot Class:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.imageio.ImageIO;
public class JavaScreenCaptureUtil {
public static void main(String args[]) throws Exception {
/**
* This class (Robot.java) is used to generate native system input events for the
* purposes of test automation, self-running demos, and other
* applications where control of the mouse and keyboard is needed.
* The primary purpose of Robot is to facilitate automated testing
* of Java platform implementations.
*/
Robot robot = new Robot();
/**
* Get the current screen dimensions.
*/
Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
int width = (int) d.getWidth();
int height = (int) d.getHeight();
/**
* Delay the robot for 5 seconds (5000 ms) allowing you to switch to proper
* screen/window whose screenshot is to be taken.
*
* You can change the delay time as required.
*/
robot.delay(5000);
/**
* Create a screen capture of the active window and then create a buffered image
* to be saved to disk.
*/
Image image = robot.createScreenCapture(new Rectangle(0, 0, width,
height));
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
/**
* Filename where to save the file to.
* I am appending formatted timestamp to the filename.
*/
String fileNameToSaveTo = "ScreenCaptured_"
+ createTimeStampStr() + ".jpg";
/**
* Write the captured image to a file.
* I am using PNG format. You can choose PNG, JPG, GIF.
*/
writeImage(bi, fileNameToSaveTo, "PNG");
System.out.println("Screen Captured Successfully and Saved to :\n In Your Folder with Name :- "+fileNameToSaveTo);
}
/**
* This method writes a buffered image to a file
*
* @param img -- > BufferedImage
* @param fileLocation --> e.g. "C:/testImage.jpg"
* @param extension --> e.g. "jpg","gif","png"
*/
public static void writeImage(BufferedImage img, String fileLocation,
String extension) {
try {
BufferedImage bi = img;
File outputfile = new File(fileLocation);
ImageIO.write(bi, extension, outputfile);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* @return String representation of timestamp
* in the format of yyyyMMdd_hhmmss (e.g. 20100426_111612)
* @throws Exception
*/
public static String createTimeStampStr() throws Exception {
Calendar mycalendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_hhmmss");
String timeStamp = formatter.format(mycalendar.getTime());
return timeStamp;
}
}
May this one is helpful to you:
Thanks...
Upvotes: 1