ก่อนที่จะอ่านคุณต้องเข้าใจ SpringMVC ก่อนจากนั้นดูที่ภาพเอฟเฟกต์ก่อน
การเขียนรหัส
1. นำเข้าแพ็คเกจที่เกี่ยวข้อง
2. ไฟล์กำหนดค่า
web.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <web-app xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://xmlns.jcp.org/xml xsi: schemalocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/nsds id = "webapp_id" เวอร์ชัน = "3.1"> <pramplay-name> watermarkspringmvc </display-name> <servlet> <servlet-name> dispatcherservlet </servlet-name> <param-value> classpath: springmvc.xml </param-value> </itrin-param> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> <welcome-file> index.jsp </welcome-file> </welcome-file-list> </eb-app>
Springmvc.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http://www.springframework.org/schema/MVC http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <mvc: default-servlet-handler/> base-package = "com.wenteryan"> </บริบท: component-scan> <bean> <property name = "คำนำหน้า" value = "/"> </property> <property name = "suffix" value = ". jsp"> </property> <property name = "value =" org.springframework. <bean id = "multipartresolver"> <property name = "defaultEncoding" value = "utf-8"> </property> <property name = "maxuploadsize" value = "10485760000"> </property> <property name = "MaxinMemorySize"
3. เขียนการกระทำ
ลายน้ำ. การกระทำ
แพ็คเกจ com.wenteryan.watermarkspringmvc; นำเข้า Javax.servlet.http.httpsession; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.stereotype.controller; org.springframework.web.bind.annotation.requestmethod; นำเข้า org.springframework.web.bind.annotation.requestparam; นำเข้า org.springframework.web.multipart.commons.commonsmontipartfile; com.wenteryan.service.markservice; นำเข้า com.wenteryan.service.uploadservice; @controllerpublic ชั้นเรียนลายน้ำ {mackservice ส่วนตัว; อัพโหลดส่วนตัวอัปโหลดบริการ; @RequestMapping (value = "/watermark", method = requestMethod.post) Public ModelandView Watermark (@requestparam ("image") ไฟล์ CommonsMultipartFile, เซสชัน httpsession) โยนข้อยกเว้น String realUploadPath = session.getServletContext (). getRealPath (uploadPath); string imageUrl = uploadService.uploadImage (ไฟล์, uploadPath, realuploadPath); String logoimageUrl = mackservice.watermark (ไฟล์, uploadpath, realuploadpath); ModelAndView ret = new ModelandView (); ret.addobject ("ImageUrl", ImageUrl); ret.addobject ("logoimageurl", logoimageurl); ret.setViewName ("watermark"); return ret; } @autowired โมฆะสาธารณะ setMackService (Markservice MackService) {this.mackService = MackService; } @autowired โมฆะสาธารณะ setuploadService (UploadService UploadService) {this.uploadService = UploadService; -4. เขียนคลาสบริการ
Markservice .Java
แพ็คเกจ com.wenteryan.service; นำเข้า java.awt.color; นำเข้า java.awt.font; นำเข้า java.io.file; นำเข้า org.springframework.web.multipart.commons.commonsmultipartfile; สตริงสุดท้ายคงที่ FONT_NAME = "Microsoft Yahei"; public Static Final Int font_size = 120; public Static Final int font_stype = font.bold; สาธารณะสีสุดท้ายสีสุดท้าย FONT_COLOR = COLOR.RED; สาธารณะคงที่สุดท้าย int x = 10; สาธารณะคงที่สุดท้าย int y = 10; ล่องลอยแบบคงที่สาธารณะอัลฟ่า = 0.3F; ลายน้ำสาธารณะสาธารณะ (ไฟล์ CommonsMultipartFile, String uploadPath, String realUploadPath); -
5. คลาสการใช้งานอินเตอร์เฟสเขียน
UploadService .Java
แพ็คเกจ com.wenteryan.service.impl; นำเข้า java.io.fileoutputstream; นำเข้า java.io.ioexception; นำเข้า java.io.InputStream; นำเข้า java.io.OutputStream; นำเข้า org.springframework.stereotype.service; org.springframework.web.multipart.Commons.CommonsMultipartFile; @ServicePublic คลาส UploadService {Public String UploadImage (ไฟล์ CommonsMultipartFile OutputStream OS = NULL; ลอง {is = file.getInputStream (); OS = ใหม่ fileOutputStream (RealUploadPath+"/"+file.getoriginalfilename ()); ไบต์ [] บัฟเฟอร์ = ไบต์ใหม่ [1024]; int len = 0; ในขณะที่ ((len = is.read (บัฟเฟอร์))> 0) {os.write (บัฟเฟอร์); }} catch (exception e) {e.printstacktrace (); } ในที่สุด {ถ้า (คือ! = null) {ลอง {is.close (); } catch (ioexception e) {// todo บล็อก catch block ที่สร้างอัตโนมัติ e.printstacktrace (); }} if (os! = null) {ลอง {os.close (); } catch (ioexception e) {// todo บล็อก catch block ที่สร้างอัตโนมัติ e.printstacktrace (); }} return uploadPath+"/"+file.getoriginalfilename (); -Markserviceimpl .Java
แพ็คเกจ com.wenteryan.service.impl; นำเข้า java.awt.alphacomposite; นำเข้า java.awt.font; นำเข้า java.awt.graphics2d นำเข้า java.awt.image; นำเข้า java.Awt.image.BufferedImage; java.io.InputStream; นำเข้า java.io.OutputStream; นำเข้า Javax.imageio.imageio; นำเข้า org.springframework.stereotype.service; นำเข้า org.springframework.web.multipart.commons.commontiPartiPartfile; com.sun.image.codec.jpeg.jpegimageencoder; นำเข้า com.wenteryan.service.markservice; @servicepublic คลาส markserviceimpl ใช้ markservice {@Override String ลายน้ำสาธารณะ "โลโก้"+file.getoriginalfilename (); OutputStream OS = NULL; ลอง {image image2 = imageio.read (file.getInputStream ()); ความกว้าง int = image2.getWidth (null); ความสูง int = image2.getheight (null); bufferedImage bufferimage = bufferedImage ใหม่ (ความกว้าง, ความสูง, bufferedImage.type_int_rgb); graphics2d g = bufferimage.createGraphics (); G.Drawimage (Image2, 0, 0, ความกว้าง, ความสูง, null); G.SetFont (FONT ใหม่ (FONT_NAME, FONT_STYPE, FONT_SIZE)); G.SetColor (FONT_COLOR); int width1 = font_size*getTextLength (mark_text); int height1 = font_size; int widthdiff = ความกว้างความกว้าง 1; int heightdiff = ความสูง 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 = ใหม่ fileOutputStream (RealUploadPath+"/"+logofileName); JPeGimageEncoder EN = JPEGCODEC.CREATEJPEGENCODER (OS); EN.ENCODE (BUFFERIMAGE); } catch (exception e) {e.printstacktrace (); } ในที่สุด {ถ้า (os! = null) {ลอง {os.close (); } catch (ioexception e) {// todo บล็อก catch block ที่สร้างอัตโนมัติ e.printstacktrace (); }} return uploadPath+"/"+logofileName; } public int getTextLength (ข้อความสตริง) {int length = text.length (); สำหรับ (int i = 0; i <text.length (); i ++) {string s = string.valueof (text.charat (i)); if (s.getBytes (). ความยาว> 1) {ความยาว ++; }} ความยาว = ความยาว%2 == 0? ความยาว/2: ความยาว/2+1; ความยาวคืน; - 6. เขียนหน้า
index.jsp
<form action = "watermark" method = "post" enctype = "multipart/form-data"> <h2> โปรดเลือกรูปภาพที่อัปโหลด </h2> <div> <br> <input type = "file" name = "image" Id = "image"
Watermark.jsp
<div> <img src = "$ {pageContext.request.contextpath} $ {imageUrl}"/> <img src = "$ {pageContext.Request.ContextPath} $ {lOgoImageUrl}"/> <a href = "$สรุป
Java มีแพ็คเกจการประมวลผลภาพพิเศษซึ่งควรจะสามารถใช้ฟังก์ชั่นลายน้ำได้ หลังจากตรวจสอบข้อมูลฉันพบว่า Java สะดวกมากในการใช้ลายน้ำ ลายน้ำอาจเป็นรูปภาพหรือข้อความ จะมีลายน้ำในระยะต่อมา หากคุณต้องการในอนาคตคุณสามารถเขียนโค้ดเพื่อประมวลผลรูปภาพของคุณเองเป็นแบทช์
ข้างต้นเป็นเรื่องเกี่ยวกับบทความนี้ฉันหวังว่ามันจะเป็นประโยชน์สำหรับทุกคนในการเรียนรู้การเขียนโปรแกรม Java