How can I use ImageJ as a library for a separate Java application?

谁说我不能喝 提交于 2019-12-03 05:26:39

The following site describes ImageJ API with examples: http://albert.rierol.net/imagej_programming_tutorials.html#ImageJ programming basics

The examples include reading images, processing pixels etc. Well, I guess you will also need to use the API documentation a lot.

Here is a sample code that opens an image, inverts it and saves it back:

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

And here's a sample code that shows how to manipulate an image to make it grayscale:

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

I hope it helps you get started :)

Here is an opensource project implementation with imagej for photo sharing web app.

Use this as a reference to implement imagej apis in your application

http://www.gingercart.com/Home/java-snippets/create-image-thumbnail-in-java-using-imagej-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!