缩略图的生成与添加水印
2014-11-30 11:22
232 查看
1 缩略图的生成
在目前很多的相片网页中,图片容量越大打开网页的速度就越慢,为了解决这个问题,我们可以根据大图生成小图,具体方法如下:
public static void createSmallPhoto(String photoPath, String smallPath) {
File _file = new File(photoPath); // 读入文件
Image src;
try {
src = javax.imageio.ImageIO.read(_file);
int wideth = 110;
int height = 80;
BufferedImage tag = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, wideth, height, null); // 绘制缩小后的图
FileOutputStream out = new FileOutputStream(smallPath); // 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // 近JPEG编码
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
在上面的代码中,参数photoPath表示大图的地址,参数smallPath表示生成缩略图的地址。在该方法中,首先将photoPath参数转换成Image对象src,够着目标文件tag,设置tag的长度和宽度,对tag进行编码,输出到文件流out,最后关闭文件流。、
在进行图片处理时,还有一些问题需要说明:
目前只能支持 JPG(JPEG)、GIF和PNG 3种格式
对于原图的容量是有限制的,最后不要超过1MB,否则会抛出内存不足的错误.
2 水印效果
给相片加水印就是在图片上添加文字,添加文字的作用就是最大限度地防止盗用,同时也起到标识图片的作用。该方法的具体实现如下:
public static boolean createMark(String filePath, String printPath,
String markContent) {
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(Color.red);
g.drawImage(theImg, 0, 0, null);
Font font = new Font(markContent, Font.BOLD, 200);
g.setFont(font);
g.setComposite(AlphaComposite
.getInstance(AlphaComposite.SRC_OVER, 0.5f));// 50%透明
g.rotate(0.3f); // 文字的旋转角度
g.drawString(markContent, width / 3, height / 3);// 绘制水印的位置
g.dispose();
try { //通过输出流生成图片内容
FileOutputStream out = new FileOutputStream(printPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(100f, true);
encoder.encode(bimage, param);
out.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
代码流程如下:
1 参数filePath表示原图的路径,参数printPath表示生成水印图片的路径,参数markContent表示加入水印文字的内容
2 根据源相片的路径filePath获取相片的对象,根据根据这个对象获取原图片的长度和宽度
3 根据原图片的长度和宽度,生成RGB类型的图片的相片缓存对象bimage
4 设置水印文字的效果,红色字,透明度50%及文字的倾斜度30度
5 通过输出流对象生成图片内容.
在目前很多的相片网页中,图片容量越大打开网页的速度就越慢,为了解决这个问题,我们可以根据大图生成小图,具体方法如下:
public static void createSmallPhoto(String photoPath, String smallPath) {
File _file = new File(photoPath); // 读入文件
Image src;
try {
src = javax.imageio.ImageIO.read(_file);
int wideth = 110;
int height = 80;
BufferedImage tag = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, wideth, height, null); // 绘制缩小后的图
FileOutputStream out = new FileOutputStream(smallPath); // 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // 近JPEG编码
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
在上面的代码中,参数photoPath表示大图的地址,参数smallPath表示生成缩略图的地址。在该方法中,首先将photoPath参数转换成Image对象src,够着目标文件tag,设置tag的长度和宽度,对tag进行编码,输出到文件流out,最后关闭文件流。、
在进行图片处理时,还有一些问题需要说明:
目前只能支持 JPG(JPEG)、GIF和PNG 3种格式
对于原图的容量是有限制的,最后不要超过1MB,否则会抛出内存不足的错误.
2 水印效果
给相片加水印就是在图片上添加文字,添加文字的作用就是最大限度地防止盗用,同时也起到标识图片的作用。该方法的具体实现如下:
public static boolean createMark(String filePath, String printPath,
String markContent) {
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(Color.red);
g.drawImage(theImg, 0, 0, null);
Font font = new Font(markContent, Font.BOLD, 200);
g.setFont(font);
g.setComposite(AlphaComposite
.getInstance(AlphaComposite.SRC_OVER, 0.5f));// 50%透明
g.rotate(0.3f); // 文字的旋转角度
g.drawString(markContent, width / 3, height / 3);// 绘制水印的位置
g.dispose();
try { //通过输出流生成图片内容
FileOutputStream out = new FileOutputStream(printPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(100f, true);
encoder.encode(bimage, param);
out.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
代码流程如下:
1 参数filePath表示原图的路径,参数printPath表示生成水印图片的路径,参数markContent表示加入水印文字的内容
2 根据源相片的路径filePath获取相片的对象,根据根据这个对象获取原图片的长度和宽度
3 根据原图片的长度和宽度,生成RGB类型的图片的相片缓存对象bimage
4 设置水印文字的效果,红色字,透明度50%及文字的倾斜度30度
5 通过输出流对象生成图片内容.
相关文章推荐
- c# 添加图片水印,可以指定水印位置+生成缩略图
- c# 添加图片水印,可以指定水印位置+生成缩略图
- 一个c#上传的程序,可以生成缩略图也可以自由添加水印效果
- 生成缩略图和添加水印
- PHP Imagick完美实现图片裁切、生成缩略图、添加水印
- 上传图片 并生成缩略图 并添加文字水印
- Struts2+FlashFileUpload文件上传并生成缩略图和添加水印(转)
- 生成缩略图和添加水印
- C#文件上传生成缩略图及添加文字和图片水印
- PHP 使用 Imagick 裁切/生成缩略图/添加水印, 自动检测和处理 GIF
- c# 添加图片水印,可以指定水印位置+生成缩略图
- c# 添加图片水印,可以指定水印位置+生成缩略图
- c# 添加图片水印,可以指定水印位置+生成缩略图[付上帅图1,2,3,4]
- asp.net 生成缩略图、为图片添加文字水印、图片水印等功能
- 一个c#上传的程序,可以生成缩略图也可以自由添加水印效果
- asp.net生成缩略图,添加图片水印的类
- aspjpeg 添加水印教程及生成缩略图教程
- c# 添加图片水印,可以指定水印位置+生成缩略图
- PHP 使用 Imagick 裁切/生成缩略图/添加水印自动检测和处理 GIF
- FCKEditor 2.63 上传图片时,可按日期生成文件名、生成缩略图、添加水印