如何将Picture对象转换为Java中的JPG文件,以便保存?

时间:2016-05-27 23:31:26

标签: java

我有一个程序在我加载到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

1 个答案:

答案 0 :(得分:1)

“图片”类是您的课程为您提供的,而不是内置课程。那说我在我的大学读了一堂java课,他们也给了我一个名为图片的课。试试这个

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