Thumbnailator图像处理
2016-06-16 20:56
405 查看
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.hand.emp.test;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.coobird.thumbnailator.Thumbnails;
import org.junit.Test;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.builders.BufferedImageBuilder;
import net.coobird.thumbnailator.geometry.Positions;
import net.coobird.thumbnailator.Thumbnails.Builder;
import java.math.BigDecimal;
/**
*
* @author JP
*/
public class TestThumbnailator {
String outputBasePth = "D:\\pictures\\thumbnail\\";
String resourceFilePath = "D:\\pictures\\yu1.jpg";
// @Test
public void TestChangeFormat() {
try {
Thumbnails.of(resourceFilePath)
.scale(0.5f)//there must be size or scale
.outputFormat("png")
.toFile(new File(outputBasePth + "PicFormat1.png"));// .toFile(new File(dicString + "t1.png"));
//Allow no suffix
//if the name of the file is same,the new one will cover the old one
} catch (IOException ex) {
Logger.getLogger(TestThumbnailator.class.getName()).log(Level.SEVERE, null, ex);
}
}
// @Test
public void TestChangeSize() {
try {
Thumbnails.of(resourceFilePath)
.forceSize(800, 300)
.toFile(new File(outputBasePth + "PicSize1.png"));
/**
* the picture will be stretched if you want get a new one which can
* be the size you need and wouldn`t be stretched, you should
* computing it`s own scale -->can call TestChangeSizeNotStrexched
*/
} catch (IOException ex) {
Logger.getLogger(TestThumbnailator.class.getName()).log(Level.SEVERE, null, ex);
}
}
// @Test
public void TestChangeSizeNotStrexched() {
String outputPath = outputBasePth + "Notstretched1";
int target_width = 1000;
int target_height = 2000;
String target_suffix = "jpg";
cover(resourceFilePath, outputPath, target_width, target_height, target_suffix);
}
// @Test
public void TestWaterMark() {
try {
//给图片加水印,watermark(position,waterPictureFile,透明度)
Thumbnails.of(resourceFilePath).scale(2f)
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("D:\\pictures\\timg.jpg")), 0.6f)
.outputQuality(0.8f).toFile(outputBasePth+"Picwatermark.jpg");
} catch (IOException ex) {
Logger.getLogger(TestThumbnailator.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Test
public void TestRotate(){
try {
//rotate picture
Thumbnails.of(resourceFilePath).scale(2f)
.rotate(90.0)
.outputQuality(0.8f).toFile(outputBasePth+"PicRotate.jpg");
} catch (IOException ex) {
Logger.getLogger(TestThumbnailator.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void cover(String sourceFile, String targetFile, int targetWidth, int targetHeight, String target_suffix) {
File fromFile = new File(sourceFile);
BufferedImage srcImage = null;
int s_height = 0;
int s_width = 0;
try {
srcImage = ImageIO.read(fromFile);
// 获取源文件的宽高
s_height = srcImage.getHeight();
s_width = srcImage.getWidth();
System.out.println(s_height + "");
System.out.println(s_width + "");
} catch (IOException e1) {
e1.printStackTrace();
}
// 判断是否需要压缩
if (targetWidth != s_width || targetHeight != s_height) {// 需要压缩
BigDecimal n = new BigDecimal(s_height).divide(new BigDecimal(s_width), 10, BigDecimal.ROUND_UP);// 源图片文件高/宽 的比例
BigDecimal m = new BigDecimal(targetHeight).divide(new BigDecimal(targetWidth), 10, BigDecimal.ROUND_UP);// 目标图片文件高/宽 的比例
System.out.println(n + "");
System.out.println(m + "");
int flag = n.compareTo(m); //比较源图片文件和目标图片 的高/宽 比
try {
if (flag == 0) {// n == m,高/宽 比一致, 无需裁减, 直接压缩
System.out.println("flag: " + flag);
Thumbnails.of(sourceFile).forceSize(targetWidth, targetHeight).outputFormat(target_suffix).toFile(new File(targetFile));
} else { // 高/宽 比不一致,需要裁减
if (flag > 0) {// 高的比例 > 宽的比例,以宽的比例进行缩放, 需要裁减高
srcImage = Thumbnails.of(sourceFile).width(targetWidth).asBufferedImage();//以宽的比例进行缩放
} else if (flag < 0) {// n < m 高的比例 < 宽的比例,以高的比例进行缩放, 需要裁减宽
srcImage = Thumbnails.of(sourceFile).height(targetHeight).asBufferedImage();//以高的比例进行缩放
}
// 居中裁减
Builder<BufferedImage> builder = Thumbnails.of(srcImage)
.sourceRegion(Positions.CENTER, targetWidth, targetHeight).size(targetWidth, targetHeight);
builder.outputFormat(target_suffix).toFile(new File(targetFile));
}
} catch (IOException ex) {
Logger.getLogger(TestThumbnailator.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.hand.emp.test;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.coobird.thumbnailator.Thumbnails;
import org.junit.Test;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.builders.BufferedImageBuilder;
import net.coobird.thumbnailator.geometry.Positions;
import net.coobird.thumbnailator.Thumbnails.Builder;
import java.math.BigDecimal;
/**
*
* @author JP
*/
public class TestThumbnailator {
String outputBasePth = "D:\\pictures\\thumbnail\\";
String resourceFilePath = "D:\\pictures\\yu1.jpg";
// @Test
public void TestChangeFormat() {
try {
Thumbnails.of(resourceFilePath)
.scale(0.5f)//there must be size or scale
.outputFormat("png")
.toFile(new File(outputBasePth + "PicFormat1.png"));// .toFile(new File(dicString + "t1.png"));
//Allow no suffix
//if the name of the file is same,the new one will cover the old one
} catch (IOException ex) {
Logger.getLogger(TestThumbnailator.class.getName()).log(Level.SEVERE, null, ex);
}
}
// @Test
public void TestChangeSize() {
try {
Thumbnails.of(resourceFilePath)
.forceSize(800, 300)
.toFile(new File(outputBasePth + "PicSize1.png"));
/**
* the picture will be stretched if you want get a new one which can
* be the size you need and wouldn`t be stretched, you should
* computing it`s own scale -->can call TestChangeSizeNotStrexched
*/
} catch (IOException ex) {
Logger.getLogger(TestThumbnailator.class.getName()).log(Level.SEVERE, null, ex);
}
}
// @Test
public void TestChangeSizeNotStrexched() {
String outputPath = outputBasePth + "Notstretched1";
int target_width = 1000;
int target_height = 2000;
String target_suffix = "jpg";
cover(resourceFilePath, outputPath, target_width, target_height, target_suffix);
}
// @Test
public void TestWaterMark() {
try {
//给图片加水印,watermark(position,waterPictureFile,透明度)
Thumbnails.of(resourceFilePath).scale(2f)
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("D:\\pictures\\timg.jpg")), 0.6f)
.outputQuality(0.8f).toFile(outputBasePth+"Picwatermark.jpg");
} catch (IOException ex) {
Logger.getLogger(TestThumbnailator.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Test
public void TestRotate(){
try {
//rotate picture
Thumbnails.of(resourceFilePath).scale(2f)
.rotate(90.0)
.outputQuality(0.8f).toFile(outputBasePth+"PicRotate.jpg");
} catch (IOException ex) {
Logger.getLogger(TestThumbnailator.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void cover(String sourceFile, String targetFile, int targetWidth, int targetHeight, String target_suffix) {
File fromFile = new File(sourceFile);
BufferedImage srcImage = null;
int s_height = 0;
int s_width = 0;
try {
srcImage = ImageIO.read(fromFile);
// 获取源文件的宽高
s_height = srcImage.getHeight();
s_width = srcImage.getWidth();
System.out.println(s_height + "");
System.out.println(s_width + "");
} catch (IOException e1) {
e1.printStackTrace();
}
// 判断是否需要压缩
if (targetWidth != s_width || targetHeight != s_height) {// 需要压缩
BigDecimal n = new BigDecimal(s_height).divide(new BigDecimal(s_width), 10, BigDecimal.ROUND_UP);// 源图片文件高/宽 的比例
BigDecimal m = new BigDecimal(targetHeight).divide(new BigDecimal(targetWidth), 10, BigDecimal.ROUND_UP);// 目标图片文件高/宽 的比例
System.out.println(n + "");
System.out.println(m + "");
int flag = n.compareTo(m); //比较源图片文件和目标图片 的高/宽 比
try {
if (flag == 0) {// n == m,高/宽 比一致, 无需裁减, 直接压缩
System.out.println("flag: " + flag);
Thumbnails.of(sourceFile).forceSize(targetWidth, targetHeight).outputFormat(target_suffix).toFile(new File(targetFile));
} else { // 高/宽 比不一致,需要裁减
if (flag > 0) {// 高的比例 > 宽的比例,以宽的比例进行缩放, 需要裁减高
srcImage = Thumbnails.of(sourceFile).width(targetWidth).asBufferedImage();//以宽的比例进行缩放
} else if (flag < 0) {// n < m 高的比例 < 宽的比例,以高的比例进行缩放, 需要裁减宽
srcImage = Thumbnails.of(sourceFile).height(targetHeight).asBufferedImage();//以高的比例进行缩放
}
// 居中裁减
Builder<BufferedImage> builder = Thumbnails.of(srcImage)
.sourceRegion(Positions.CENTER, targetWidth, targetHeight).size(targetWidth, targetHeight);
builder.outputFormat(target_suffix).toFile(new File(targetFile));
}
} catch (IOException ex) {
Logger.getLogger(TestThumbnailator.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
相关文章推荐
- php实现批量压缩图片文件大小的脚本
- angularjs客户端实现压缩图片文件并上传实例
- java实现上传图片并压缩图片大小功能
- iOS实现压缩图片上传功能
- Java缩略图生成库之Thumbnailator应用说明
- fuelphp学习和知识积累
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- servlet中图像上传以及缩小
- 图片裁剪缩略处理小工具类
- H5 压缩图片上传(pc端适用)支持png/jpg格式(其他格式都会转为png)
- PHP压缩上传图片
- java 使用Thumbnailator图片处理包装类
- Android—压缩图片,减少内存
- 八、TexturePacker命令行压缩图片
- Android 高清加载巨图方案 拒绝压缩图片
- Android高效压缩图片不失真的方法总结
- Android如何实现压缩图片后再上传至服务器
- 压缩图片尺寸
- Java图片上传压缩处理
- 根据路径获取图片,缩放图片