如何利用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.Frame

import java.awt.Graphics

public class DrawString extends Frame{

private static final int spacing = 20 //文字与图片边界的空白

private static final int wordNum = 7 //文字字号

private Color strColor

private StringBuffer message

private int pictureWidth

private int pictureHight

public void paint(Graphics g){

Color c = g.getColor()

g.setColor(strColor)

StringBuffer msg = message

int strNumPerLine = (pictureWidth - spacing) / wordNum

int strLineNum = message.length() / strNumPerLine

for(int l=strLineNum,n=0 l<0l--){

msg.substring(n, strNumPerLine)

g.drawString(msg.toString(), wordNum, wordNum * strNumPerLine)

n = strNumPerLine

}

g.setColor(c)

//this.draw(g)

}

public DrawString(Color strColor, StringBuffer message, int pictureWidth, int pictureHight) {

this.strColor = strColor

this.message = message

this.pictureWidth = pictureWidth

this.pictureHight = pictureHight

}

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存