先看一下效果图吧:
主要的代码如下:
package sea;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* User: Greta.Wang
* Date: 12-11-3
* Read nine pictures from file system, and compose them to one, in the process, each picture will be added a watermark.
*/
public class ComposePicture {
public static void main(String args[])
throws IOException {
//从工程中去读九张图片,并对图片做进一步的处理(缩小比例,旋转30度,加水印),并把图片保存在内存中做进一步的处理
List<BufferedImage> bufferedImageList = readPicturesToMemory();
//在内存中,合并九张图片成一张图片
BufferedImage bufferedImageNineByNine = composeNinePicturesToOne(bufferedImageList);
//把内存中的图片写入到指定的文件中
File fileOutPut = new File(System.getProperty("user.dir") + "/handledpictures/compose.jpg");
ImageIO.write(bufferedImageNineByNine, "jpg", fileOutPut);
}
public static List<BufferedImage> readPicturesToMemory()
throws IOException {
String[] pathArray = new String[9];
List<BufferedImage> bufferedImageList = new ArrayList<BufferedImage>();
//Thumbnail读取水印文件到内存中
BufferedImage waterMarkBufferedImage = Thumbnails.of(ComposePicture.class.getClassLoader().getResource("5.png").getFile())
//Thumbnail的方法,根据比例缩小,0.4f意思是缩小到原图的40%
.scale(0.4f)
//读取成BufferedImage对象
.asBufferedImage();
//读取9张待处理的图片到内存中
for (int i = 0; i < 9; i++) {
//取得图片的路径
pathArray[i] = ComposePicture.class.getClassLoader().getResource("IMG_000" + i + ".JPG").getFile();
BufferedImage image = Thumbnails.of(pathArray[i])
//按比例缩小
.scale(0.05f)
//加水印,0.5f表示的是透明度
.watermark(Positions.CENTER, waterMarkBufferedImage, 0.5f)
//旋转30度
.rotate(30)
//读取成BufferedImage对象
.asBufferedImage();
bufferedImageList.add(image);
}
return bufferedImageList;
}
public static BufferedImage composeNinePicturesToOne(List<BufferedImage> bufferedImageList) {
//取得处理后图像的宽度和高度,要处理的9张图片有相同的高度和宽度
int width = bufferedImageList.get(0).getWidth();
int height = bufferedImageList.get(0).getHeight();
List<int[]> imageArrayList = new ArrayList<int[]>();
for (BufferedImage bufferedImage : bufferedImageList) {
int[] oneImageArray = new int[width * height];
//读取图片成byte数组
oneImageArray = bufferedImage.getRGB(0, 0, width, height, oneImageArray, 0, width);
imageArrayList.add(oneImageArray);
}
//合并图片到同一个bufferedImage对象中
BufferedImage bufferedImageNineByNine = new BufferedImage(width * 3, height * 3, BufferedImage.TYPE_INT_RGB);
int x = 0;
int y = 0;
//根据x,y的坐标,先输出x轴上的三张,然后输出y轴上的图片
for (int[] buffered : imageArrayList) {
bufferedImageNineByNine.setRGB(width * y, height * x, width, height, buffered, 0, width);
y++;
if (y % 3 == 0) {
y = 0;
x++;
}
}
return bufferedImageNineByNine;
}
}
![点击查看原始大小图片](http://dl2.iteye.com/upload/attachment/0090/4345/53747855-5942-3d23-b8c7-ef76d810e14d-thumb.jpg)
- 大小: 47.7 KB
分享到:
相关推荐
thumbnailator(包括jar包跟api) java用于图片压缩 生成缩略图 添加水印等 这是我见过最好的压缩工具了 使用方法: Thumbnails.of(new File("path/to/directory").listFiles()) .size(640, 480) .outputFormat(...
Thumbnailator 是一个优秀的图片处理的Google开源Java类库。支持的处理操作:图片缩放,区域裁剪,水印,旋转,保持比例
Thumbnailator图片压缩jar包以及使用demo,图片等比例压缩 不改变尺寸压缩大小 以及批量压缩
功能强大的图片压缩工具类。支持比例缩放,指定大小进行缩放,旋转,水印,裁剪,转化图像格式等。
在代码中关键部分都有详细的注释...1、使用两张图片合成水印; 2、将文字直接绘制到图片上; 3、获取手机或sd卡上的图片,压缩并显示; 4、可将图片保存到sd卡上; 5、将图片进行Base64转换; 6、将Base64转换为图片。
thumbnailator-0.4.8最新压缩包,压缩图片代码,加水印更加方便易用
实现技术:Struts1.3+JSP 主要实现:上传文件类型的前台验证,后台验证上传文件的类型、格式;实现图片的压缩和添加水印等功能。
php 图片压缩添加水印,可自己设置参数
thumbnailator-0.4.8 图片压缩工具类及调用方法 ,含内部代码
jQuery图片自动添加水印插件是一款可以为图片添加自定义水印的jquery插件。
批量图片压缩加水印,实在好用 里面包括实用说明,
图片压缩jar包;thumbnailator 版本0.4.8 看到有人设置了50积分,还是上传一份吧
至于添加图片水印,我们可以使用一张透明小图片复制到另一张图片上。这样,我们的图片就拥有了一个独特的水印标识啦! 当然啦,压缩图片也是必不可少的步骤。在保存图片时设置压缩质量参数,从而达到压缩图片大小的...
java 图片压缩和加水印代码和必要的jar包
眸目图片批量压缩器Moumu 3.3中文绿色版(批量压缩相片及制作添加水印工具) 软件大小:495 KB 更新时间:2011-12-30 软件类别:国产软件 / 图像处理 软件语言:简体中文 授权方式:免费软件 Moumu眸目图片批量...
HTML5图片压缩旋转调整 可以自定义图片旋转方向后压缩
图片压缩thumbnailator-0.4.5.jar
Android图片压缩、加水印 http://blog.csdn.net/zivensonice/article/details/51462470
java 图片压缩 iphone拍照上传旋转问题处理,压缩工具类 首先导入jar 包,通过imgxz获取图片是否旋转属性,在调用旋转方法,旋转过来,然后在进行压缩
旋风图片批量删除添加水印专家是一款免费的图像添加水印 删除水印 转换、缩放软件。它还可以进行批量改名、裁剪、旋转,批量变换颜色深度,以及批量给图片增加文字水印。 图片压缩,缩小图片尺寸,图片旋转,...