java,调整图片大小

发布于 2024-10-21 20:14:38 字数 119 浏览 2 评论 0原文

我在java中调整图片大小时遇到​​问题!

我从 xml 文档中获取图片,但大多数都是横向模式! 我需要将它们调整大小/转换为纵向模式,以便它们适合我的 GUI 布局。

有没有办法做到这一点?

im having a problem with resizing pictures in java!

i get the pictures from an xml document but most of them are in landscape mode!
i need to have them resized/converted into portrait mode so they fit into my GUI layout.

is there a method for doing this??

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

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

发布评论

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

评论(2

南渊 2024-10-28 20:14:38

您可以将宽度和高度缩放相同的值以获得具有相似分辨率的图像。

但你的问题到底是什么?如何扩展或者什么?

我写这个是为了根据目标宽度和高度将高度和宽度缩放相同的值

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageTest {
    public static BufferedImage resize(int targetWidth, int targetHeight,
            BufferedImage src) {
        double scaleW = (double) targetWidth / (double) src.getWidth() ;
        double scaleH = (double) targetHeight / (double) src.getHeight() ;

        double scale = scaleW < scaleH ? scaleW : scaleH;

        BufferedImage result = new BufferedImage((int) (src.getWidth() * scale),
                (int) (src.getHeight() * scale), BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = result.createGraphics();
        g2d.drawImage(src, 0, 0, result.getWidth(), result.getHeight(), null);
        g2d.dispose();

        return result;
    }

    public static void main(String[] args) throws IOException {
        BufferedImage origin = ImageIO.read(new File(
                "/home/marcos/icons/print.png"));

        File dest = new File("/home/marcos/icons/print2.png");
        dest.createNewFile();
        ImageIO.write(resize(180, 200, origin), "PNG", dest);
    }
}

You can scale width and height by the same value to get the image with a similar resolution.

But what you problem really is? How to scale or what?

I wrote this for scale both height and width by the same value based on the target width and height

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageTest {
    public static BufferedImage resize(int targetWidth, int targetHeight,
            BufferedImage src) {
        double scaleW = (double) targetWidth / (double) src.getWidth() ;
        double scaleH = (double) targetHeight / (double) src.getHeight() ;

        double scale = scaleW < scaleH ? scaleW : scaleH;

        BufferedImage result = new BufferedImage((int) (src.getWidth() * scale),
                (int) (src.getHeight() * scale), BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = result.createGraphics();
        g2d.drawImage(src, 0, 0, result.getWidth(), result.getHeight(), null);
        g2d.dispose();

        return result;
    }

    public static void main(String[] args) throws IOException {
        BufferedImage origin = ImageIO.read(new File(
                "/home/marcos/icons/print.png"));

        File dest = new File("/home/marcos/icons/print2.png");
        dest.createNewFile();
        ImageIO.write(resize(180, 200, origin), "PNG", dest);
    }
}
十雾 2024-10-28 20:14:38

为了将图片从横向模式转换为纵向模式而不旋转,您首先必须裁剪图片。然后,您可能需要缩小图片的尺寸。

假设您的风景图片为 500 x 300 像素,而您希望肖像图片为 150 X 250 像素。

您可以通过裁剪中心将风景图片裁剪为 180 X 300 像素。然后您可以将裁剪后的图片缩小到 150 x 250 像素。

下面是一些用于裁剪图像的 Java 代码:

    int x           = (videoWidth - imageWidth) / 2;
    int y           = (videoHeight - imageHeight) / 2;
    CropImageFilter cropFilter = new CropImageFilter
            (x, y, imageWidth, imageHeight);
    Image croppedImage = toolkit.createImage(new FilteredImageSource
            (image.getSource(), cropFilter));

image 是 java.awt.Image,toolkit 是默认的 java.awt.Toolkit。

In order to convert a picture from landscape mode to portrait mode, without rotating, you first have to crop the picture. Then, you may have to reduce the size of the picture.

Let's say your landscape picture is 500 x 300 pixels, and you want your portrait picture to be 150 X 250 pixels.

You could crop the landscape picture to 180 X 300 pixels by cropping the center. Then you can reduce the cropped picture down to 150 x 250 pixels.

Here's some Java code for cropping an image:

    int x           = (videoWidth - imageWidth) / 2;
    int y           = (videoHeight - imageHeight) / 2;
    CropImageFilter cropFilter = new CropImageFilter
            (x, y, imageWidth, imageHeight);
    Image croppedImage = toolkit.createImage(new FilteredImageSource
            (image.getSource(), cropFilter));

image is a java.awt.Image, and toolkit is the default java.awt.Toolkit.

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