I need to write some text on an image, before centering it or put it in some paragraphs I wanted to get used to the library step by step so I've written this code found on a tutorial:
import ij.IJ;
import ij.ImagePlus;
import ij.process.ImageProcessor;
import java.awt.*;
public void prova(String nome) {
ImagePlus image = IJ.openImage(myimagepath);
Font font = new Font("Arial", Font.BOLD, 30);
ImageProcessor ip = image.getProcessor();
ip.setColor(Color.BLACK);
ip.setFont(font);
ip.drawString(nome, 20, 20);
}
I expected the file on the path to be already edited but it isn't, am I missing something? it's a .jpg image btw, also when this works I'll have to put an smaller image onto this one as well so ugh hope the library can do that too
EDIT: guys use ImageIO, i'm leaving the working code for the basic
public void prova(String nome, String profilo, MultipartFile foto) {
try {
BufferedImage image = ImageIO.read(new File(path));
Font font = new Font("Arial", Font.BOLD, 18);
Graphics g = image.getGraphics();
g.setFont(font);
g.setColor(Color.GREEN);
g.drawString(nome, 0, 20);
ImageIO.write(image, "jpg", new File(path.substring(0, path.lastIndexOf("\\")+1)+"processedImage.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I do not know imagej, but in with ImageIO the pattern would be similar:
In your example I cannot see that you try to save the image back to disk. As I do not use ImageJ myself I can only suggest to check the documentation. See https://albert.rierol.net/imagej_programming_tutorials.html#How%20to%20integrate%20a%20new%20file%20format%20reader%20and%20writer, especially the part regarding the writer. For those who prefer no link-only answers I copied the example code mentioned there:
static public void saveCustom(ImagePlus imp, String path) {
File file = new File(path);
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
// read data:
FileInfo fi = imp.getFileInfo();
// HEADER: ... read all header tags and metadata
dos.write( ... );
// BODY: ... read all stack slices (or single slice)
for (int i=1; i<imp.getNSlices(); i++) {
dos.write( ... );
}
dos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
dos.close();
}
}