最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)圖片上加文字水印(SpringMVC + Jsp)

 更新時(shí)間:2022年05月06日 15:34:34   作者:wenteryan  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)在圖片上加文字水印的方法,水印可以是圖片或者文字,操作方便,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

看之前要先對(duì)SpringMVC進(jìn)行了解打好基礎(chǔ),下面直接先看效果圖

代碼編寫(xiě)

1.導(dǎo)入相關(guān)架包

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/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 <display-name>watermarkspringmvc</display-name>
 <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>
 <welcome-file-list>
 <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 
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/mvc 
  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

 <mvc:default-servlet-handler/>
 <mvc:annotation-driven/> 
 <context:component-scan base-package="com.wenteryan"></context:component-scan>
 <bean class="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>

 <bean id="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>

</beans>

3.編寫(xiě)action

WatermarkAction .action

package com.wenteryan.watermarkspringmvc;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.wenteryan.service.MarkService;
import com.wenteryan.service.UploadService;

@Controller
public class WatermarkAction {

 private MarkService mackService ;
 private UploadService uploadService ;

 @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 = mackService.watermark(file, uploadPath, realUploadPath) ;
  ModelAndView ret = new ModelAndView() ;
  ret.addObject("imageUrl", imageUrl) ;
  ret.addObject("logoImageUrl", logoImageUrl) ;
  ret.setViewName("watermark");

  return ret ;
 }

 @Autowired
 public void setMackService(MarkService mackService) {
  this.mackService = mackService;
 }
 @Autowired
 public void setUploadService(UploadService uploadService) {
  this.uploadService = uploadService;
 }


}

4.編寫(xiě)服務(wù)類

MarkService .java

package 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 MARK_TEXT = "wenteryan" ;
 public static final String FONT_NAME = "微軟雅黑" ;

 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 ;

 public String watermark(CommonsMultipartFile file, String uploadPath, 
   String realUploadPath) ; 

}

5.編寫(xiě)接口實(shí)現(xiàn)類

UploadService .java

package 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;

@Service
public class UploadService {

