A watermark image of the Java implementation picture watermark (SPRINGMVC + Jsp)

Source: Internet
Author: User

Image watermark is not only the watermark text can also be a picture, this time to achieve a watermark image watermark picture, after the need to use a code batch processing their own pictures.

everybody look first .

Code Writing

1. Import the relevant rack package

2. Configure the XML file
Xml

    <Servlet>        <Servlet-name>Dispatcherservlet</servlet-name>        <Servlet-class>Org.Springframework.Web.Servlet.Dispatcherservlet</servlet-class>        <Init-param>            <Param-name>Contextconfiglocation</param-name>            <Param-value>Classpath:springmvc.XML</param-value>        </init-param>        <Load-on-startup>1</load-on-startup>    </servlet>    <Servlet-mapping>        <Servlet-name>Dispatcherservlet</servlet-name>        <Url-pattern>/</url-pattern>    </servlet-mapping>

Springmvc.xml

<mvc:default-servlet-handler/> <mvc:annotation-driven/> <context:component-scan base-package="Com.wenteryan"></context:component-scan> <beanclass="Org.springframework.web.servlet.view.InternalResourceViewResolver"> < Property name="prefix"Value="/"></ Property> < Property name="suffix"Value=". JSP"></ Property> < Property name="Viewclass"Value="Org.springframework.web.servlet.view.JstlView"></ Property> </bean> <beanID="Multipartresolver"             class="Org.springframework.web.multipart.commons.CommonsMultipartResolver"> < Property name="Defaultencoding"Value="UTF-8"></ Property> < Property name="Maxuploadsize"Value="10485760000"></ Property> < Property name="Maxinmemorysize"Value="40960"></ Property> </bean>

3. Writing the service class interface

Imagemarkservice.java

publicstaticfinal"logo.png" ;    publicwatermark(CommonsMultipartFile file, String uploadPath,             String realUploadPath) ;

4. Writing service class implementations

Uploadservice.java

@Service Public classUploadservice { PublicStringUploadimage(commonsmultipartfile file, String Uploadpath, String realuploadpath) {InputStream is=NULL; OutputStream OS =NULL;Try{ is= File.getinputstream (); OS =NewFileOutputStream (realuploadpath+"/"+file.getoriginalfilename ());byte[] buffer =New byte[1024x768] ;intLen =0; while((len= is. Read (buffer)) >0) {os.write (buffer); }        }Catch(Exception e)        {E.printstacktrace (); }finally{if( is!=NULL) {Try{ is. Close (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }            }if(os!=NULL) {Try{Os.close (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }            }        }returnuploadpath+"/"+file.getoriginalfilename (); }}

Imagemarkserviceimpl.java

@Service Public  class Imagemarkserviceimpl implements imagemarkservice {    @Override     PublicStringWatermark(commonsmultipartfile file, String Uploadpath, String realuploadpath) {//TODO auto-generated method stubString Logofilename ="Logoimage"+file.getoriginalfilename (); OutputStream OS =NULL; String Logopath = realuploadpath+"/"+logo;Try{Image image2 = Imageio.read (File.getinputstream ());intwidth = image2.getwidth (NULL) ;intHeight = image2.getheight (NULL) ; BufferedImage Bufferimage =NewBufferedImage (width, height, bufferedimage.type_int_rgb);            Graphics2D g = bufferimage.creategraphics (); G.drawimage (Image2,0,0, width, height,NULL) ; File logo =NewFile (Logopath); Image Imagelogo = imageio.read (logo);intWidth1 = Imagelogo.getwidth (NULL) ;intheight1 = Imagelogo.getheight (NULL) ;intWidthdiff = width-width1;intHeightdiff = height-height1;intx = x;inty = y;if(X>widthdiff)            {x = Widthdiff; }if(Y>heightdiff)            {Y=heightdiff;            } g.setcomposite (Alphacomposite.getinstance (Alphacomposite.src_atop, ALPHA)); G.drawimage (Imagelogo, x, Y,NULL) ;            G.dispose (); OS =NewFileOutputStream (realuploadpath+"/"+logofilename);            JPEGImageEncoder en = jpegcodec.createjpegencoder (OS);        En.encode (Bufferimage); }Catch(Exception e)        {E.printstacktrace (); }finally{if(os!=NULL) {Try{Os.close (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }            }        }returnuploadpath+"/"+logofilename; }}

5. Write action

Watermarkaction.java

@RequestMapping (value="/watermark", Method=requestmethod. POST) Public Modelandview Watermark (@RequestParam ("Image") Commonsmultipartfile file, HttpSession session) throws Exception {String Uploadpath ="/images" ;String Realuploadpath = Session. Getservletcontext(). Getrealpath(Uploadpath);String IMAGEURL = Uploadservice. Uploadimage(File, Uploadpath, Realuploadpath);String Logoimageurl = Imagemarkservice. Watermark(File, Uploadpath, Realuploadpath);Modelandviewret= new Modelandview ();        ret. AddObject("ImageUrl", IMAGEURL);        ret. AddObject("Logoimageurl", Logoimageurl);        ret. Setviewname("Watermark");Returnret ;}

6. Writing the page

watermark.jsp

<div class="Panel-body">        <img class="img-responsive img-rounded" src="${ Pagecontext.request.contextpath}${imageurl} "/>        <img class="img-responsive img-rounded" src="${ Pagecontext.request.contextpath}${logoimageurl} "/>        <a class="btn btn-warning" href="${ PageContext.request.contextPath} ">Return</a></div>
Summary

Java implementation watermark is very convenient, will be watermark image and watermark text, after the need to use this code batch processing their own pictures. GREAT!!!!!!.

A watermark image of the Java implementation picture watermark (SPRINGMVC + Jsp)

Related Article

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.