java图片加水印代码 最好有实例!!!先谢了!!

java图片加水印代码 最好有实例!!!先谢了!!,第1张

文字水印

import java.awt.*

import java.awt.image.*

import java.io.*

import javax.swing.*

import com.sun.image.codec.jpeg.*

public class WaterSet {

/**

* 给图片添加水印

*

* @param filePath

*需要添加水印的图片的路径

* @param markContent

*水印的文字

* @param markContentColor

*水印文字的颜色

* @param qualNum

*图片质量

* @return

*/

public boolean createMark(String filePath, String markContent,

Color markContentColor, float qualNum) {

ImageIcon imgIcon = new ImageIcon(filePath)

Image theImg = imgIcon.getImage()

int width = theImg.getWidth(null)

int height = theImg.getHeight(null)

BufferedImage bimage = new BufferedImage(width, height,

BufferedImage.TYPE_INT_RGB)

Graphics2D g = bimage.createGraphics()

g.setColor(markContentColor)

g.setBackground(Color.white)

g.drawImage(theImg, 0, 0, null)

g.drawString(markContent, width / 5, height / 5)// 添加水印的文字和设置水印文字出现的内容

g.dispose()

try {

FileOutputStream out = new FileOutputStream(filePath)

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out)

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage)

param.setQuality(qualNum, true)

encoder.encode(bimage, param)

out.close()

} catch (Exception e) {

return false

}

return true

}

}

图片水印

import java.awt.Color

import java.awt.Font

import java.awt.Graphics

import java.awt.Image

import java.awt.image.BufferedImage

import java.io.File

import java.io.FileOutputStream

import javax.imageio.ImageIO

import com.sun.image.codec.jpeg.JPEGCodec

import com.sun.image.codec.jpeg.JPEGImageEncoder

public final class ImageUtils {

public ImageUtils() {

}

/*

* public final static String getPressImgPath() { return ApplicationContext

* .getRealPath("/template/data/util/shuiyin.gif")}

*/

/**

* 把图片印刷到图片上

*

* @param pressImg --

*水印文件

* @param targetImg --

*目标文件

* @param x

*--x坐标

* @param y

*--y坐标

*/

public final static void pressImage(String pressImg, String targetImg,

int x, int y) {

try {

//目标文件

File _file = new File(targetImg)

Image src = ImageIO.read(_file)

int wideth = src.getWidth(null)

int height = src.getHeight(null)

BufferedImage image = new BufferedImage(wideth, height,

BufferedImage.TYPE_INT_RGB)

Graphics g = image.createGraphics()

g.drawImage(src, 0, 0, wideth, height, null)

//水印文件

File _filebiao = new File(pressImg)

Image src_biao = ImageIO.read(_filebiao)

int wideth_biao = src_biao.getWidth(null)

int height_biao = src_biao.getHeight(null)

g.drawImage(src_biao, (wideth - wideth_biao) / 2,

(height - height_biao) / 2, wideth_biao, height_biao, null)

//水印文件结束

g.dispose()

FileOutputStream out = new FileOutputStream(targetImg)

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out)

encoder.encode(image)

out.close()

} catch (Exception e) {

e.printStackTrace()

}

}

/**

* 打印文字水印图片

*

* @param pressText

*--文字

* @param targetImg --

*目标图片

* @param fontName --

*字体名

* @param fontStyle --

*字体样式

* @param color --

*字体颜色

* @param fontSize --

*字体大小

* @param x --

*偏移量

* @param y

*/

public static void pressText(String pressText, String targetImg,

String fontName, int fontStyle, int color, int fontSize, int x,

int y) {

try {

File _file = new File(targetImg)

Image src = ImageIO.read(_file)

int wideth = src.getWidth(null)

int height = src.getHeight(null)

BufferedImage image = new BufferedImage(wideth, height,

BufferedImage.TYPE_INT_RGB)

Graphics g = image.createGraphics()

g.drawImage(src, 0, 0, wideth, height, null)

// String s="www.qhd.com.cn"

g.setColor(Color.RED)

g.setFont(new Font(fontName, fontStyle, fontSize))

g.drawString(pressText, wideth - fontSize - x, height - fontSize

/ 2 - y)

g.dispose()

FileOutputStream out = new FileOutputStream(targetImg)

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out)

encoder.encode(image)

out.close()

} catch (Exception e) {

System.out.println(e)

}

}

public static void main(String[] args) {

pressImage("F:/logo.png", "F:/123.jpg", 0, 0)

}

}

