0

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

MRVDOG
  • 1,717
  • 2
  • 13
  • 20
  • I've looked at the image. Its properties state that the size of the image is 0px x 0px and scaled to 800px x 450px. – Michael Feb 19 '13 at 16:59
  • hmm, so that means that the AWT Robot isn't doing anything, because when I take the screenshot, the game is at 800px x 450px, it also explains why the base64 data is so short... I may have to see about trying to implement minecraft's screenshot methods – MRVDOG Feb 19 '13 at 19:19
  • The `Robot` should work. Have you tried to save it as a file? – Michael Feb 19 '13 at 20:28
  • the file is saving correctly, so it must be to do with either base64 encoding it, or sending it to my website then, this is the base64 class I am using http://iharder.sourceforge.net/current/java/base64/ – MRVDOG Feb 19 '13 at 21:07
  • Why aren't you using the `apache` commons? – Michael Feb 19 '13 at 21:18
  • I honestly have no Idea – MRVDOG Feb 19 '13 at 21:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24790/discussion-between-michael-and-vinny-benson) – Michael Feb 19 '13 at 21:46
  • haha, I'm an IDIOT, guess who set screenshot_data in my database to varchar(255) instead of longtext, whoops, its working now, thanks for your help Michael – MRVDOG Mar 02 '13 at 14:32
  • I'm glad it's working :) And to be honest I've didn't do anything that could've helped. – Michael Mar 02 '13 at 19:39
  • I'm aware, but if you hadn't tried helping, I probably wouldn't have figured it out, :D – MRVDOG Apr 05 '13 at 14:20

1 Answers1

1

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...

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437