简体   繁体   English

ImageJ 如何在图像上写字符串?

[英]ImageJ how do I write strings on an image?

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顺便说一句,这是一个 .jpg 图像,当它起作用时,我也必须在这个图像上放一个较小的图像,所以希望图书馆也能做到这一点

EDIT: guys use ImageIO, i'm leaving the working code for the basic编辑:伙计们使用 ImageIO,我将工作代码留作基本

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:我不知道 imagej,但在 ImageIO 中,模式将是相似的:

  • load the image加载图像
  • process it (draw the string on top)处理它(在顶部绘制字符串)
  • save the image保存图像

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.由于我自己不使用 ImageJ,我只能建议检查文档。 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 .请参阅https://albert.rierol.net/imagej_programming_tutorials.html#How%20to%20integrate%20a%20new%20file%20format%20reader%20and%20writer ,尤其是关于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();  
    }  
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM