Thumbnailator 是一个Google的图片处理开源Java类库
支持的处理操作:图片缩放,区域裁剪,水印,旋转,保持比例。
Thumbnailator官网:http://code.google.com/p/thumbnailator/
maven配置
<dependency>
<groupId>net.coobirdgroupId>
<artifactId>thumbnailatorartifactId>
<version>0.4.8version>
dependency>
public static void main(String[] args) {
//创建一个新的outputStream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
FileOutputStream outputStream1 = null;
try {
File file =new File("路径");
//用ImageIO将文件读取为BufferedImage格式
//BufferedImage使用双缓冲技术,将生成合适的缓冲区
//具体可参考http://blog.csdn.net/novaless/article/details/5954933
BufferedImage image = ImageIO.read(file);
//使用Thumbnails方法
//scale(x) x>1 为放大 x<1为缩小
//toOutputStream转为流(toFile转为file文件,但是会生成一个临时文件,影响性能)
Thumbnails.of(image).scale(0.5f).outputFormat("jpg").toOutputStream(outputStream);
//写入文件
outputStream1 = new FileOutputStream(new File("C:\\Users\\PC\\Desktop\\out.jpg"));
outputStream.writeTo(outputStream1);
} catch (IOException e) {
e.printStackTrace();
}finally {
IOUtils.closeQuietly(outputStream);
IOUtils.closeQuietly(outputStream);
}
}
public static void main(String[] args) {=
try {
File file =new File("C:\\Users\\PC\\Desktop\\timg.jpg");
//用ImageIO将文件读取为BufferedImage格式
BufferedImage image = ImageIO.read(file);
//读取图片高宽度
int width = (image.getWidth(null));
int height = (image.getHeight(null));
//新建一个新的BufferedImage对象
BufferedImage newImage = new BufferedImage(width / 2,height / 2,BufferedImage.TYPE_INT_RGB);
//将原有对象以 1/2 宽度 ,1/2高度进行缩放 newImage.getGraphics().drawImage(image.getScaledInstance(width / 2,height / 2,BufferedImage.TYPE_INT_RGB), 0, 0, null);
//新建文件
FileOutputStream outputStream = new FileOutputStream(new File("C:\\Users\\PC\\Desktop\\out.jpg"));
//读出
ImageIO.write(newImage,"jpg",outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
支持操作:JPEG解码增强(无内存泄漏,支持CMYK),GIF动画的读取和保存,图像颜色空间转换,图像高质量缩略,图像打水印文字
源码地址:https://github.com/alibaba/simpleimage
maven配置:
<dependency>
<groupId>com.alibabagroupId>
<artifactId>simpleimageartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>javax.mediagroupId>
<artifactId>jai-coreartifactId>
<version>1.1.3version>
dependency>
代码
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
ImageRender imageRender = null;
try {
//读取文件
inputStream = new FileInputStream(new File("C:\\Users\\PC\\Desktop\\timg.png"));
//转为BufferedImage,获得宽高度
BufferedImage image = ImageIO.read(inputStream);
int width = image.getWidth();
int height = image.getHeight();
//将输入流放入ImageRender
ImageRender inImageRender = new ReadRender(inputStream);
//设置缩放参数
ScaleParameter scaleParameter = new ScaleParameter();
scaleParameter.setMaxHeight(height / 2);
scaleParameter.setMaxWidth(width / 2);
ImageRender scaleImageRender = new ScaleRender(inImageRender, scaleParameter);
outputStream = new FileOutputStream(new File("C:\\Users\\PC\\Desktop\\out.png"));
//写出文件,文件为Jpg格式
imageRender = new WriteRender(scaleImageRender, outputStream,ImageFormat.PNG);
imageRender.render();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SimpleImageException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
if (imageRender != null) {
try {
//释放simpleImage的内部资源
imageRender.dispose();
} catch (SimpleImageException ignore) {
}
}
}
mageJ能够显示,编辑,分析,处理,保存,打印8位,16位,32位的图片, 支持TIFF, PNG, GIF, JPEG, BMP, DICOM, FITS等多种格式。ImageJ支持图像栈功能,即在一个窗口里以多线程的形式层叠多个图像, 并行处理。只要内存允许,ImageJ能打开任意多的图像进行处理。除了基本的图像操作, 比如缩放,旋转, 扭曲, 平滑处理外,ImageJ还能进行图片的区域和像素统计, 间距,角度计算, 能创建柱状图和剖面图,进行傅里叶变换。
maven配置:
<dependency>
<groupId>gov.nih.imagejgroupId>
<artifactId>imagejartifactId>
<version>1.46version>
dependency>
代码:
public static void main(String[] args) {
try {
// File file = new File("C:\\Users\\PC\\Desktop\\timg.png");
ImagePlus imagePlus = IJ.openImage("C:\\Users\\PC\\Desktop\\timg2.png");
//用代码打开图片O(∩_∩)O哈哈哈~
//imagePlus.show();
//获取宽高度
int width = imagePlus.getWidth();
int height = imagePlus.getHeight();
ImageProcessor imageProcessor = imagePlus.getProcessor();
//设置为可以进行编辑
imageProcessor.setInterpolate(true);
//重置大小
ImageProcessor outImageProcessor = imageProcessor.resize(width / 10 ,height / 10);
imagePlus.setProcessor(imagePlus.getTitle(),outImageProcessor);
//更新原本图像
imagePlus.updateAndDraw();
//写入本地
IJ.save(imagePlus,"C:\\Users\\PC\\Desktop\\timg3.png");
}catch (Exception e){
e.getStackTrace();
}
}
java-image-scaling 可用来生成高质量的图像缩略图。
maven配置:
<dependency>
<groupId>com.mortennobelgroupId>
<artifactId>java-image-scalingartifactId>
<version>0.8.6version>
dependency>
代码:
public static void main(String[] args) {
FileOutputStream fileOutputStream = null;
try {
File file = new File("C:\\Users\\PC\\Desktop\\timg.png");
BufferedImage bufferedImage = ImageIO.read(file);
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
//true -- 生成低品质缩略图 (不过我试过false,好像效果一样/(ㄒoㄒ)/~~)
ResampleOp resampleOp = new ResampleOp(DimensionConstrain.createMaxDimension(width/ 2 , height / 2 ,true));
BufferedImage scaleBufferedImage = resampleOp.filter(bufferedImage,null);
fileOutputStream = new FileOutputStream(new File("C:\\Users\\PC\\Desktop\\timg4.jpg"));
//写出
ImageIO.write(scaleBufferedImage,"jpg",fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
好了,就写到这了,还有很多方法去实现这个功能,可以参考:http://blog.csdn.net/it_man/article/details/7606218