在java中调整图像大小-我需要更改什么?
我只是想知道如何在java中调整图像的大小? 这是一个任务,我必须找到一个图像,然后将其保存为.png文件,该文件的分辨率为原始图像的一半。 这是我到目前为止的代码在java中调整图像大小-我需要更改什么?,java,image,resize,bufferedimage,Java,Image,Resize,Bufferedimage,我只是想知道如何在java中调整图像的大小? 这是一个任务,我必须找到一个图像,然后将其保存为.png文件,该文件的分辨率为原始图像的一半。 这是我到目前为止的代码 enter code here import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class saveimage { pub
enter code here
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class saveimage {
public static void main(String[] args) // IOException
{
String sourceLocation;
sourceLocation = (args[0]);
int width = 963;
int height = 640;
int halfwidth = width / 2;
int halfheight = height / 2;
BufferedImage image1 = null;
BufferedImage imagehalf = null;
File readfile = null;
try {
readfile = new File(sourceLocation);
image1 = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
image1 = ImageIO.read(readfile);
imagehalf = new BufferedImage(halfwidth, halfheight,
BufferedImage.TYPE_INT_ARGB);
imagehalf = ImageIO.read(readfile);
System.out.println("reading complete");
}
catch (IOException e) {
System.out.println("Error: " + e);
}
try {
readfile = new File("LOCATION OF FILE");
ImageIO.write(image1, "png", readfile);
System.out.println("Writing complete");
} catch (IOException fail1) {
System.out.println("Error:" + fail1);
}
try {
readfile = new File "LOCATION OF OUTPUT");
ImageIO.write(imagehalf, "png", readfile);
System.out.println("writing half is complete");
} catch (IOException fail2) {
System.out.println("Error:" + fail2);
}
}
}
正如你所看到的,我刚开始时将整数值减半,因为我认为它会将输出大小减半,但它没有……我有什么做错了吗
任务的下一部分是我需要平铺图像,但我只是一步一个脚印:)
提前感谢AFAIK
imagehalf=ImageIO.read(readfile)
只需读取图像文件并创建原始大小的buffereImage
。基本上,您要做的是创建一个新的BufferedImage
,然后用一个从文件中读取的文件替换它,这是行不通的
你需要做的是:读取原始图像,创建一个大小为一半的buffereImage
,然后将原始图像绘制成大小为一半的图像
使用getGraphics()
方法,并在返回的Graphics
对象上调用drawImage(…)
请注意,您可以使用
BufferedImage#getScaledInstance(…)
,但您可能希望开始使用图形
对象,以便为将来的作业做好准备。他们可以随意使用我在下面发布的代码:
public ImageIcon resizeImage(ImageIcon imageIcon, int width, int height, boolean max)
{
Image image = imageIcon.getImage();
Image newimg = image.getScaledInstance(-1, height, java.awt.Image.SCALE_SMOOTH);
int width1 = newimg.getWidth(null);
if ((max && width1 > width) || (!max && width1 < width))
newimg = image.getScaledInstance(width, -1, java.awt.Image.SCALE_SMOOTH);
return new ImageIcon(newimg);
}
public-ImageIcon-resizeImage(ImageIcon-ImageIcon,int-width,int-height,boolean-max)
{
Image=imageIcon.getImage();
Image newimg=Image.getScaledInstance(-1,height,java.awt.Image.SCALE\u SMOOTH);
int width1=newimg.getWidth(null);
如果((最大和宽度1>宽度)| |(!最大和宽度1<宽度))
newimg=image.getScaledInstance(宽度-1,java.awt.image.SCALE\u平滑);
返回新的图像图标(newimg);
}
实际上,我不知道布尔最大值是什么,我在互联网上的某个地方找到了它。您需要为resizedImage获取一个图形上下文,然后使用所需的尺寸将原始图像绘制到其中。根据它的外观,您可能还需要查看Java的渲染提示
BufferedImage imageHalf = new BufferedImage(halfwidth, halfheight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = imageHalf.createGraphics();
g.drawImage(fullImage, 0, 0, halfwidth, halfheight, null);
g.dispose();
我将使用什么代码和哪个部分来完成此操作??第三次尝试??