Created
January 8, 2020 08:49
-
-
Save keepingcoding/dcc5607e638063427eabfcb789df9da5 to your computer and use it in GitHub Desktop.
图片工具类
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.codec.binary.Base64; | |
import javax.imageio.ImageIO; | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
public class ImageUtils { | |
private ImageUtils() { | |
} | |
/** | |
* 截屏并保存到指定文件 | |
* | |
* @param file | |
* @throws AWTException | |
* @throws IOException | |
*/ | |
public static void screenshot(File file, String fileType) throws AWTException, IOException { | |
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); | |
Robot robot = new Robot(); | |
BufferedImage screenshot = robot.createScreenCapture(new Rectangle(dimension)); | |
ImageIO.write(screenshot, fileType, file); | |
} | |
/** | |
* 将图片流转换成base64字符串 | |
* | |
* @param inputStream | |
* @return | |
* @throws IOException | |
*/ | |
public static String covertToBase64(InputStream inputStream) throws IOException { | |
byte[] data = new byte[inputStream.available()]; | |
inputStream.read(data); | |
Base64 base64 = new Base64(); | |
return base64.encodeAsString(data); | |
} | |
/** | |
* 解析将base64字符串 | |
* | |
* @param base64Str | |
* @return | |
*/ | |
public static byte[] decodeBase64(String base64Str) { | |
Base64 base64 = new Base64(); | |
return base64.decode(base64Str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment