编程语言
首页 > 编程语言> > 调整Java图像大小

调整Java图像大小

作者:互联网

我在调整图像大小时遇到​​了一个怪异的问题,无法弄清楚我在做什么错.我读过很多帖子,它们的基本代码与我相同:

(我使用Java库Scalr)

File image = new File("myimage.png");
File smallImage = new File("myimage_s");
try {
    BufferedImage bufimage = ImageIO.read(image);

    BufferedImage bISmallImage = Scalr.resize(bufimage, 30); // after this line my dimensions in bISmallImage are correct!
    ImageIO.write(bISmallImage, "png", smallImage); // but my smallImage has the same dimension as the original foto
} catch (Exception e) {}

有人可以告诉我我在做什么错吗?

解决方法:

我看不到您的代码有什么问题.

我将其放入针对Java SE 7的Eclipse中的快速测试项目,并在Windows 7 Pro 64位上使用imgscalr 4.2

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

import javax.imageio.ImageIO;

import org.imgscalr.Scalr;

public class ScalrTest {

    public static void main(String[] args) {
        File image = new File("myimage.png");
        File smallImage = new File("myimage_s.png"); // FORNOW: added the file extension just to check the result a bit more easily
        // FORNOW: added print statements just to be doubly sure where we're reading from and writing to
        System.out.println(image.getAbsolutePath());
        System.out.println(smallImage.getAbsolutePath());
        try {
            BufferedImage bufimage = ImageIO.read(image);

            BufferedImage bISmallImage = Scalr.resize(bufimage, 30); // after this line my dimensions in bISmallImage are correct!
            ImageIO.write(bISmallImage, "png", smallImage); // but my smallImage has the same dimension as the original foto
        } catch (Exception e) {
            System.out.println(e.getMessage()); // FORNOW: added just to be sure
        }
    }

}

随着以下myimage.png …

…,它产生了以下myimage_s.png:

也许有一个环境问题使您的代码受了束缚,但是想到的可能性将带来明显的错误.

标签:image,image-resizing,image-processing,java
来源: https://codeday.me/bug/20191121/2052370.html