将C#编写的屏幕区域截图工具类代码转换为可运行的Java版本
Java版屏幕区域截图工具类
功能说明
完全对齐原C#代码的核心能力:
- 输入两个坐标点即可截取屏幕指定区域
- 支持按百分比放大输出最终截图
- 全部使用JDK原生API实现,无第三方依赖,不会出现依赖引用错误
- 兼容JDK 8及以上所有版本
完整实现代码
import java.awt.AWTException; import java.awt.Graphics2D; import java.awt.Point; import java.awt.Rectangle; import java.awt.Robot; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageCapture { public static void capture(String fileName, Point firstCoordinate, Point secondCoordinate, int enlargeImageByPercentage) throws AWTException, IOException { // 拼接临时文件路径,对应原C#的Local/Temp目录 String tmpDir = System.getProperty("java.io.tmpdir"); File tmpImgFile = new File(tmpDir, new File(fileName).getName()); String tmpImgPath = tmpImgFile.getAbsolutePath(); // 计算截图区域 int rectX = firstCoordinate.x; int rectY = firstCoordinate.y; int rectWidth = secondCoordinate.x - firstCoordinate.x; int rectHeight = secondCoordinate.y - firstCoordinate.y; Rectangle screenRect = new Rectangle(rectX, rectY, rectWidth, rectHeight); // 截取屏幕区域 Robot robot = new Robot(); BufferedImage bmp = robot.createScreenCapture(screenRect); // 保存临时文件 ImageIO.write(bmp, "jpg", new File(tmpImgPath)); // 处理目标文件 File targetFile = new File(fileName); if (targetFile.exists()) { targetFile.delete(); } if (enlargeImageByPercentage > 0) { // 按比例放大图片 BufferedImage imgToEnlarge = ImageIO.read(new File(tmpImgPath)); int targetWidth = imgToEnlarge.getWidth() * enlargeImageByPercentage; int targetHeight = imgToEnlarge.getHeight() * enlargeImageByPercentage; BufferedImage resizedImg = resizeImage(imgToEnlarge, targetWidth, targetHeight); ImageIO.write(resizedImg, "jpg", targetFile); } else { // 直接复制临时文件到目标路径 java.nio.file.Files.copy(tmpImgFile.toPath(), targetFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); } // 清理临时文件 tmpImgFile.deleteOnExit(); } // 重载方法,默认放大比例为0 public static void capture(String fileName, Point firstCoordinate, Point secondCoordinate) throws AWTException, IOException { capture(fileName, firstCoordinate, secondCoordinate, 0); } private static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) { float sourceWidth = originalImage.getWidth(); float sourceHeight = originalImage.getHeight(); // 计算等比缩放比例,对应原C#的逻辑,避免拉伸变形 float percentW = targetWidth / sourceWidth; float percentH = targetHeight / sourceHeight; float finalPercent = Math.min(percentW, percentH); int destWidth = (int) (sourceWidth * finalPercent); int destHeight = (int) (sourceHeight * finalPercent); BufferedImage resizedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g = resizedImage.createGraphics(); // 对应原C#的HighQualityBicubic插值模式,保证缩放质量 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.drawImage(originalImage, 0, 0, destWidth, destHeight, null); g.dispose(); return resizedImage; } }
使用示例
public class Test { public static void main(String[] args) throws Exception { // 示例:截取屏幕(0,0)到(500,500)的区域,放大2倍后保存到d:/test.jpg Point p1 = new Point(0, 0); Point p2 = new Point(500, 500); ImageCapture.capture("d:/test.jpg", p1, p2, 2); } }
内容的提问来源于stack exchange,提问作者nabila
相关产品推荐
相关产品推荐

