import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageEdit {
public static void main(String[] a) {
ImageEdit.createStringMark("D://A.jpg", "测试文字","d://B.jpg");
* @param filePath 源图片路径
* @param markContent 图片中添加内容
* @param outPath 输出图片路径
* 字体颜色等在函数内部实现的
//给jpg添加文字
public static boolean createStringMark(String filePath,String markContent,String outPath)
ImageIcon imgIcon=new ImageIcon(filePath);
Image theImg =imgIcon.getImage();
int width=theImg.getWidth(null)==-1?200:theImg.getWidth(null);
int height= theImg.getHeight(null)==-1?200:theImg.getHeight(null);
System.out.println(width);
System.out.println(height);
System.out.println(theImg);
BufferedImage bimage = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
Graphics2D g=bimage.createGraphics();
Color mycolor = Color.red;
g.setColor(mycolor);
g.setBackground(Color.red);
g.drawImage(theImg, 0, 0, null );
g.setFont(new Font("宋体",Font.PLAIN,50)); //字体、字型、字号
g.drawString(markContent,width/2,height/2); //画文字
g.dispose();
FileOutputStream out=new FileOutputStream(outPath); //先用一个特定的输出文件名
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(100, true); //
encoder.encode(bimage, param);
out.close();
catch(Exception e)
{ return false; }
return true;
在
Java
中,我们经常需要处理
图片
,有时候需要在
图片
的特
定位
置上
添加
文字
。这种需求可能是为了标注
图片
中的重要信息,也可能是为了美化
图片
的展示效果。本文将介绍如何使用
Java
实现在
图片
指
定位
置上
添加
文字
的
功能
。
在
Java
中,我们可以使用
Java
AWT和
Java
Swing库来处理图形图像。其中,
Java
AWT提供了一些...
实现思路:
先使用绘图工具将上图中的时间抹成白色。将
图片
加载到BufferedImage类,使用Graphics2D类对
图片
进行
文字
绘制,最后输出流,前端通过a标签访问即可完成下载。在我的电脑中,Arial字体样式和
图片
中的时间的字体样式一致,将项目部署到linux中后由于系统中没有该字体,需要先向linux中安装字体,安装方式见:
https://blog.csdn.net/wangxintong_1992/article/details/81194896