java web 打水印
/**
* 把图片印刷到图片上
*
* @param pressImg --
* 水印文件
* @param targetinp --
* 目标文件
* @param x
* --x坐标
* @param y
* --y坐标
*/
public static File pressImage(File pressImg, InputStream targetinp,String format,
int x, int y) {
File file = null;
try {
//目标文件
Image src = ImageIO.read(targetinp);
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);
//水印文件
Image src_biao = ImageIO.read(pressImg);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
g.drawImage(src_biao, (wideth - wideth_biao),
(height - height_biao)-40 , wideth_biao, height_biao, null);
//水印文件结束
g.dispose();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image,format,os);
byte [] bytes = os.toByteArray();
BufferedOutputStream bos = null;
FileOutputStream fos = null;
file = new File("temp");
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
bos.close();
fos.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
以上工具代码
无论压缩 水印 都是差不多用Image.read
在spring muitpartfile 读取传入的file ,出现 can't read input file , 由于传入file 引起的问题
改为 inputstream即可
不过最好使用InputStream读
ps :
ImageIO.read(targetinp);
ImageIO的read 静态方法 可以传入 url , inputstream , file 多个重载方法
posted on 2017-03-22 09:12 signheart 阅读(1469) 评论(0) 编辑 收藏 举报