• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

java 上传图片 打水印

其实就是在现有的图片上,画东西,也可以直接 贴图片

        //添加水印
	@Override
	public File pressFile(File file,String press_path) throws IOException {
		// TODO Auto-generated method stub
		File newFile=null;
                String pressText="TOGO";
		String path=file.getPath();
		String oldFileName=file.getName();
		//目标文件
		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);
		Graphics2D g = image.createGraphics();
		g.drawImage(src, 0, 0, wideth, height, null);
		
		//水印文件
		File pressFile=new File(press_path+"/images/LOGO.png");
		Image press_img=ImageIO.read(pressFile);
		int press_wideth=press_img.getWidth(null);
		int press_height=press_img.getHeight(null);
		//添加图片水印
		g.drawImage(press_img,wideth-press_wideth,height-press_height,press_wideth,press_height,null);	
		g.drawImage(press_img,(wideth-press_wideth)/2,(height-press_height)/2,press_wideth,press_height,null);
		g.drawImage(press_img,10,10,press_wideth,press_height,null);
		//添加文字水印
                g.setColor(Color.RED);
                g.setFont(new Font(fontName, fontStyle, fontSize));
                g.drawString(pressText, 10, 10);
                //添加完成
                g.dispose();
                //输出保存文件
                FileOutputStream out = new FileOutputStream(path);
		file.delete();
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);		
//		JPEGEncodeParam param=encoder.getDefaultJPEGEncodeParam(image);   //图片质量
//		param.setQuality(1, true);
		encoder.encode(image);
		out.close(); 

		return newFile;
	}


 

 

posted @ 2013-08-22 19:24  Class Xman  阅读(316)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3