So I got one Class and its methods written in C# basically what it does is it takes a screenshot of a part of the screen by x and y coordinates and height, width I want to convert this c# code to JAVA so that I can use it in my Desktop UI automation project so this is a utility which takes image based on "X" and "Y" coordinates of the screen please note: I already tried online converters and the code which it generates shows library package errors
Below is the code:
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;
}
}
}
This class essentially does the same as the C# variant, without using temp files. The main method is only included as a usage example.
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);
}
}