 public String uploadImage(CommonsMultipartFile file, 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(Exception e) {
   e.printStackTrace() ;
  } finally {
   if(is!=null) {
    try {
     is.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if(os!=null) {
    try {
     os.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }

  return uploadPath+"/"+file.getOriginalFilename() ;
 }
}



MarkServiceImpl .java

package com.wenteryan.service.impl;

import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.wenteryan.service.MarkService;

@Service
public class MarkServiceImpl implements MarkService {

 @Override
 public String watermark(CommonsMultipartFile file, String uploadPath, String realUploadPath) {
  // TODO Auto-generated 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(width, height, BufferedImage.TYPE_INT_RGB) ;

   Graphics2D g = bufferImage.createGraphics() ;
   g.drawImage(image2, 0, 0, width, height, null) ;

   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() ;
  } finally {
   if(os!=null) {
    try {
     os.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }

  return 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 ;
  return length ;
 }

}

6.編寫(xiě)頁(yè)面
index.jsp

<form action="watermark" method="post" enctype="multipart/form-data">
  <h2>請(qǐng)選擇上傳的圖片</h2>
  <div class="form-group">
  <br>
  <input type="file" name="image" id="image" />
  </div>
  <div class="form-group">
  <br>
  <button class="btn btn-success" type="submit">開(kāi)始上傳</button>
  </div>
 </form>

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 }">返回</a>

 </div>

總結(jié)

Java有專門Image的處理包,同樣應(yīng)該可以實(shí)現(xiàn)水印功能,查了資料小試下來(lái)發(fā)現(xiàn)java實(shí)現(xiàn)水印還是非常方便的,水印可以是圖片或者文字,后期會(huì)有水印圖片水印,以后有需要可以寫(xiě)個(gè)代碼批量處理自己的圖片了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • SpringBoot單元測(cè)試之?dāng)?shù)據(jù)隔離詳解

    SpringBoot單元測(cè)試之?dāng)?shù)據(jù)隔離詳解

    我們?cè)趯?xiě)單元測(cè)試時(shí),有一個(gè)比較重要的要求是可以重復(fù)運(yùn)行, 那么這樣就會(huì)有一個(gè)比較麻煩的問(wèn)題:數(shù)據(jù)污染,所以本文為大家整理了兩個(gè)數(shù)據(jù)隔離的方式,希望對(duì)大家有所幫助
    2023-08-08
  • Java內(nèi)部類的實(shí)現(xiàn)原理與可能的內(nèi)存泄漏說(shuō)明

    Java內(nèi)部類的實(shí)現(xiàn)原理與可能的內(nèi)存泄漏說(shuō)明

    這篇文章主要介紹了Java內(nèi)部類的實(shí)現(xiàn)原理與可能的內(nèi)存泄漏說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • Java中如何靈活獲取excel中的數(shù)據(jù)

    Java中如何靈活獲取excel中的數(shù)據(jù)

    這篇文章主要給大家介紹了關(guān)于Java中如何靈活獲取excel中的數(shù)據(jù),在日常工作中我們常常會(huì)進(jìn)行文件讀寫(xiě)操作,除去我們最常用的純文本文件讀寫(xiě),更多時(shí)候我們需要對(duì)Excel中的數(shù)據(jù)進(jìn)行讀取操作,需要的朋友可以參考下
    2023-07-07
  • 什么是Spring Boot

    什么是Spring Boot

    Spring是一個(gè)非常受歡迎的Java框架,它用于構(gòu)建web和企業(yè)應(yīng)用。本文介紹將各種Spring的配置方式,幫助您了解配置Spring應(yīng)用的復(fù)雜性
    2017-08-08
  • Java用自定義的類作為HashMap的key值實(shí)例

    Java用自定義的類作為HashMap的key值實(shí)例

    下面小編就為大家?guī)?lái)一篇Java用自定義的類作為HashMap的key值實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-12-12
  • lombok?子類中如何使用@Builder問(wèn)題

    lombok?子類中如何使用@Builder問(wèn)題

    這篇文章主要介紹了lombok?子類中如何使用@Builder問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • java正則表達(dá)式解析html示例分享

    java正則表達(dá)式解析html示例分享

    這篇文章主要介紹了java正則表達(dá)式解析html示例,用到獲取url的正則表達(dá)式,獲取圖片的正則表達(dá)式,需要的朋友可以參考下
    2014-02-02
  • Spring Boot 結(jié)合 aop 實(shí)現(xiàn)讀寫(xiě)分離

    Spring Boot 結(jié)合 aop 實(shí)現(xiàn)讀寫(xiě)分離

    這篇文章主要介紹了Spring Boot 結(jié)合 aop 實(shí)現(xiàn)讀寫(xiě)分離的示例,幫助大家更好的理解和使用Spring Boot框架,感興趣的朋友可以了解下
    2020-11-11
  • Java數(shù)據(jù)類型之引用數(shù)據(jù)類型解讀

    Java數(shù)據(jù)類型之引用數(shù)據(jù)類型解讀

    這篇文章主要介紹了Java數(shù)據(jù)類型之引用數(shù)據(jù)類型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • SpringBoot中MyBatis-Flex的集成和使用實(shí)現(xiàn)

    SpringBoot中MyBatis-Flex的集成和使用實(shí)現(xiàn)

    MyBatis-Flex是一個(gè)基于MyBatis的數(shù)據(jù)訪問(wèn)框架,MyBatis-Flex能夠極大地提高我們的開(kāi)發(fā)效率和開(kāi)發(fā)體驗(yàn),本文主要介紹了SpringBoot中MyBatis-Flex的集成和使用實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12

最新評(píng)論

宁海县| 峨眉山市| 罗田县| 普宁市| 广汉市| 五莲县| 景宁| 舒城县| 西乌珠穆沁旗| 芦溪县| 安吉县| 句容市| 田东县| 吉首市| 望奎县| 阿巴嘎旗| 清流县| 皋兰县| 梓潼县| 泉州市| 台东县| 横山县| 宜君县| 新安县| 雅安市| 新民市| 广德县| 施秉县| 鄂托克前旗| 小金县| 西吉县| 东源县| 清镇市| 汤阴县| 鹤岗市| 襄垣县| 孟津县| 南和县| 西藏| 大关县| 屯留县|