sjgandhi2312 sjgandhi2312 - 6 months ago 13
Java Question

How do I turn a Picture object into a JPG file in Java so I can save it?

I have a program that runs edge detection on pictures that I load into Java. How can I take the processed image that Java returns, turn it into a JPG file, and save it?

At the moment, the program will turn each image into a Picture object. The edge detection works on that Picture object, and then returns the processed image. I have the main class below, and can upload the class that defines the Picture object if requested. I would like to be able to take the "swan" object here, turn it into a JPG file, and then save it.

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();

}
}


Source code for Picture:

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

Answer

the "picture" class is something that your class provided you, its not a built in class. that said i took a class in java at my college and they also gave me a class titled picture. try this

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