Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i dont know where my code did wrong, the problem is not save . pls help

What I have tried:

Java
import java.io.*;

public class GrayScaleConverter {
	
	public ImageResource makeGray(ImageResource inImage) {
		ImageResource outImage = new ImageResource(inImage.getWidth(), inImage.getHeight());
		
		for (Pixel pixel: outImage.pixels()) {
			Pixel inPixel = inImage.getPixel(pixel.getX(), pixel.getY());
			int average = (inPixel.getRed() + inPixel.getBlue() + inPixel.getGreen())/3;
			pixel.setRed(average);
			pixel.setGreen(average);
			pixel.setBlue(average);
		}
		
		return outImage;
	}

	public void selectAndConvert () {
		DirectoryResource dr = new DirectoryResource();
		for (File f : dr.selectedFiles()) {
			ImageResource inImage = new ImageResource(f);
			ImageResource gray = makeGray(inImage);
			String fname = gray.getFileName();
			String newName = "copy-" + fname;
			gray.setFileName(newName);
			gray.draw();
			gray.save();
		}
	}
}
Posted
Updated 25-Apr-18 1:10am
v3
Comments
wseng 24-Apr-18 23:34pm    
not save to where?
BrantleyOng 25-Apr-18 0:04am    
hw to let the return output to save in my local file?

1 solution

Your ImageResource returned from makeGray() does not have a file name because it has been created in memory and setFileName() has not been called for it.

Use the name of the input file instead:
Java
gray.setFileName(f.getParent() + f.pathSeperator + "copy-" + f.GetName());
If you need to save the file in a different directory use that instead of f.getParent().
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900