繁体   English   中英

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

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

我需要在图像上写一些文本,然后将其居中或放在我想逐步习惯库的某些段落中,因此我编写了在教程中找到的以下代码:

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

我希望路径上的文件已经被编辑,但事实并非如此,我错过了什么吗? 顺便说一句,这是一个 .jpg 图像,当它起作用时,我也必须在这个图像上放一个较小的图像,所以希望图书馆也能做到这一点

编辑:伙计们使用 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();
        }
    }

我不知道 imagej,但在 ImageIO 中,模式将是相似的:

  • 加载图像
  • 处理它(在顶部绘制字符串)
  • 保存图像

在您的示例中,我看不到您尝试将图像保存回磁盘。 由于我自己不使用 ImageJ,我只能建议检查文档。 请参阅https://albert.rierol.net/imagej_programming_tutorials.html#How%20to%20integrate%20a%20new%20file%20format%20reader%20and%20writer ,尤其是关于writer的部分。 对于那些不喜欢仅链接答案的人,我复制了那里提到的示例代码:

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