如何利用java在图片上添加文字

如何利用java在图片上添加文字,第1张

private static final String FILEINPUT="C:/apache-tomcat-6.0.18/webapps/elePhoto/images/许慧欣.jpg"private static final String FILEMARK="C:/apache-tomcat-6.0.18/webapps/elePhoto/images/http_imgload.jpg"private static final String FILEDEST1="C:/apache-tomcat-6.0.18/webapps/elePhoto/images/许慧欣1.jpg"private static final String FILEDEST2="C:/apache-tomcat-6.0.18/webapps/elePhoto/images/许慧欣2.jpg"/*** 给图片添加文字水印* @param filePath 需要添加水印的图片的路径* @param markContent 水印的文字* @param markContentColor 水印文字的颜色* @param qualNum 图片质量* @return 布尔类型*/public boolean createStringMark(String filePath,String markContent,Color markContentColor,float qualNum){ImageIcon imgIcon=new ImageIcon(filePath)Image theImg =imgIcon.getImage()int width=theImg.getWidth(null)int height= theImg.getHeight(null)System.out.println(theImg)BufferedImage bimage = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB)Graphics2D g=bimage.createGraphics()g.setColor(markContentColor)g.setBackground(Color.white)g.drawImage(theImg, 0, 0, null )g.setFont(new Font(null,Font.BOLD,32))//字体、字型、字号g.drawString(markContent,width/10,height/10)//画文字g.dispose()try{FileOutputStream out=new FileOutputStream(FILEDEST1)//先用一个特定的输出文件名JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out)JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage)param.setQuality(qualNum, true)encoder.encode(bimage, param)out.close()}catch(Exception e)

import java.awt.Color

import java.awt.Font

import java.awt.Graphics

import java.awt.image.BufferedImage

import java.io.File

import java.io.IOException

import javax.imageio.ImageIO

public class Test{

public static void main(String[] args){

try {

File f1 = new File("a.jpg")

BufferedImage image = ImageIO.read(f1)

Graphics g = image.getGraphics()

g.setFont(new Font("Serif",Font.BOLD,20))

g.setColor(Color.red)

g.drawString("happy new year!", 10, 20)

File f2 = new File("copy.jpg")

ImageIO.write(image, "JPEG", f2)

} catch (IOException e) {

e.printStackTrace()

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11623093.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存