So this method in java... // program 16 public void grascale() { Pixel[] pixelAr
ID: 3615967 • Letter: S
Question
So this method in java...// program 16
public void grascale()
{
Pixel[] pixelArray = this.getPixels();
Pixel pixel = null;
int intensity = 0;
for (int i = 0; i < pixelArray.length;i++)
{
pixel = pixelArray[i];
intensity = (int) ((pixel.getRed() +pixel.getGreen() +
pixel.getBlue()) / 3);
pixel.setColor(new Color(intensity,intensity, intensity));
}
}
Running as..
public static void main(String[] args)
{
String pic = "C:/Users/noman/Desktop/Dr.Java2/mediasources/pictures/caterpillar.jpg";
Picture p = new Picture(pic);
p.grascale();
p.show();
p.repaint();
}
How would I save the actual gray scaled image as a JPEG image?
Explanation / Answer
Here's a good example: http://www.exampledepot.com/egs/javax.imageio/Graphic2File.htmlimport java.io.*;import javax.imageio.*;import java.awt.image.*;public class FileOperations { public static BufferedImage readImageFromFile(File file) throws IOException { return ImageIO.read(file); } public static void writeImageToJPG (File file,BufferedImage bufferedImage) throws IOException { ImageIO.write(bufferedImage,"jpg",file); }}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.