想要将图像生成器 C# 代码转换为 JAVA
want to convert a image generator C# code to JAVA
所以我得到了一个 Class 及其用 C# 编写的方法
基本上它所做的是通过 x 和 y 坐标以及高度、宽度截取屏幕的一部分
我想将此 c# 代码转换为 JAVA 以便我可以在我的桌面 UI 自动化项目中使用它
所以这是一个根据屏幕的“X”和“Y”坐标拍摄图像的实用程序
请注意:我已经尝试过在线转换器,它生成的代码显示库包错误
代码如下:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace myClass
{
class ImageCapture
{
public static void Capture(string fileName, Point firstCoordinate, Point secondCoordinate, int enlargeImageByPercentage=0)
{
var tmpImg = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).Replace("Roaming", "Local") + "\temp", Path.GetFileName(fileName));
Rectangle rect = new Rectangle(firstCoordinate.X, firstCoordinate.Y, secondCoordinate.X - firstCoordinate.X, secondCoordinate.Y - firstCoordinate.Y);
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
bmp.Save(tmpImg, ImageFormat.Jpeg);
g.Dispose();
bmp.Dispose();
if (File.Exists(fileName)) File.Delete(fileName);
if (enlargeImageByPercentage != 0)
{
Image imgToEnlarge = Image.FromFile(tmpImg);
Image resizedImg = resizeImage(tmpImg, new Size(imgToEnlarge.Size.Width * enlargeImageByPercentage, imgToEnlarge.Size.Height * enlargeImageByPercentage));
resizedImg.Save(fileName, ImageFormat.Jpeg);
}
else
{
File.Copy(tmpImg, fileName, true);
}
}
private static Image resizeImage(string imgToResize, Size size)
{
Image _img = Image.FromFile(imgToResize);
//Get the image current width
int sourceWidth = _img.Width;
//Get the image current height
int sourceHeight = _img.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
//Calulate width with new desired size
nPercentW = ((float)size.Width / (float)sourceWidth);
//Calculate height with new desired size
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
//New Width
int destWidth = (int)(sourceWidth * nPercent);
//New Height
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw image with new width and height
g.DrawImage(_img, 0, 0, destWidth, destHeight);
return b;
}
}
}
此 class 本质上与 C# 变体相同,但不使用临时文件。主要方法仅作为用法示例包含在内。
import java.awt.AWTException;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Robot;
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 {
Rectangle rectangle = new Rectangle(firstCoordinate);
rectangle.add(secondCoordinate);
BufferedImage image = new Robot().createScreenCapture(rectangle);
BufferedImage resizedImage = resizeImage(image, enlargeImageByPercentage);
ImageIO.write(resizedImage, "JPEG", new File(fileName));
}
public static BufferedImage resizeImage(BufferedImage image, int enlargeImageByPercentage) {
BufferedImage resizedImage = new BufferedImage(image.getWidth()*(100+enlargeImageByPercentage)/100, image.getHeight()*(100+enlargeImageByPercentage)/100, image.getType());
Graphics2D graphics = (Graphics2D) resizedImage.getGraphics();
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.drawImage(image, 0, 0, resizedImage.getWidth(), resizedImage.getHeight(), null);
return resizedImage;
}
public static void main(String[] args) throws Exception {
ImageCapture.capture("screenshot.jpg", new Point(100, 100), new Point(200,150), 250);
}
}
所以我得到了一个 Class 及其用 C# 编写的方法 基本上它所做的是通过 x 和 y 坐标以及高度、宽度截取屏幕的一部分 我想将此 c# 代码转换为 JAVA 以便我可以在我的桌面 UI 自动化项目中使用它 所以这是一个根据屏幕的“X”和“Y”坐标拍摄图像的实用程序 请注意:我已经尝试过在线转换器,它生成的代码显示库包错误
代码如下:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace myClass
{
class ImageCapture
{
public static void Capture(string fileName, Point firstCoordinate, Point secondCoordinate, int enlargeImageByPercentage=0)
{
var tmpImg = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).Replace("Roaming", "Local") + "\temp", Path.GetFileName(fileName));
Rectangle rect = new Rectangle(firstCoordinate.X, firstCoordinate.Y, secondCoordinate.X - firstCoordinate.X, secondCoordinate.Y - firstCoordinate.Y);
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
bmp.Save(tmpImg, ImageFormat.Jpeg);
g.Dispose();
bmp.Dispose();
if (File.Exists(fileName)) File.Delete(fileName);
if (enlargeImageByPercentage != 0)
{
Image imgToEnlarge = Image.FromFile(tmpImg);
Image resizedImg = resizeImage(tmpImg, new Size(imgToEnlarge.Size.Width * enlargeImageByPercentage, imgToEnlarge.Size.Height * enlargeImageByPercentage));
resizedImg.Save(fileName, ImageFormat.Jpeg);
}
else
{
File.Copy(tmpImg, fileName, true);
}
}
private static Image resizeImage(string imgToResize, Size size)
{
Image _img = Image.FromFile(imgToResize);
//Get the image current width
int sourceWidth = _img.Width;
//Get the image current height
int sourceHeight = _img.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
//Calulate width with new desired size
nPercentW = ((float)size.Width / (float)sourceWidth);
//Calculate height with new desired size
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
//New Width
int destWidth = (int)(sourceWidth * nPercent);
//New Height
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw image with new width and height
g.DrawImage(_img, 0, 0, destWidth, destHeight);
return b;
}
}
}
此 class 本质上与 C# 变体相同,但不使用临时文件。主要方法仅作为用法示例包含在内。
import java.awt.AWTException;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Robot;
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 {
Rectangle rectangle = new Rectangle(firstCoordinate);
rectangle.add(secondCoordinate);
BufferedImage image = new Robot().createScreenCapture(rectangle);
BufferedImage resizedImage = resizeImage(image, enlargeImageByPercentage);
ImageIO.write(resizedImage, "JPEG", new File(fileName));
}
public static BufferedImage resizeImage(BufferedImage image, int enlargeImageByPercentage) {
BufferedImage resizedImage = new BufferedImage(image.getWidth()*(100+enlargeImageByPercentage)/100, image.getHeight()*(100+enlargeImageByPercentage)/100, image.getType());
Graphics2D graphics = (Graphics2D) resizedImage.getGraphics();
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.drawImage(image, 0, 0, resizedImage.getWidth(), resizedImage.getHeight(), null);
return resizedImage;
}
public static void main(String[] args) throws Exception {
ImageCapture.capture("screenshot.jpg", new Point(100, 100), new Point(200,150), 250);
}
}