gpt4 book ai didi

java - 使图像 resize() 的代码起作用

转载 作者:行者123 更新时间:2023-12-02 08:38:07 25 4
gpt4 key购买 nike

我的导师向我发送了这段代码,它应该是正在进行的项目的组成部分。问题是,它不起作用。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.Kernel;
import java.awt.image.ConvolveOp;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import javax.swing.*;


public class ImageUtil {

public static void resize(File originalFile, File resizedFile, int newWidth, float quality) throws IOException{

if(quality <0||quality>1){
throw new IllegalArgumentException ("quality has to be between 0 and 1");

}

ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
Image i = ii.getImage();
Image resizedImage = null;

int iWidth = i.getWidth (null);
int iHeight = i.getHeight(null);

if (iWidth > iHeight){
resizedImage = i.getScaledInstance(newWidth,(newWidth*iHeight)/iWidth, Image.SCALE_SMOOTH);
} else {
resizedImage = i.getScaledInstance((newWidth*iWidth)/iHeight,newWidth, Image.SCALE_SMOOTH);
}

Image temp = new ImageIcon (resizedImage).getImage();

BufferedImage bufferedImage = new BufferedImage (temp.getWidth(null), temp.getHeight(null),
BufferedImage.TYPE_INT_RGB);

// copy to a buffered image
Graphics g = bufferedImage.createGraphics();

g.setColor(Color.white);
g.fillRect(0,0,temp.getWidth(null), temp.getHeight(null)); // causea el error de ejecución?
g.drawImage(temp,0,0,null);
g.dispose();

float softenFactor = 0.05f;
float [] softenArray = {0, softenFactor,0, softenFactor, 1-(softenFactor*4), softenFactor,0, softenFactor,0};
Kernel kernel = new Kernel (3,3,softenArray);
ConvolveOp cOp = new ConvolveOp (kernel, ConvolveOp.EDGE_NO_OP,null);
bufferedImage = cOp.filter (bufferedImage,null);

FileOutputStream out = new FileOutputStream(resizedFile);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);

param.setQuality(quality,true);

encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage);



}

public static void main(String args[]) throws IOException {
File originalImage= new File ("/images/goya.jpg");
resize(originalImage,new File("/images/goyanuevotamanio.jpg"),350, 0.2f);
resize(originalImage, new File("/images/goyanuevotamanio.jpg"), 350, 1f);

}

}

他说代码应该能够调整高度和宽度,但他的调整大小方法:

public static void resize(File originalFile, File resizedFile, int newWidth, float quality)

缺少 newHeight 参数。

抛出的 RuntimeException 表示:

Exception in thread "main" java.lang.IllegalArgumentException: **Width (-1) and height (-1) cannot be <= 0**
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:999)
at java.awt.image.BufferedImage.<init>(BufferedImage.java:312)
at ImageUtil.resize(ImageUtil.java:38)
at ImageUtil.main(ImageUtil.java:72)
Java Result: 1

如何更改此设置?

最佳答案

在不分析代码来查看问题所在的情况下,我可以说,如果目的是保持图像的宽高比,则不需要新的高度参数。它始终是宽度的固定比例。

更新:

我已经针对我自己的测试图像运行了代码,它确实按预期工作。当程序找不到您指定的源图像时,很可能会出现您收到的错误。当我指定不存在的源图像时,我收到相同的错误。

尝试对源图像使用绝对路径(包括驱动器号等)。

更新2

在该行后添加以下代码..

ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());

if(ii.getImageLoadStatus() == MediaTracker.COMPLETE)
{
System.out.print("Load image ok\n");
}
else
{
System.out.print("Load image failed\n");
}

这将告诉您程序是否无法加载源图像。

关于java - 使图像 resize() 的代码起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/764794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com