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

2024-11-27 15:33:34
推荐回答(1个)
回答(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)