如何创建彩色照片转换为灰度并由java保存 [英] How to create a color photo convert to grayscale and save by java
本文介绍了如何创建彩色照片转换为灰度并由java保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道我的代码在哪里做错了,问题不在于保存。请帮助
我的尝试:
i dont know where my code did wrong, the problem is not save . pls help
What I have tried:
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();
}
}
}
推荐答案
您的ImageResource $从$ code>返回的c $ c> makeGray()
没有文件名,因为它已在内存中创建并且setFileName()
没有被要求它。
请改用输入文件的名称:
YourImageResource
returned frommakeGray()
does not have a file name because it has been created in memory andsetFileName()
has not been called for it.
Use the name of the input file instead:
gray.setFileName(f.getParent() + f.pathSeperator + "copy-" + f.GetName());
如果你需要将文件保存在不同的目录中,请使用而不是 f.getParent()
。
If you need to save the file in a different directory use that instead of f.getParent()
.
这篇关于如何创建彩色照片转换为灰度并由java保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文