code
import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.file;import java.io.fileOutputStream;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;import javax.imageio.ImageIO;public class WaterMark{ public static voID pressImage(String dst,String src,int x,int y){ try{ file f=new file(src); Image img=ImageIO.read(f); int w=img.getWIDth(null); int h=img.getHeight(null); BufferedImage bimg=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); Graphics g=bimg.getGraphics(); g.drawImage(img,w,null); file f1=new file(dst); Image img1=ImageIO.read(f1); int w1=img1.getWIDth(null); int h1=img1.getHeight(null); g.drawImage(img1,w-w1-x,h-h1-y,w1,h1,null); g.dispose(); fileOutputStream out=new fileOutputStream(dst); JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out); encoder.encode(bimg); out.close(); }catch(Exception e){ e.printstacktrace(); } } public static voID presstext(String presstext,String targetimg,String Fontname,int FontStyle,int color,int FontSize,int y){ try{ file f=new file(targetimg); Image img=ImageIO.read(f); int w=img.getWIDth(null); int h=img.getHeight(null); BufferedImage bimg=new BufferedImage(w,BufferedImage.TYPE_INT_RGB); Graphics g=bimg.getGraphics(); g.drawImage(img,null); g.setFont(new Font(Fontname,FontStyle,FontSize)); g.drawString(presstext,w-FontSize-x,h-FontSize/2-y); g.dispose(); fileOutputStream out=new fileOutputStream(targetimg); JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out); encoder.encode(bimg); out.close(); }catch(Exception e){ e.printstacktrace(); } } public static voID main(String[] args){ pressImage("1.jpg","2.jpg",200,200); presstext("真的好想你","3.jpg","宋体",Font.BolD,20,200); }}
以上是内存溢出为你收集整理的Java图片加水印全部内容,希望文章能够帮你解决Java图片加水印所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)