|
| 1 | +package com.jelly.FarmHelper.utils; |
| 2 | + |
| 3 | +import com.google.gson.JsonElement; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import com.google.gson.JsonParser; |
| 6 | +import io.netty.handler.codec.base64.Base64; |
| 7 | + |
| 8 | +import javax.imageio.ImageIO; |
| 9 | +import java.awt.*; |
| 10 | +import java.awt.image.BufferedImage; |
| 11 | +import java.io.*; |
| 12 | +import java.net.HttpURLConnection; |
| 13 | +import java.net.URL; |
| 14 | +import java.net.URLEncoder; |
| 15 | + |
| 16 | +public class Screenshot { |
| 17 | + public static String takeScreenshot() { |
| 18 | + try { |
| 19 | + Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); |
| 20 | + BufferedImage capture = (new Robot()).createScreenCapture(screenRect); |
| 21 | + String filename = String.valueOf(System.currentTimeMillis()).substring(String.valueOf(System.currentTimeMillis()).length() - 3); |
| 22 | + File temp = File.createTempFile(filename, ".png"); |
| 23 | + ImageIO.write(capture, "png", temp); |
| 24 | + temp.deleteOnExit(); |
| 25 | + return uploadScreenshot(temp); |
| 26 | + } catch (Exception var4) { |
| 27 | + var4.printStackTrace(); |
| 28 | + return null; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public static String uploadScreenshot(File file) { |
| 33 | + String link = ""; |
| 34 | + |
| 35 | + try { |
| 36 | + URL url = new URL("https://api.imgur.com/3/image"); |
| 37 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 38 | + BufferedImage image = ImageIO.read(file); |
| 39 | + ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); |
| 40 | + ImageIO.write(image, "png", byteArray); |
| 41 | + // String dataImage = Base64.encode(byteImage); |
| 42 | + String dataImage = byteArray.toString(); |
| 43 | + String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(dataImage, "UTF-8"); |
| 44 | + conn.setDoOutput(true); |
| 45 | + conn.setDoInput(true); |
| 46 | + conn.setRequestMethod("POST"); |
| 47 | + conn.setRequestProperty("Authorization", "Client-ID 9a47f5fc66bc069"); |
| 48 | + conn.setRequestMethod("POST"); |
| 49 | + conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
| 50 | + conn.connect(); |
| 51 | + StringBuilder stb = new StringBuilder(); |
| 52 | + OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); |
| 53 | + wr.write(data); |
| 54 | + wr.flush(); |
| 55 | + BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| 56 | + |
| 57 | + String line; |
| 58 | + while ((line = rd.readLine()) != null) { |
| 59 | + stb.append(line).append("\n"); |
| 60 | + } |
| 61 | + |
| 62 | + wr.close(); |
| 63 | + rd.close(); |
| 64 | + JsonElement jsonElement = (new JsonParser()).parse(stb.toString()); |
| 65 | + JsonObject jsonObject = jsonElement.getAsJsonObject(); |
| 66 | + link = jsonObject.get("data").getAsJsonObject().get("link").toString(); |
| 67 | + } catch (Exception var15) { |
| 68 | + var15.printStackTrace(); |
| 69 | + } |
| 70 | + |
| 71 | + return link; |
| 72 | + } |
| 73 | +} |
| 74 | + |
0 commit comments