下面是一个示例代码,用于打开图像、反转图像并将其保存回去:
import ij.ImagePlus;
import ij.io.FileSaver;
import ij.process.ImageProcessor;
ImagePlus imgPlus = new ImagePlus("path-to-sample.jpg");
ImageProcessor imgProcessor = imgPlus.getProcessor();
imgProcessor.invert();
FileSaver fs = new FileSaver(imgPlus);
fs.saveAsJpeg("path-to-inverted.jpg");
下面是一个示例代码,演示如何操作图像以使其灰度:
BufferedImage bufferedImage = imgProcessor.getBufferedImage();
for(int y=0;y<bufferedImage.getHeight();y++)
{
for(int x=0;x<bufferedImage.getWidth();x++)
{
Color color = new Color(bufferedImage.getRGB(x, y));
int grayLevel = (color.getRed() + color.getGreen() + color.getBlue()) / 3;
int r = grayLevel;
int g = grayLevel;
int b = grayLevel;
int rgb = (r<<16) | (g<<8) | b;
bufferedImage.setRGB(x, y, rgb);
}
}
ImagePlus grayImg = new ImagePlus("gray", bufferedImage);
fs = new FileSaver(grayImg);
fs.saveAsJpeg("path-to-gray.jpg");
我希望它能帮助你开始:)