捕获屏幕截图并将其转换为 base64 字符串
Capture a screenshot and convert it into a base64 String
发布此问题的目的是帮助电枢编码人员和所有人摆脱以下问题(我从互联网上找到了一些针对以下问题的误导性答案)
- 通过 Java 机器人
捕获桌面图像
- 图片convert/encode到base64字符串
答案为本人发布,保证100%正常工作
答案在这里
您需要以下 java 导入
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
方法如下
public final String takeScreenshot() {
String base64 = "";
try {
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture;
capture = new Robot().createScreenCapture(screenRect);
BufferedImage bufferedThumbnail = new BufferedImage(capture.getWidth(null), capture.getHeight(null),
BufferedImage.TYPE_INT_RGB);
bufferedThumbnail.getGraphics().drawImage(capture, 0, 0, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedThumbnail, "png", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
byte[] encodedArr = Base64.encodeBase64(imageInByte);
// base64 = encodedArr.toString();
base64 = new String(encodedArr);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error While Getting Screen Shot" + e.getLocalizedMessage());
}
return base64;
}
在我看来没有必要创建 BufferedImage
保持它像 :
public String captureToBase64() {
Rectangle screenSize = new
Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenCapture = null;
String base64Encoded = "";
try {
screenCapture = new Robot().createScreenCapture(screenSize);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(screenCapture, "jpg", baos);
baos.flush();
byte[] encodeBase64 = Base64.encodeBase64(baos.toByteArray());
base64Encoded = new String(encodeBase64);
baos.close();
} catch (AWTException e) {
e.getMessage();
}
return base64Encoded;
}
发布此问题的目的是帮助电枢编码人员和所有人摆脱以下问题(我从互联网上找到了一些针对以下问题的误导性答案)
- 通过 Java 机器人 捕获桌面图像
- 图片convert/encode到base64字符串
答案为本人发布,保证100%正常工作
答案在这里
您需要以下 java 导入
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
方法如下
public final String takeScreenshot() {
String base64 = "";
try {
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture;
capture = new Robot().createScreenCapture(screenRect);
BufferedImage bufferedThumbnail = new BufferedImage(capture.getWidth(null), capture.getHeight(null),
BufferedImage.TYPE_INT_RGB);
bufferedThumbnail.getGraphics().drawImage(capture, 0, 0, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedThumbnail, "png", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
byte[] encodedArr = Base64.encodeBase64(imageInByte);
// base64 = encodedArr.toString();
base64 = new String(encodedArr);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error While Getting Screen Shot" + e.getLocalizedMessage());
}
return base64;
}
在我看来没有必要创建 BufferedImage
保持它像 :
public String captureToBase64() {
Rectangle screenSize = new
Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenCapture = null;
String base64Encoded = "";
try {
screenCapture = new Robot().createScreenCapture(screenSize);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(screenCapture, "jpg", baos);
baos.flush();
byte[] encodeBase64 = Base64.encodeBase64(baos.toByteArray());
base64Encoded = new String(encodeBase64);
baos.close();
} catch (AWTException e) {
e.getMessage();
}
return base64Encoded;
}