使用Thumbnailator工具需要引入thumbnailator-0.4.8.jar包,在pom中添加一下代码即可。
net.coobird
thumbnailator
0.4.8
1、创建ImageZoomService接口,添加一下代码。
import java.io.File;
import java.io.OutputStream;
import java.net.URL;
/*
* 图片缩放
* */
public interface ImageZoomService {
/**
* 指定大小进行缩放 若图片横比width小,高比height小,不变 若图片横比width小,高比height大,高缩小到height,图片比例不变
* 若图片横比width大,高比height小,横缩小到width,图片比例不变
* 若图片横比width大,高比height大,图片按比例缩小,横为width或高为height
*
* @param source
* 输入源
* @param output
* 输出源
* @param width
* 宽
* @param height
* 高
*/
public void imgThumb(String source, String output, int width, int height);
public void imgThumb(File source, String output, int width, int height);
public void imgThumb(URL source, String output, int width, int height);
public String imgThumbOutputStream(URL source, int width, int height);
/**
* 按照比例进行缩放
*
* @param source
* 输入源
* @param output
* 输出源
* @param scale
* 比例
*/
public void imgScale(String source, String output, double scale);
public void imgScale(File source, String output, double scale);
public void imgScale(URL source, String output, double scale);
public String imgScaleOutputStream(URL source, double scale);
/**
* 不按照比例,指定大小进行缩放
*
* @param source
* 输入源
* @param output
* 输出源
* @param width
* 宽
* @param height
* 高
* @param keepAspectRatio
* 默认是按照比例缩放的,值为false 时不按比例缩放
*/
public void imgNoScale(String source, String output, int width, int height, boolean keepAspectRatio);
public void imgNoScale(File source, String output, int width, int height, boolean keepAspectRatio);
public void imgNoScale(URL source, String output, int width, int height, boolean keepAspectRatio);
public String imgNoScaleOutputStream(URL source, int width, int height, boolean keepAspectRatio);
/**
* 输出到OutputStream
*
* @param source
* 输入源
* @param output
* 输出源
* @param width
* 宽
* @param height
* 高
* @return toOutputStream(流对象)
*/
public OutputStream imgOutputStream(String source, String output, int width, int height);
public OutputStream imgOutputStream(File source, String output, int width, int height);
}
2、创建类ImageZoomServiceImpl,继承ImageZoomService。
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import org.springframework.stereotype.Service;
import com.chaoxing.service.ImageZoomService;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
public class ImageZoomServiceImpl implements ImageZoomService {
public void imgThumb(String source, String output, int width, int height) {
try {
Thumbnails.of(source).size(width, height).toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public void imgThumb(File source, String output, int width, int height) {
try {
Thumbnails.of(source).size(width, height).toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public void imgThumb(URL source, String output, int width, int height) {
try {
Thumbnails.of(source).size(width, height).toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public String imgThumbOutputStream(URL source, int width, int height) {
ByteArrayOutputStream baos = null;
String binary = null;
try {
baos = new ByteArrayOutputStream();
Thumbnails.of(source).size(width, height).toOutputStream(baos);
binary = Base64.encode(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return binary;
}
public void imgScale(String source, String output, double scale) {
try {
Thumbnails.of(source).scale(scale).toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public void imgScale(File source, String output, double scale) {
try {
Thumbnails.of(source).scale(scale).toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public void imgScale(URL source, String output, double scale) {
try {
Thumbnails.of(source).scale(scale).toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public String imgScaleOutputStream(URL source, double scale) {
ByteArrayOutputStream baos = null;
String binary = null;
try {
baos = new ByteArrayOutputStream();
Thumbnails.of(source).scale(scale).toOutputStream(baos);
binary = Base64.encode(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return binary;
}
public void imgNoScale(String source, String output, int width, int height, boolean keepAspectRatio) {
try {
Thumbnails.of(source).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public void imgNoScale(File source, String output, int width, int height, boolean keepAspectRatio) {
try {
Thumbnails.of(source).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public void imgNoScale(URL source, String output, int width, int height, boolean keepAspectRatio) {
try {
Thumbnails.of(source).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public String imgNoScaleOutputStream(URL source, int width, int height, boolean keepAspectRatio) {
ByteArrayOutputStream baos = null;
String binary = null;
try {
baos = new ByteArrayOutputStream();
Thumbnails.of(source).size(width, height).keepAspectRatio(keepAspectRatio).toOutputStream(baos);
binary = Base64.encode(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return binary;
}
public OutputStream imgOutputStream(String source, String output, int width, int height) {
OutputStream os = null;
try {
os = new FileOutputStream(output);
Thumbnails.of(source).scale(width, height).toOutputStream(os);
} catch (IOException e) {
e.printStackTrace();
}
return os;
}
public OutputStream imgOutputStream(File source, String output, int width, int height) {
OutputStream os = null;
try {
os = new FileOutputStream(output);
Thumbnails.of(source).scale(width, height).toOutputStream(os);
} catch (IOException e) {
e.printStackTrace();
}
return os;
}
}
1、创建ImageWatermarkService接口
import java.io.File;
import java.net.URL;
import net.coobird.thumbnailator.geometry.Position;
/*
* 图片水印
* */
public interface ImageWatermarkService {
/**
* 水印
*
* @param source
* 输入源
* @param output
* 输入源
* @param width
* 宽
* @param height
* 高
* @param position
* 水印位置 Positions.BOTTOM_RIGHT o.5f
* @param watermark
* 水印图片地址
* @param transparency
* 透明度 0.5f
* @param quality
* 图片质量 0.8f
*/
public void imgWatermark(String source, String output, int width, int height, Position position, String watermark,
float transparency, float quality);
public void imgWatermark(File source, String output, int width, int height, Position position, String watermark,
float transparency, float quality);
public void imgWatermark(URL source, String output, int width, int height, Position position, String watermark,
float transparency, float quality);
public String imgWatermarkOutputStream(URL source, int width, int height, Position position, String watermark,
float transparency, float quality);
}
2、创建ImageWatermarkServiceImpl类,继承ImageWatermarkService
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import org.springframework.stereotype.Service;
import com.chaoxing.service.ImageWatermarkService;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Position;
public class ImageWatermarkServiceImpl implements ImageWatermarkService {
public void imgWatermark(String source, String output, int width, int height, Position position, String watermark,
float transparency, float quality) {
try {
Thumbnails.of(source).size(width, height)
.watermark(position, ImageIO.read(new File(watermark)), transparency).outputQuality(0.8f)
.toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
;
}
public void imgWatermark(File source, String output, int width, int height, Position position, String watermark,
float transparency, float quality) {
try {
Thumbnails.of(source).size(width, height)
.watermark(position, ImageIO.read(new File(watermark)), transparency).outputQuality(0.8f)
.toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public void imgWatermark(URL source, String output, int width, int height, Position position, String watermark,
float transparency, float quality) {
try {
Thumbnails.of(source).size(width, height)
.watermark(position, ImageIO.read(new File(watermark)), transparency).outputQuality(0.8f)
.toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public String imgWatermarkOutputStream(URL source, int width, int height, Position position, String watermark,
float transparency, float quality) {
ByteArrayOutputStream baos = null;
String binary = null;
try {
baos = new ByteArrayOutputStream();
Thumbnails.of(source).size(width, height)
.watermark(position, ImageIO.read(new File(watermark)), transparency).outputQuality(0.8f)
.toOutputStream(baos);
binary = Base64.encode(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return binary;
}
}
1、创建接口ImageRoteService
/*
* 图片旋转
* */
public interface ImageRoteService {
/**
* 旋转 ,正数:顺时针 负数:逆时针
*
* @param source
* 输入源
* @param output
* 输出源
* @param width
* 宽
* @param height
* 高
* @param rotate
* 角度,正数顺时针 负数逆时针
*/
public void imgRotate(String source, String output, int width, int height, double rotate);
public void imgRotate(File source, String output, int width, int height, double rotate);
public void imgRotate(URL source, String output, int width, int height, double rotate);
public String imgRotateOutputStream(URL source, int width, int height, double rotate);
}
2、创建类ImageRoteServiceImpl,并继承ImageRoteService
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import org.springframework.stereotype.Service;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import net.coobird.thumbnailator.Thumbnails;
public class ImageRoteServiceImpl implements com.chaoxing.service.ImageRoteService {
public void imgRotate(String source, String output, int width, int height, double rotate) {
try {
Thumbnails.of(source).size(width, height).rotate(rotate).toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public void imgRotate(File source, String output, int width, int height, double rotate) {
try {
Thumbnails.of(source).size(width, height).rotate(rotate).toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public void imgRotate(URL source, String output, int width, int height, double rotate) {
try {
Thumbnails.of(source).size(width, height).rotate(rotate).toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public String imgRotateOutputStream(URL source, int width, int height, double rotate) {
ByteArrayOutputStream baos = null;
String binary = null;
try {
baos = new ByteArrayOutputStream();
Thumbnails.of(source).size(width, height).rotate(rotate).toOutputStream(baos);
binary = Base64.encode(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return binary;
}
}
1、创建接口ImageFormatService
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
/*
* 图片格式转换
* */
public interface ImageFormatService {
/**
* 转化图像格式
*
* @param source
* 输入源
* @param output
* 输出源
* @param width
* 宽
* @param height
* 高
* @param format
* 图片类型,gif、png、jpg
*/
public void imgFormat(String source, String output, int width, int height, String format);
public void imgFormat(File source, String output, int width, int height, String format);
public void imgFormat(URL source, String output, int width, int height, String format);
public String imgFormatOutputStream(URL source, int width, int height, String format);
/**
* 输出到BufferedImage
*
* @param source
* 输入源
* @param output
* 输出源
* @param width
* 宽
* @param height
* 高
* @param format
* 图片类型,gif、png、jpg
* @return BufferedImage
*/
public BufferedImage imgBufferedImage(String source, String output, int width, int height, String format);
public BufferedImage imgBufferedImage(File source, String output, int width, int height, String format);
public BufferedImage imgBufferedImage(URL source, String output, int width, int height, String format);
}
2、创建ImageFormatServiceImpl类,并继承ImageFormatService
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import org.springframework.stereotype.Service;
import com.chaoxing.service.ImageFormatService;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import net.coobird.thumbnailator.Thumbnails;
public class ImageFormatServiceImpl implements ImageFormatService {
public void imgFormat(String source, String output, int width, int height, String format) {
try {
Thumbnails.of(source).size(width, height).outputFormat(format).toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public void imgFormat(File source, String output, int width, int height, String format) {
try {
Thumbnails.of(source).size(width, height).outputFormat(format).toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public void imgFormat(URL source, String output, int width, int height, String format) {
try {
Thumbnails.of(source).size(width, height).outputFormat(format).toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public String imgFormatOutputStream(URL source, int width, int height, String format) {
ByteArrayOutputStream baos = null;
String binary = null;
try {
baos = new ByteArrayOutputStream();
Thumbnails.of(source).size(width, height).outputFormat(format).toOutputStream(baos);
binary = Base64.encode(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return binary;
}
public BufferedImage imgBufferedImage(String source, String output, int width, int height, String format) {
BufferedImage buff = null;
try {
buff = Thumbnails.of(source).size(width, height).asBufferedImage();
ImageIO.write(buff, format, new File(output));
} catch (IOException e) {
e.printStackTrace();
}
return buff;
}
public BufferedImage imgBufferedImage(File source, String output, int width, int height, String format) {
BufferedImage buff = null;
try {
buff = Thumbnails.of(source).size(width, height).asBufferedImage();
ImageIO.write(buff, format, new File(output));
} catch (IOException e) {
e.printStackTrace();
}
return buff;
}
public BufferedImage imgBufferedImage(URL source, String output, int width, int height, String format) {
BufferedImage buff = null;
try {
buff = Thumbnails.of(source).size(width, height).asBufferedImage();
ImageIO.write(buff, format, new File(output));
} catch (IOException e) {
e.printStackTrace();
}
return buff;
}
}
1、创建接口ImageCutService
import java.io.File;
import java.net.URL;
import net.coobird.thumbnailator.geometry.Position;
/*
* 图片剪裁
* */
public interface ImageCutService {
/**
* 裁剪图片
*
* @param source
* 输入源
* @param output
* 输出源
* @param position
* 裁剪位置
* @param x
* 裁剪区域x
* @param y
* 裁剪区域y
* @param width
* 宽
* @param height
* 高
* @param keepAspectRatio
* 默认是按照比例缩放的,值为false 时不按比例缩放
*/
public void imgSourceRegion(String source, String output, Position position, int x, int y, int width, int height,
boolean keepAspectRatio);
public void imgSourceRegion(File source, String output, Position position, int x, int y, int width, int height,
boolean keepAspectRatio);
public void imgSourceRegion(URL source, String output, Position position, int x, int y, int width, int height,
boolean keepAspectRatio);
public String imgSourceRegionOutputStream(URL source, Position position, int x, int y, int width, int height,
boolean keepAspectRatio);
/**
* 按坐标裁剪
*
* @param source
* 输入源
* @param output
* 输出源
* @param x
* 起始x坐标
* @param y
* 起始y坐标
* @param x1
* 结束x坐标
* @param y1
* 结束y坐标
* @param width
* 宽
* @param height
* 高
* @param keepAspectRatio
* 默认是按照比例缩放的,值为false 时不按比例缩放
*/
public void imgSourceRegion(String source, String output, int x, int y, int x1, int y1, int width, int height,
boolean keepAspectRatio);
public void imgSourceRegion(File source, String output, int x, int y, int x1, int y1, int width, int height,
boolean keepAspectRatio);
public void imgSourceRegion(URL source, String output, int x, int y, int x1, int y1, int width, int height,
boolean keepAspectRatio);
public String imgSourceRegionOutputStream(URL source, int x, int y, int x1, int y1, int width, int height,
boolean keepAspectRatio);
}
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import org.springframework.stereotype.Service;
import com.chaoxing.service.ImageCutService;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Position;
public class ImageCutServiceImpl implements ImageCutService {
public void imgSourceRegion(String source, String output, Position position, int x, int y, int width, int height,
boolean keepAspectRatio) {
try {
Thumbnails.of(source).sourceRegion(position, x, y).size(width, height).keepAspectRatio(keepAspectRatio)
.toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public void imgSourceRegion(File source, String output, Position position, int x, int y, int width, int height,
boolean keepAspectRatio) {
try {
Thumbnails.of(source).sourceRegion(position, x, y).size(width, height).keepAspectRatio(keepAspectRatio)
.toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public void imgSourceRegion(URL source, String output, Position position, int x, int y, int width, int height,
boolean keepAspectRatio) {
try {
Thumbnails.of(source).sourceRegion(position, x, y).size(width, height).keepAspectRatio(keepAspectRatio)
.toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public String imgSourceRegionOutputStream(URL source, Position position, int x, int y, int width, int height, boolean keepAspectRatio) {
ByteArrayOutputStream baos = null;
String binary = null;
try {
baos = new ByteArrayOutputStream();
Thumbnails.of(source).sourceRegion(position, x, y).size(width, height).keepAspectRatio(keepAspectRatio).toOutputStream(baos);
binary = Base64.encode(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return binary;
}
public void imgSourceRegion(String source, String output, int x, int y, int x1, int y1, int width, int height,
boolean keepAspectRatio) {
try {
Thumbnails.of(source).sourceRegion(x, y, x1, y1).size(width, height).keepAspectRatio(keepAspectRatio)
.toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public void imgSourceRegion(File source, String output, int x, int y, int x1, int y1, int width, int height,
boolean keepAspectRatio) {
try {
Thumbnails.of(source).sourceRegion(x, y, x1, y1).size(width, height).keepAspectRatio(keepAspectRatio)
.toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public void imgSourceRegion(URL source, String output, int x, int y, int x1, int y1, int width, int height,
boolean keepAspectRatio) {
try {
Thumbnails.of(source).sourceRegion(x, y, x1, y1).size(width, height).keepAspectRatio(keepAspectRatio)
.toFile(output);
} catch (IOException e) {
e.printStackTrace();
}
}
public String imgSourceRegionOutputStream(URL source, int x, int y, int x1, int y1, int width, int height,
boolean keepAspectRatio) {
ByteArrayOutputStream baos = null;
String binary = null;
try {
baos = new ByteArrayOutputStream();
Thumbnails.of(source).sourceRegion(x, y, x1, y1).size(width, height).keepAspectRatio(keepAspectRatio)
.toOutputStream(baos);
binary = Base64.encode(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return binary;
}
}