Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在图像上追加多行文字并保存_Java_Image_Bufferedimage - Fatal编程技术网

Java 在图像上追加多行文字并保存

Java 在图像上追加多行文字并保存,java,image,bufferedimage,Java,Image,Bufferedimage,我的目标是在图像上附加文本并保存图像。我能够在图像上附加文本。 我的问题是如何为文本指定区域,以及如何使其多行,以便数据不会跨越边界。 代码:- 此处字符串s2不适合图像屏幕。提前感谢。您可以在设置计划使用的字体后,对当前图形调用getFontMetrics()。stringWidth(您的_字符串)将为您提供当前字体中字符串的宽度,就像它显示在BuffereImage中一样。然后可以构建字符串,直到它超过图像的宽度,然后为下一行启动另一个字符串。拆分字符串后,您必须计算出字符串位置的垂直偏移,

我的目标是在图像上附加文本并保存图像。我能够在图像上附加文本。 我的问题是如何为文本指定区域,以及如何使其多行,以便数据不会跨越边界。 代码:-


此处字符串s2不适合图像屏幕。提前感谢。

您可以在设置计划使用的字体后,对当前图形调用getFontMetrics()。stringWidth(您的_字符串)将为您提供当前字体中字符串的宽度,就像它显示在BuffereImage中一样。然后可以构建字符串,直到它超过图像的宽度,然后为下一行启动另一个字符串。拆分字符串后,您必须计算出字符串位置的垂直偏移,并知道需要包含多少行/字符串。这次再次使用fontMetrics获取用于计算的字符串高度

下面是一个类似的问题,给出了拆分的简单版本:

这里有一个实用程序类,在给定FontMetrics和字符串的情况下,它为您完成了大量这方面的工作

我敢肯定,如果更新的Java版本有与此相关的附加功能,其他人可以发表评论

public Test()
 {
s = "Welcome TO Image Rendering";
s1="Title data";
s2="DescriptionA fast paced action thriller about a stunt driver.He finds himself a target for some dangerous men after he agrees to help his beautiful neighbour, Irene. Subtitles: Chinesedata";
         Font f = new Font("Serif",Font.BOLD,22);
         text = new JLabel("Welcome TO Image Rendering");
         text.setFont(f);

         AttributedString astr = new AttributedString(s);
         astr.addAttribute(TextAttribute.FOREGROUND, Color.red, 0, 1);

         MediaTracker mt = new MediaTracker(this);
         image = Toolkit.getDefaultToolkit().createImage("c://Ci_Audio.jpg");
         mt.addImage(image,0);
         try{mt.waitForID(0);}catch(InterruptedException ie){}
         int width = image.getWidth(null);
         int height = image.getHeight(null);
         BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
         bimg.createGraphics().drawImage(image, 0, 0, this);
         bimg.getGraphics().setFont(f);
         bimg.getGraphics().drawString(s,10,50);
         bimg.getGraphics().drawString(s1,38,40);
         bimg.getGraphics().drawString(s2,188,84);
         img = new ImageIcon(bimg);
         label = new JLabel(img);
         p = new JPanel();
         p.add(label);
         getContentPane().add(p);
     }
     public static void main(String args[])
         {
         Test tt = new Test();
         tt.setDefaultCloseOperation(EXIT_ON_CLOSE);
         tt.setSize(360,305);
         tt.setVisible(true);
     }
}