Java 中图像的放大和缩小

发布于 2025-01-06 20:53:41 字数 357 浏览 0 评论 0原文

我认为这个问题是不言自明的。我想使用 JSlider 实现一个简单的缩放功能,例如在 Windows Live 照片库中。

我在网上快速浏览了一下,但是当我将其复制到 Eclipse 中时,我尝试使用的所有代码似乎都有错误。我也不想使用第三方库,因为该应用程序可能以公司名称出售。另外,我开始意识到可能需要一些安全预防措施来防止错误,但我不知道这些是什么。

因此,如果有人能为我提供一些用于放大和缩小图像的 Java 代码,我将不胜感激。

PS 我计划将图像用作 JLabel 内的 ImageIcon ,它将添加到 JScrollPane 中。

I think the question is quite self-explanatory. I want to implement a simple zoom function using a JSlider like in Windows Live Photo Gallery for instance.

I've had a quick look online but all the code I've tried to use appears to have errors when I copy it into Eclipse. I don't really want to use a third-party library either as the application may be sold under a company name. Plus, I'm beginning to realise that there may be some safety precautions required in order to prevent errors, but I do not know what these will be.

So, if anybody can provide me with some Java code to zoom in and out of images it would be greatly appreciated.

P.S. I plan to use the Image as an ImageIcon inside of a JLabel which will be added to a JScrollPane.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失眠症患者 2025-01-13 20:53:41

您可以通过在原始图像上使用比例变换轻松实现此目的。
假设当前图像宽度 newImageWidth、当前图像高度 newImageHeight 以及当前缩放级别 zoomLevel,您可以执行以下操作

int newImageWidth = imageWidth * zoomLevel;
int newImageHeight = imageHeight * zoomLevel;
BufferedImage resizedImage = new BufferedImage(newImageWidth , newImageHeight, imageType);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, newImageWidth , newImageHeight , null);
g.dispose();

: ,将显示区域中的原始图像 originalImage 替换为 resizedImage

You can easily achieve this by using scale transforms on the original image.
Assuming your the current image width newImageWidth, and the current image height newImageHeight, and the current zoom level zoomLevel, you can do the following:

int newImageWidth = imageWidth * zoomLevel;
int newImageHeight = imageHeight * zoomLevel;
BufferedImage resizedImage = new BufferedImage(newImageWidth , newImageHeight, imageType);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, newImageWidth , newImageHeight , null);
g.dispose();

Now, replace the original image, originalImage, in your display area by resizedImage.

乜一 2025-01-13 20:53:41

您还可以按如下方式使用

public class ImageLabel extends JLabel{
    Image image;
    int width, height;

    public void paint(Graphics g) {
        int x, y;
        //this is to center the image
        x = (this.getWidth() - width) < 0 ? 0 : (this.getWidth() - width);
        y = (this.getHeight() - width) < 0 ? 0 : (this.getHeight() - width);

        g.drawImage(image, x, y, width, height, null);
    }

    public void setDimensions(int width, int height) {
        this.height = height;
        this.width = width;

        image = image.getScaledInstance(width, height, Image.SCALE_FAST);
        Container parent = this.getParent();
        if (parent != null) {
            parent.repaint();
        }
        this.repaint();
    }
}

然后您可以将其放入框架中,并使用使用缩放系数进行缩放的方法,对此我使用了百分比值。

public void zoomImage(int zoomLevel ){
    int newWidth, newHeight, oldWidth, oldHeight;
    ImagePreview ip = (ImagePreview) jLabel1;
    oldWidth = ip.getImage().getWidth(null);
    oldHeight = ip.getImage().getHeight(null);

    newWidth = oldWidth * zoomLevel/100;
    newHeight = oldHeight * zoomLevel/100;

    ip.setDimensions(newHeight, newWidth);
}

You can also use it as follows
:

public class ImageLabel extends JLabel{
    Image image;
    int width, height;

    public void paint(Graphics g) {
        int x, y;
        //this is to center the image
        x = (this.getWidth() - width) < 0 ? 0 : (this.getWidth() - width);
        y = (this.getHeight() - width) < 0 ? 0 : (this.getHeight() - width);

        g.drawImage(image, x, y, width, height, null);
    }

    public void setDimensions(int width, int height) {
        this.height = height;
        this.width = width;

        image = image.getScaledInstance(width, height, Image.SCALE_FAST);
        Container parent = this.getParent();
        if (parent != null) {
            parent.repaint();
        }
        this.repaint();
    }
}

Then you can put it to your frame and with the method that zooms with a zooming factor, for which I used percent values.

public void zoomImage(int zoomLevel ){
    int newWidth, newHeight, oldWidth, oldHeight;
    ImagePreview ip = (ImagePreview) jLabel1;
    oldWidth = ip.getImage().getWidth(null);
    oldHeight = ip.getImage().getHeight(null);

    newWidth = oldWidth * zoomLevel/100;
    newHeight = oldHeight * zoomLevel/100;

    ip.setDimensions(newHeight, newWidth);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文