码迷,mamicode.com
首页 > 编程语言 > 详细

Java 实现图片水印之水印图片(SpringMVC + Jsp)

时间:2016-05-12 14:12:59      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:

图片水印不仅水印文字还可以是图片,本次实现图片水印之水印图片,以后有需要可以用个代码批量处理自己的图片了。

大家先看效果图

技术分享

技术分享

技术分享

代码编写

1.导入相关架包

技术分享

2.配置XML文件
web.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>
    <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>

3.编写服务类接口

ImageMarkService.java

public static final String LOGO = "logo.png" ;

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

4.编写服务类实现

UploadService.java

@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() ;
    }
}

ImageMarkServiceImpl.java

@Service
public class ImageMarkServiceImpl implements ImageMarkService {

    @Override
    public String watermark(CommonsMultipartFile file, String uploadPath, String realUploadPath) {
        // TODO Auto-generated method stub
        String logoFileName = "logoImage"+file.getOriginalFilename() ;
        OutputStream os = null ;

        String logoPath = realUploadPath+"/"+LOGO ;
        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) ;

            File logo = new File(logoPath) ;
            Image imageLogo = ImageIO.read(logo) ;

            int width1 = imageLogo.getWidth(null) ;
            int height1 = imageLogo.getHeight(null) ;

            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.drawImage(imageLogo, x, y, null) ;
            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;
    }

}

5.编写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) ;

        ModelAndView ret = new ModelAndView() ;
        ret.addObject("imageUrl", imageUrl) ;
        ret.addObject("logoImageUrl", logoImageUrl) ;

        ret.setViewName("watermark");

        return ret ;
    }

6.编写页面

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>

总结

Java实现水印还是非常方便的,会了水印图片与水印文字,以后有需要可以用这个代码批量处理自己的图片了。棒棒的!!!!!!

Java 实现图片水印之水印图片(SpringMVC + Jsp)

标签:

原文地址:http://blog.csdn.net/wenteryan/article/details/51362384

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!