Question

Sorry if this bugs anyone, but I'm unsure of the other questions I perused searching for an answer. My issue is I'm creating a class that simply adds Gaussian Noise to the File type passed in. Once I've added the noise, I'd like to return the result to a MainScreen class I have created. The problem is, MainClass uses File in its JPanel.

My question is how do I convert the BufferedImage type into a File type return for my Mainscreen class without causing errors for use with Swing's Jpanel?

Here's the code, if you'd like to see it for clarity. (I'm okay with this being closed if it's the wrong way to ask; I'm new to all of this and I'll be zen about this)

import java.io.*;
import java.util.*;
import java.lang.Math;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class NoiseGenerator {
    Image image;
    BufferedImage buffered;
    Random r;

    int width;
    int height;
    int index;

    //The part of the code I'm referring to in the question
    //addNoiseToImage takes in a File.  
    public BufferedImage addNoiseToImage(File imageToBeChanged, int standardDeviation){
        try {
        image = ImageIO.read(imageToBeChanged);
        } catch (IOException e) {
        // TODO Auto-generated catch block
            e.printStackTrace();
        }

        buffered = (BufferedImage) image;
        width = image.getWidth(null);
        height = image.getHeight(null);
        index = 0;

        double noiseHolder;
        int noiseValues[] = new int[100];
        int delay = 0;
        int check = 0;

        do {
            noiseHolder = r.nextGaussian() * standardDeviation;
            check = (int) Math.round(noiseHolder);
            if (check > 0) {
                noiseValues[delay] = (int) noiseHolder;
                delay++;
            }
        } while (delay <= 100);

        int j;
        int i;
        for (j=0; j<height; j++) {
            for(i=0;i<width;i++) {  
                index = (int) (Math.random() * (99 - 0)) + 0; 
                buffered.setRGB(j,i,noiseValues[index]);
            }
        }
        return buffered;  //I just have it returning a BufferedImage type to stop the error message; I'd prefer it to be a File type.
    }
}
Was it helpful?

Solution

The Image I/O class provides a simple way to save images in a variety of image formats in the following example:

static boolean ImageIO.write(RenderedImage im, 
                             String formatName,
                             File output)  throws IOException

Note: The BufferedImage class implements the RenderedImage interface. .

The formatName parameter selects the image format in which to save the BufferedImage.

try {
    // retrieve image
    BufferedImage bi = getMyImage();
    File outputfile = new File("saved.png");
    ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
    ...
}

for more information please look at this example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top