【问题标题】:want to convert a image generator C# code to JAVA想要将图像生成器 C# 代码转换为 JAVA
【发布时间】:2021-08-09 13:19:02
【问题描述】:

所以我得到了一个用 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;
        }

    }
}

【问题讨论】:

    标签: java image-processing coordinates screen screenshot


    【解决方案1】:

    此类本质上与 C# 变体相同,但不使用临时文件。 main 方法仅作为使用示例。

    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);
        }
    }
    

    【讨论】:

    • 这就是我要找的东西,你能帮我为 resizeImage 方法编写代码吗,或者我们已经在下面几行代码中调整大小 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);
    • 我提取了 resize-method 以匹配 C# 代码并提高可读性。但是,是的,原始答案在您引用的那些行中进行了调整大小操作。
    猜你喜欢
    • 2015-07-15
    • 2014-03-04
    • 2016-06-25
    • 2020-09-02
    • 2016-04-14
    • 2014-05-27
    • 2015-09-27
    • 2016-02-12
    • 2014-09-16
    相关资源
    最近更新 更多