Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote