扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
比如我现在 要把我这个字和一张图片放到一起组成一张新图,应该怎么实现阿 |
比如我现在要把我这个字和一张图片放到一起组成一张新图,应该怎么实现?
import java.awt.*; import java.awt.event.*; import java.io.*; import java.awt.image.*; import org.w3c.dom.*; import com.sun.image.codec.jpeg.*; import javax.imageio.*; public class ImgBean{ public void ImgBean(){} public void ImgYin(String s,String ImgName){ try{ File _file = new File(ImgName); Image src = ImageIO.read(_file); int wideth=src.getWidth(null); int height=src.getHeight(null); BufferedImage image=new BufferedImage(wideth,height,BufferedImage.TYPE_INT_RGB); Graphics g=image.createGraphics(); g.drawImage(src,0,0,wideth,height,null); String s="我要加的水印"; g.setColor(Color.RED); g.setFont(new Font("宋体",Font.PLAIN,20)); Font aa=new Font("宋体",Font.PLAIN,20); g.drawString(s,wideth-150,height-10); g.dispose(); FileOutputStream out=new FileOutputStream(ImgName); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); } catch(Exception e){ System.out.println(e); } } }
我改了下,字体颜色再改改,换张图,调调字的位置就可以了.
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package test1; /** * * @author ilrxx */ import java.awt.*; import java.io.*; import java.awt.image.*; import com.sun.image.codec.jpeg.*; import javax.imageio.*; public class ImgBean{ public void ImgBean(){} public static void ImgYin(String s,String ImgName){ byte[] bytes = null; try{ String str = s; File _file = new File(ImgName); Image src = ImageIO.read(_file); int wideth=src.getWidth(null); int height=src.getHeight(null); BufferedImage image=new BufferedImage(wideth,height,BufferedImage.TYPE_INT_RGB); Graphics g=image.createGraphics(); g.drawImage(src,0,0,wideth,height,null); g.setColor(Color.RED); g.setFont(new Font("宋体",Font.PLAIN,20)); Font aa=new Font("宋体",Font.PLAIN,20); g.drawString(str,wideth-150,height-10); g.dispose(); ByteArrayOutputStream out1 = new ByteArrayOutputStream(); saveImage(image, out1); bytes = out1.toByteArray(); out1.close(); FileOutputStream out2 = new FileOutputStream(ImgName); out2.write(bytes); out2.close(); } catch(Exception e){ System.out.println(e); } } public static void saveImage(BufferedImage img, OutputStream out1) throws Exception { JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out1); encoder.encode(img); } public static void main(String[] args){ ImgYin("我要加的水印" , "d:/nbactivities.jpg"); } }
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者