0

minecraft(F2にバインド)でスクリーンショットを撮り、base64に変換してから、ウェブサイトに送信しようとしていますが、どのスクリーンショットも画像を返していませんが、サイズはあります。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());
}

どんな援助でも大歓迎です、そしてあなたがより多くの情報を必要とするならば、私に知らせてください

ビニー

4

1 に答える 1

1

ロボット クラスを使用したベスト スクリーン キャプチャ プログラム:

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;

    } 
}

これがあなたの役に立ちますように:

ありがとう...

于 2013-09-21T06:53:10.640 に答える