読む前に、最初にSpringMVCを理解し、最初に効果の写真を見てください。
コードライティング
1。関連するパッケージをインポートします
2。構成ファイル
web.xml
<?xml version = "1.0" encoding = "utf-8"?> <web-app xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://xmlns.jcp.org/xml/ns/javaee xsi:schemalocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/ns/javaee id = "webapp_id" version = "3.1"> <display-name> watermarkspringmvc </display-name> <servlet-name> dispatcherservlet </servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet </servet-conticconfig> <init-param> <pram-name> <parm-value> classpath:springmvc.xml </param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-name> dispatcherservlet </servet-name> <url-pattern>/</url-pertherth <welcome-file> index.jsp </welcome-file> </welcome-file-list> </web-app>
springmvc.xml
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns:Context = "http://www.springframework.org/schema/context" xmlns:mvc = "http://www.springframework.org/schema/mvc" xsi:schemalocation = " http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-contex http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <mvc:default-servlethandler/> base-package = "com.wenteryan"> </context:component-scan> <bean> <プロパティ名= "preix" value = "/"> </property> <property name = "suffix" value = "。jsp"> </property> <property name = "viewclass" value = "org.springframework.web.servelet.wiew.wiew. id = "multipartresolver"> <プロパティ名= "defaultEncoding" value = "utf-8"> </property> <property name = "maxuploadsize" value = "10485760000"> </property name = "maxinmemorysize" value = "40960"
3。アクションを書きます
ウォーターマークアクション
パッケージcom.wenteryan.watermarkspringmvc; import javax.servlet.http.httpsession; import org.springframework.beans.factory.annotation.autowired; import org.springframework.streeotype.controller; import org.springframework.springframework.web.bind.annotation. org.springframework.web.bind.annotation.RequestMethod; Import org.springframework.web.bind.annotation.RequestParam; Import org.springframework.web.multipart.commons.commonsmultipartfile; Import.web.sprameframework.springframework com.wenteryan.service.markservice; Import com.wenteryan.service.uploadservice; @controllerpublic class Watermarkaction {private markservice mackservice; Private uploadService uploadService; @RequestMapping(value = "/watermark"、method = requestmethod.post)public modelandview watermark(@requestparam( "image")commonsmultipartfileファイル、httpssessionセッション)スロー例外{string uploadpath = "/images"; String realuploadpath = session.getServletContext()。getRealPath(uploadPath); string imageurl = uploadservice.uploadimage(file、uploadpath、realuploadpath); string logoimageurl = mackservice.watermark(file、uploadpath、realuploadpath); modelandview ret = new ModelandView(); Ret.AddoBject( "ImageUrl"、ImageUrl); Ret.Addobject( "logoimageurl"、logoimageurl); Ret.SetViewName( "Watermark"); Retを返します。 } @autowired public void setMackService(Markservice mackservice){this.mackservice = mackservice; } @autowired public void setuploadservice(uploadservice uploadservice){this.uploadservice = uploadService; }}4。サービスクラスを書き込みます
Markservice .java
パッケージcom.wenteryan.service; Import java.awt.color; import java.awt.font; import java.io.file; import org.springframework.web.multipart.commons.commonsmultipartfile; public interface markservice {public static final string final String = "waenteran" public static final string font_name = "microsoft yahei"; public static final int font_size = 120; public static final int font_stype = font.bold; public static final color font_color = color.red; public static final int x = 10; public static final int y = 10; public static float alpha = 0.3f;パブリックストリングウォーターマーク(CommonsMultipartFileファイル、String UploadPath、String RealuploAdPath); }5.インターフェイスの実装クラスを書き込みます
uploadservice .java
パッケージcom.wenteryan.service.impl; Import java.io.fileoutputStream; Import java.io.ioexception; Import java.io.inputStream; Import java.io.outputStream; Import org.springframework.stereotype.service; Import org.springframework.web.multipart.commons.commonsmultipartfile; @servicepublic class uploadservice {public string uploadimage(commonsmultipartfileファイル、string uploadpath、string realuploadpath){inputstream is = null; outputStream os = null; try {is = file.getInputStream(); os = new fileoutputStream(realuploadpath+"/"+file.getoriginalFileName()); byte [] buffer = new byte [1024]; int len = 0; while((len = is.read(buffer))> 0){os.write(buffer); }} catch(例外e){e.printstacktrace(); }最後に{if(is!= null){try {is.close(); } catch(ioException e){// todo auto-fenated catch block e.printstacktrace(); }} if(os!= null){try {os.close(); } catch(ioException e){// todo auto-fenated catch block e.printstacktrace(); }} uploadPath+"/"+file.getoriginalFileName()を返します。 }}markserviceimpl .java
パッケージcom.wenteryan.service.impl; import java.awt.alphacomposite; Import java.awt.font; Import java.awt.graphics2d; Import java.awt.image.Bufferedimage; Import Java.file. java.io.ioexception; Import java.io.inputStream; Import java.io.outputStream; Import javax.imageio.imageio; Import org.springframework.Struetoty.Service; Import org.springframework.web.multipart.commonsmultipartfile; com.sun.image.codec.jpeg.jpegcodec;インポートcom.sun.image.codec.jpegimageencoder;インポートcom.wenteryan.service.markservice; @servicepublic class class markserviceimpl実装{@override public string sintring(commonsmultipartfile、watermultipartfide) realuploadpath){// dodo auto-enerated method stub string logofilename = "logo"+file.getoriginalfilename(); outputStream os = null; try {image image2 = imageio.read(file.getInputStream()); int width = image2.getWidth(null); int height = image2.getheight(null); bufferedimage bufferimage = new BufferedImage(幅、高さ、bufferedimage.type_int_rgb); graphics2d g = bufferimage.creategraphics(); G.Drawimage(Image2、0、0、幅、高さ、ヌル); g.setFont(new Font(font_name、font_stype、font_size)); g.setcolor(font_color); int width1 = font_size*getTextLength(mark_text); int height1 = font_size; int widthdiff = width-width1; int heightdiff = height-height1; int x = x; int y = y; if(x> widthdiff){x = widthdiff; } if(y> heightdiff){y = heightdiff; } g.setcomposite(alphacomposite.getInstance(alphacomposite.src_atop、alpha)); g.drawstring(mark_text、x、y+font_size); g.dispose(); os = new fileoutputStream(realuploadpath+"/"+logofileName); jpegimageencoder en = jpegcodec.createjpegencoder(os); en.Encode(bufferimage); } catch(Exception e){e.printstacktrace(); }最後に{if(os!= null){try {os.close(); } catch(ioException e){// todo auto-fenated catch block e.printstacktrace(); }} uploadPath+"/"+logofileNameを返します。 } public int getTextLength(string text){int length = text.length(); for(int i = 0; i <text.length(); i ++){string s = string.valueof(text.charat(i)); if(s.getbytes()。length> 1){length ++; }} length = length%2 == 0?length/2:length/2+1;戻り長。 }} 6。ページを書きます
index.jsp
<form action = "watermark" method = "post" enctype = "multipart/form-data"> <h2>アップロードされた画像を選択してください</h2> <div> <br> <入力タイプ= "ファイル" name "image" id = "image"/> </div> <div> <br> <button = "shint </div button </div>
watermark.jsp
<div> <img src = "$ {pagecontext.request.contextpath} $ {imageurl}"/> <img src = "$ {pagecontext.request.contextpath} $ {logoimageurl}"/>> <$ {pagecontext.context.context puth> </div>要約します
Javaには特別な画像処理パッケージがあり、透かし機能も実装できるはずです。情報を確認した後、Javaは透かしを実装するのに非常に便利であることがわかりました。透かしは写真やテキストにすることができます。後の段階では透かしがあります。将来的に必要な場合は、コードを作成してバッチで自分の写真を処理できます。
上記はこの記事に関するものです。誰もがJavaプログラミングを学ぶことが役立つことを願っています。