Java add watermark preserves pixels of original picture

Source: Internet
Author: User
	/** * * @param filePath Original image Path * @param content Add watermark text/public static Boolean Photoaddwater (String FilePath,
		String content) {//Get the original icon ImageIcon imageicon=new ImageIcon (FilePath);
		
		Create Image Object Image Image=imageicon.getimage ();
		int width=image.getwidth (NULL);
		
		int height=image.getheight (NULL);
		Create picture container BufferedImage bufferedimage=new bufferedimage (width, height, bufferedimage.type_int_rgb);
	    
	    Create a painting Environment graphics2d g=bufferedimage.creategraphics ();
	    Text color G.setcolor (color.red);
	    Fonts font font=new font ("XXFarEastFont-Arial", Font.Bold, 50);
	    G.setfont (font);
	    G.drawimage (image, 0, 0,null);
	    g.DrawString (content, (WIDTH/15) *8, (HEIGHT/15) *14);
	    
	    Change G.dispose ();
	    FileOutputStream Outputstream=null;
               try {outputstream=new FileOutputStream ("f://test/" +system.currenttimemillis () + ". jpg");
	    	   JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (OutputStream); JPEGEncodeParam param = ENcoder.getdefaultjpegencodeparam (BufferedImage);
	            Set 1 originals to save Param.setquality (1, true);
	          Encoder.encode (BufferedImage, param);
			Outputstream.close ();
		return true;
		catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
		catch (Imageformatexception e) {//TODO auto-generated catch block E.printstacktrace ();
		catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
				}finally{try{if (OutputStream!=null) {outputstream.close ();
	}catch (Exception e) {//Todo:handle Exception}} return false; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.