用过滤器,等我上线给你,不要结问题哦

package com.jc.ts.services

import java.awt.Color

import java.awt.Graphics

import java.awt.Image

import java.awt.image.BufferedImage

import java.io.FileInputStream

import java.io.FileNotFoundException

import java.io.IOException

import javax.imageio.ImageIO

import javax.servlet.http.HttpServletRequest

import javax.servlet.http.HttpServletResponse

import com.sun.image.codec.jpeg.ImageFormatException

import com.sun.image.codec.jpeg.JPEGCodec

import com.sun.image.codec.jpeg.JPEGImageEncoder

/**

*添加图片水印的服务类

* */

public class WaterMark {

/**

* @param sizeContext添加水印文字

* @param request 请求流对象

* @param request 响应流对象

* */

@SuppressWarnings("deprecation")

public static void createMarkSize(String sizeContext,HttpServletRequest request,HttpServletResponse response) {

try {

String path=request.getRealPath(request.getServletPath())

FileInputStream in=new FileInputStream(path)

Image src=ImageIO.read(in)

int w=src.getWidth(null)

int h=src.getHeight(null)

BufferedImage img=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB)//构建画板

Graphics g=img.getGraphics()//得到画笔

g.drawImage(src,0,0,w,h,null)//把源图片写入画板

g.setColor(Color.red)

g.drawString(sizeContext,10,5) // 添加文字

g.dispose()//生成图片

JPEGImageEncoder e=JPEGCodec.createJPEGEncoder(response.getOutputStream())

e.encode(img)

response.getOutputStream().close()

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} catch (ImageFormatException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

}

/**

* @param localPath 添加水印LOGO路径

* @param request 请求流对象

* @param request 响应流对象

**/

@SuppressWarnings("deprecation")

public static void createMarkLogo(String localPath,HttpServletRequest request,HttpServletResponse response) {

try {

FileInputStream file=new FileInputStream(localPath)

Image fimg=ImageIO.read(file)

int fw=fimg.getWidth(null)

int fh=fimg.getHeight(null)

String path=request.getRealPath(request.getServletPath())

FileInputStream in=new FileInputStream(path)

Image src=ImageIO.read(in)

int w=src.getWidth(null)

int h=src.getHeight(null)

BufferedImage img=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB)//构建画板

Graphics g=img.getGraphics()//得到画笔

g.drawImage(src,0,0,w,h,null)//把原图片写入画板

g.drawImage(fimg,w-20,h-15,fw,fh,null)//把水印图片写入画板

g.dispose()//生成图片

JPEGImageEncoder e=JPEGCodec.createJPEGEncoder(response.getOutputStream())

e.encode(img)

response.getOutputStream().close()

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} catch (ImageFormatException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

}

}

/**

* @param localPath 添加水印图片路径

* @param request 请求流对象

* @param request 响应流对象

* @param width 水印图片的宽度

* @param height 水印图片的长度

**/

@SuppressWarnings("deprecation")

public static void createMarkLogo(String localPath,HttpServletRequest request,HttpServletResponse response,int width,int height) {

try {

FileInputStream file=new FileInputStream(localPath)

Image fimg=ImageIO.read(file)

int fw=fimg.getWidth(null)

int fh=fimg.getHeight(null)

String path=request.getRealPath(request.getServletPath())

FileInputStream in=new FileInputStream(path)

Image src=ImageIO.read(in)

int w=src.getWidth(null)//w为你过滤图片的宽度

int h=src.getHeight(null)//h为你过滤图片的长度

BufferedImage img=new BufferedImage(w+width,h+height,BufferedImage.TYPE_INT_RGB)//构建画板(画板的宽度为两个图片之和)

Graphics g=img.getGraphics()//得到画笔

g.drawImage(src,0,0,w,h,null)//把原图片写入画板

g.drawImage(fimg,width,height,fw,fh,null)//把水印图片写入画板

g.dispose()//生成图片

JPEGImageEncoder e=JPEGCodec.createJPEGEncoder(response.getOutputStream())

e.encode(img)

response.getOutputStream().close()

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} catch (ImageFormatException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

}

}

注意第三个方法的注释地方g.drawImage(fimg,width,height,fw,fh,null)根据参数你在调调(放原图下面的)

过滤器调用

package com.jc.ts.filter

import java.io.IOException

import javax.servlet.Filter

import javax.servlet.FilterChain

import javax.servlet.FilterConfig

