0

在 C++/Java/Python 库中是否有 PHP 函数 imagettftext() 的简单等价物?

http://php.net/manual/en/function.imagettftext.php

4

1 回答 1

0

在python中你可以使用PIL

from PIL import Image, ImageDraw, ImageFont
font = ImageFont.truetype("/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf", 18)
img = Image.open("test.jpg")
draw = ImageDraw.Draw(img)
draw.text((40, 40), "...sample text...", font=font, fill="red")
img.show()

在 java 中,您可以简单地绘制任何java.awt.Image实例:

import javax.imageio.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;

public class ImageTest {
   public static void main(String args[]) throws Exception {
      BufferedImage img = ImageIO.read(new File("test.jpg"));
      Graphics2D g = img.createGraphics();
      Font f = Font.createFont(
            Font.TRUETYPE_FONT,
            new File("/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf")
      ).deriveFont(18f);
      g.setFont(f);
      g.setColor(Color.RED);
      g.drawString("test string", 40, 40);
      JFrame jf = new JFrame("test");
      jf.add(new JLabel(new ImageIcon(img)));
      jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      jf.setVisible(true);
      jf.pack();

   }
}

对于 c++ 有很多图像库(imagemagick-bindingsCImg,...)

于 2012-05-08T22:01:49.257 回答