如何才能将上传得图片自动加上水印,最好有个例子,谢谢各位高手。
解决方案 »
- 表单提交有时能成功保存,有时不能成功保存。websphere!
- 数据丢失,还能找回来吗?都进来给点建议吧!
- 麻烦熟悉JSTL的兄弟帮忙阐明一些困惑?
- 拜托了!myeclipse里javabean頁面中文亂碼(系統繁體)
- 怎么样上传图片文件
- 先谢,请教大家一个关于session的问题
- 我的resin运行的时候说找不到JDK的支持!谁可以解决?
- 特别简单的问题。
- 类ServletContext的setAttribute()设置属性的生命周期是怎样的?
- 为什么我在jsp中执行executequery后,数据库数据进去了,但报错SQLException: No ResultSet was produced
- jsp中如何写用fso生成静态html文件
- 找人做网站 规定只能用JSP
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.OutputStream;
import javax.imageio.ImageIO;public class Graphic {
Image image; int w; int h; /**
*
* @param args
*/
public static void main(String[] args) {
try {
// test resize
Image image = ImageIO.read(new File("d:/pic.png"));
Graphic graphic = new Graphic(image);
BufferedImage bfresize = graphic.resize(graphic.w / 2,
graphic.h / 2);
graphic.output(bfresize, new File("d:/picresize.png")); // test clip
BufferedImage bfclip = graphic.clip(30, 30, 100, 100);
graphic.output(bfclip, new File("d:/picclip.png")); // test mask
Font font = new Font("Times New Roman", Font.BOLD, 18);
Color color = new Color(255, 0, 0);
BufferedImage bfimg = graphic.mask(font, color, "mask demo", 200,
200);
graphic.output(bfimg, new File("d:/picmask.PNG")); // test splice
Image subpic = ImageIO.read(new File("d:/subpic.png"));
BufferedImage bfsplice = graphic.splice(subpic, 0, 0, subpic
.getWidth(null), subpic.getHeight(null), 0, 0, subpic
.getWidth(null), subpic.getHeight(null));
graphic.output(bfsplice, new File("d:/picsplice.png"));
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
} /**
*
* @param image
*/
public Graphic(Image image) {
this.image = image;
this.w = image.getWidth(null);
this.h = image.getHeight(null);
} /**
* 鍓緫鍥剧墖鐨勪竴閮ㄥ垎
*
* @param x
* @param y
* @param w
* @param h
* @return
*/
public BufferedImage clip(int x, int y, int w, int h) {
if (!checkCoordinate(x, y) || w <= 0 || h <= 0) {
return null;
}
BufferedImage bfimg = new BufferedImage(this.w, this.h,
BufferedImage.TYPE_INT_RGB);
Graphics gp = bfimg.getGraphics();
gp.drawImage(this.image, 0, 0, this.w, this.h, 0, 0, this.w, this.h,
null);
gp.dispose();
return bfimg.getSubimage(x, y, w, h);
} /**
* 鏀瑰彉鍥剧墖澶у皬
*
* @param w
* 鏀瑰彉鍚庣殑鍥惧
* @param h
* 鏀瑰彉鍚庣殑鍥鹃珮
* @return
*/
public BufferedImage resize(int w, int h) {
if (w <= 0 || h <= 0) {
return null;
}
Image newImage = this.image
.getScaledInstance(w, h, Image.SCALE_DEFAULT);
BufferedImage bfimg = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
Graphics gp = bfimg.getGraphics();
gp.drawImage(newImage, 0, 0, w, h, 0, 0, w, h, null);
gp.dispose();
return bfimg;
} public void format(String fmt) { } /**
* 鍥剧墖娣诲姞鏂囧瓧
*
* @param font
* 瀛椾綋
* @param color
* 棰滆壊
* @param str
* 鏂囧瓧
* @param w
* 鍐欏叆鏂囧瓧鐨勮捣鐐箈鍧愭爣
* @param h
* 鍐欏叆鏂囧瓧鐨勮捣鐐箉鍧愭爣
* @return
*/
public BufferedImage mask(Font font, Color color, String str, int w, int h) {
if (!checkCoordinate(w, h)) {
return null;
}
if (w >= this.w || h >= this.h) {
return null;
}
BufferedImage bfImg = new BufferedImage(this.w, this.h,
BufferedImage.TYPE_INT_RGB);
Graphics gp = bfImg.getGraphics();
Font oldFont = gp.getFont();
Color oldColor = gp.getColor();
if (font != null) {
gp.setFont(font);
}
if (color != null) {
gp.setColor(color);
}
gp.drawImage(this.image, 0, 0, this.w, this.h, 0, 0, this.w, this.h,
null);
gp.drawString(str, w, h);
gp.setFont(oldFont);
gp.setColor(oldColor);
gp.dispose();
return bfImg;
} /**
* 鍥剧墖鍙犲姞
* @param image
* 鍙犲姞鍔犵殑鍥剧墖鐗?
* @param dx1
* 鑳屾櫙绗竴鐐圭殑x搴ф爣
* @param dy1
* 鑳屾櫙绗竴鐐圭殑y搴ф爣
* @param dx2
* 鑳屾櫙绗簩鐐圭殑x搴ф爣
* @param dy2
* 鑳屾櫙绗簩鐐圭殑y搴ф爣
* @param sx1
* 鍥剧墖绗竴鐐圭殑x搴ф爣
* @param sy1
* 鍥剧墖绗竴鐐圭殑y搴ф爣
* @param sx2
* 鍥剧墖绗簩鐐圭殑x搴ф爣
* @param sy2
* 鍥剧墖绗簩鐐圭殑y搴ф爣
* @return
*/
public BufferedImage splice(Image image, int dx1, int dy1, int dx2,
int dy2, int sx1, int sy1, int sx2, int sy2) {
BufferedImage bfImg = new BufferedImage(this.w, this.h,
BufferedImage.TYPE_INT_RGB);
Graphics gp = bfImg.getGraphics();
int imgW = image.getWidth(null);
int imgH = image.getHeight(null);
if (!checkCoordinate(dx1, dy1, dx2, dy2)
&& !checkCoordinate(sx1, sy1, sx2, sy2)) {
return null;
}
if (dx1 >= this.w || dy1 >= this.h || sx1 >= imgW || sy1 >= imgH) {
return null;
}
gp.drawImage(this.image, 0, 0, this.w, this.h, 0, 0, this.w, this.h,
null);
gp.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
gp.dispose();
return bfImg;
} /**
*
* @param bfimg
* @param os
*/
public void output(BufferedImage bfimg, OutputStream os) {
// ImageIO.write(RenderedImage.)
if (bfimg == null)
return;
try {
// TODO:
ImageIO.write(bfimg, "png", os);
} catch (Exception e) {
System.out.println(e.getMessage());
}
} /**
*
* @param bfimg
* @param file
*/
public void output(BufferedImage bfimg, File file) {
if (bfimg == null)
return;
try {
// TODO:
ImageIO.write(bfimg, "png", file);
} catch (Exception e) {
System.out.println(e.getMessage());
}
} /**
* 妫€鏌ュ潗鏍囩殑鏈夋晥鎬?涓ょ偣鍐冲畾涓€涓煩褰?
*
* @param x1
* 绗竴鐐圭殑x搴ф爣
* @param y1
* 绗竴鐐圭殑y搴ф爣
* @param x2
* 绗簩鐐圭殑x搴ф爣
* @param y2
* 绗簩鐐圭殑y搴ф爣
* @return
*/
private boolean checkCoordinate(int x1, int y1, int x2, int y2) {
if (x1 < 0 || y1 < 0 || x1 < 0 || y2 < 0) {
return false;
}
if (x1 > x2 || y1 > y2) {
return false;
}
return true;
} /**
* 妫€鏌ョ偣鍧愭爣鐨勬湁鏁堟€?
*
* @param x
* x搴ф爣
* @param y
* y搴ф爣
* @return
*/
private boolean checkCoordinate(int x, int y) {
if (x < 0 || y < 0)
return false;
return true;
}
}