import javax.servlet.ServletException

import javax.servlet.ServletRequest

import javax.servlet.ServletResponse

import javax.servlet.http.HttpServletRequest

import javax.servlet.http.HttpServletResponse

import com.jc.ts.services.WaterMark

public class WaterFilter implements Filter {

public void destroy() {

// TODO Auto-generated method stub

}

public void doFilter(ServletRequest arg0, ServletResponse arg1,

FilterChain arg2) throws IOException, ServletException {

HttpServletRequest request=(HttpServletRequest)arg0

HttpServletResponse response=(HttpServletResponse)arg1

//WaterMark.createMarkSize("南京ts", request, response)

//WaterMark.createMarkLogo("D:\\workspace\\mybook\\WebRoot\\images\\logo\\book.jpg", request, response)

WaterMark.createMarkLogo("D:\\workspace\\mybook\\WebRoot\\images\\logo\\book.jpg", request, response,20,30)

//注意路径为绝对路径且三个效果不能同时执行

}

public void init(FilterConfig arg0) throws ServletException {

// TODO Auto-generated method stub

}

}

web.xml配置(写在servlet上面)

<filter>

<description>This is the description of my J2EE component</description>

<display-name>This is the display name of my J2EE component</display-name>

<filter-name>WaterFilter</filter-name>

<filter-class>com.jc.ts.filter.WaterFilter</filter-class>

</filter>

希望你能满意。。。。。。

package com.aspectj

import java.awt.Color

import java.awt.Graphics2D

import java.awt.Image

import java.awt.image.BufferedImage

import java.io.File

import java.io.FileOutputStream

import javax.swing.ImageIcon

import com.sun.image.codec.jpeg.JPEGCodec

import com.sun.image.codec.jpeg.JPEGEncodeParam

import com.sun.image.codec.jpeg.JPEGImageEncoder

/**

 * @author Administrator

 * 

 *         TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板

 * 

 *         添加水印, filePath 源图片路径 含图片名, watermark 水印图片路径 savePath

 *         为你添加水印后的图片保存路径文件夹 words 要添加的文字

 */

// 添加水印,filePath 源图片路径, watermark 水印图片路径

public class Mark {

private static int wid = 0

private static int het = 0

public static boolean createMark(String filePath, String watermark,

String words, String savePath) {

ImageIcon imgIcon = new ImageIcon(filePath)

Image theImg = imgIcon.getImage()

ImageIcon waterIcon = new ImageIcon(watermark)

Image waterImg = waterIcon.getImage()

// /////////////////////////////////////////////////////////////////////

File f = new File(filePath)

String picname = f.getName()// 取得图片名

if (watermark != null && !watermark.equals("")) {// 当水印图标为空时

ImageIcon markIcon = new ImageIcon(watermark) // 要添加的水印图标

Image markImg = markIcon.getImage()

wid = markImg.getWidth(null) // 水印图标宽度

het = markImg.getHeight(null) // 水印图标高度

}

// ////////////////////////////////////////////////////////////////////

int width = theImg.getWidth(null) // 源图片宽度

int height = theImg.getHeight(null) // 源图片高度

if (savePath.equals(""))

savePath = filePath// 如果未指定保存路径则保存回原路径

else

savePath = savePath + "指定保存文件夹时,拼接出保存路径"

BufferedImage bimage = new BufferedImage(width, height,

BufferedImage.TYPE_INT_RGB)

Graphics2D g = bimage.createGraphics()

g.setColor(Color.red) // 设置颜色

g.setBackground(Color.white)

g.drawImage(theImg, 0, 0, null)

g.drawImage(waterImg, width - wid + 5, height - het + 5, null) // 添加图标中间两个数字参数

// 是设定位置

g.drawString(words, width - 120, height - 10) // 添加文字

try {

FileOutputStream out = new FileOutputStream(savePath)

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out)

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage)

param.setQuality(50f, true) // 图片质量

encoder.encode(bimage, param)

out.close()

} catch (Exception e) {

e.printStackTrace()

System.out.println("===========水印失败")

return false

} finally {

System.gc()// 清理 垃圾对象

}

System.out.println("===========水印成功")

return true

}

// /测试主程序

public static void main(String[] args) {

createMark("dcc451da81cb39dbfa76de3ad2160924ab183023.jpg", "u=4038692558,3024950167&fm=21&gp=0.jpg", "aas", "")

}

}

检测水印很难做到,但是加上水印还是比较简单的


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/7981361.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存