gpt4 book ai didi

java - 如何在 Java 中将 Picture 对象转换为 JPG 文件以便保存?

转载 作者:行者123 更新时间:2023-12-01 09:52:57 24 4
gpt4 key购买 nike

我有一个程序,可以对加载到 Java 中的图片运行边缘检测。如何将Java返回的处理后的图像转为JPG文件并保存?

此时,程序会将每个图像转换为 Picture 对象。边缘检测作用于该 Picture 对象,然后返回处理后的图像。我有下面的主类,如果需要,可以上传定义 Picture 对象的类。我希望能够在这里获取“天鹅”对象,将其转换为 JPG 文件,然后保存。

import java.util.Scanner;

public class PictureTester
{
private static Scanner input = new Scanner(System.in);

public static void testEdgeDetection2()
{
Picture swan = new Picture("clone_1_water-timelapse_t0.jpg");
swan.edgeDetection2(10);
swan.explore();
}

public static void main(String[] args)
{

testEdgeDetection2();

}
}

图片源代码:

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.text.*;
import java.util.*;
import java.util.List; // resolves problem with java.awt.List and java.util.List

/**
* A class that represents a picture. This class inherits from
* SimplePicture and allows the student to add functionality to
* the Picture class.
*
*/
public class Picture extends SimplePicture
{
///////////////////// constructors //////////////////////////////////

/**
* Constructor that takes no arguments
*/
public Picture ()
{
/* not needed but use it to show students the implicit call to super()
* child constructors always call a parent constructor
*/
super();
}

/**
* Constructor that takes a file name and creates the picture
* @param fileName the name of the file to create the picture from
*/
public Picture(String fileName)
{
// let the parent class handle this fileName
super(fileName);
}

/**
* Constructor that takes the width and height
* @param height the height of the desired picture
* @param width the width of the desired picture
*/
public Picture(int height, int width)
{
// let the parent class handle this width and height
super(width,height);
}

/**
* Constructor that takes a picture and creates a
* copy of that picture
* @param copyPicture the picture to copy
*/
public Picture(Picture copyPicture)
{
// let the parent class do the copy
super(copyPicture);
}

/**
* Constructor that takes a buffered image
* @param image the buffered image to use
*/
public Picture(BufferedImage image)
{
super(image);
}

////////////////////// methods ///////////////////////////////////////

/**
* Method to return a string with information about this picture.
* @return a string with information about the picture such as fileName,
* height and width.
*/
public String toString()
{
String output = "Picture, filename " + getFileName() +
" height " + getHeight()
+ " width " + getWidth();
return output;

}

public void edgeDetection2(int edgeDist)
{
Pixel topPixel = null;
Pixel bottomPixel = null;
Pixel[][] pixels = this.getPixels2D();
Color bottomColor = null;
for (int row = 0; row < pixels.length-1; row++)
{
for (int col = 0;
col < pixels[0].length; col++)
{
topPixel = pixels[row][col];
bottomPixel = pixels[row+1][col];
bottomColor = bottomPixel.getColor();
if (topPixel.colorDistance(bottomColor) >
edgeDist)
topPixel.setColor(Color.BLACK);
else
topPixel.setColor(Color.WHITE);
}
}
}

/* Main method for testing - each class in Java can have a main
* method
*/
public static void main(String[] args)
{
Picture beach = new Picture("beach.jpg");
beach.explore();
}

} // this } is the end of class Picture, put all new methods before this

最佳答案

“图片”类是您的类为您提供的东西,它不是内置类。也就是说,我在大学上了一门 Java 类(class),他们还给了我一门名为“图片”的类(class)。试试这个

Picture myPicture = new Picture("input-file-name.jpg");
myPicture.write("name-for-new-file.jpg");

关于java - 如何在 Java 中将 Picture 对象转换为 JPG 文件以便保存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37493687/

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