1、引入包
<!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
2、工具类(流形式)
package com.zjhang.util;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Coordinate;
import net.coobird.thumbnailator.geometry.Position;
import org.apache.commons.io.FileUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
public class ThumbnailatorUtil {
public static byte[] compressImage(byte[] bytes, String imageType, float quality) {
InputStream in = null;
ByteArrayOutputStream bout = null;
try {
in = new ByteArrayInputStream(bytes);
bout = new ByteArrayOutputStream(1024);
Thumbnails.of(in).scale(1f).outputFormat(imageType).outputQuality(quality).toOutputStream(bout);
byte[] compressiondata = bout.toByteArray();
return compressiondata;
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
try {
if (in != null) {
in.close();
}
if (bout != null) {
bout.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static byte[] compressImageWithWH(byte[] bytes, int width, int height) {
InputStream in = null;
ByteArrayOutputStream bout = null;
try {
in = new ByteArrayInputStream(bytes);
bout = new ByteArrayOutputStream(1024);
Thumbnails.of(in).size(width, height).toOutputStream(bout);
return bout.toByteArray();
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
try {
if (in != null) {
in.close();
}
if (bout != null) {
bout.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static byte[] compressImageWithScale(byte[] bytes, double scale) {
InputStream in = null;
ByteArrayOutputStream bout = null;
try {
in = new ByteArrayInputStream(bytes);
bout = new ByteArrayOutputStream(1024);
Thumbnails.of(in).scale(scale).toOutputStream(bout);
return bout.toByteArray();
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
try {
if (in != null) {
in.close();
}
if (bout != null) {
bout.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static byte[] formatImage(byte[] bytes, String toformatImageType, int width, int height) {
InputStream in = null;
ByteArrayOutputStream bout = null;
try {
in = new ByteArrayInputStream(bytes);
bout = new ByteArrayOutputStream(1024);
Thumbnails.of(in).size(width, height).outputFormat(toformatImageType).toOutputStream(bout);
return bout.toByteArray();
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
try {
if (in != null) {
in.close();
}
if (bout != null) {
bout.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static byte[] cutImage(byte[] bytes, int x, int y, int x1, int y1, boolean keepAspectRatio) {
InputStream in = null;
ByteArrayOutputStream bout = null;
try {
in = new ByteArrayInputStream(bytes);
bout = new ByteArrayOutputStream(1024);
int width = x1 - x;
int height = y1 - y;
Thumbnails.of(in).sourceRegion(x, y, x1, y1).size(width, height).keepAspectRatio(keepAspectRatio).toOutputStream(bout);
return bout.toByteArray();
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
try {
if (in != null) {
in.close();
}
if (bout != null) {
bout.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static byte[] addImageWater(byte[] bytes, int width, int height, Position position, String watermark, float opacity, float quality) {
InputStream in = null;
ByteArrayOutputStream bout = null;
try {
in = new ByteArrayInputStream(bytes);
bout = new ByteArrayOutputStream(1024);
Thumbnails.of(in)
.size(width, height)
.watermark(position, ImageIO.read(new File(watermark)), opacity)
.outputQuality(quality)
.toOutputStream(bout);
return bout.toByteArray();
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
try {
if (in != null) {
in.close();
}
if (bout != null) {
bout.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static byte[] addTextWater(byte[] bytes, Position position, String waterText, double rotate, float opacity, float quality) {
InputStream in = null;
ByteArrayOutputStream bout = null;
try {
in = new ByteArrayInputStream(bytes);
bout = new ByteArrayOutputStream(1024);
BufferedImage bi = new BufferedImage(480, 160, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
bi = g.getDeviceConfiguration().createCompatibleImage(480, 160, Transparency.TRANSLUCENT);
g.dispose();
g = bi.createGraphics();
g.setFont(new Font("微软雅黑", Font.BOLD, 32));
g.setColor(new Color(0, 0, 0));
char[] data = waterText.toCharArray();
g.drawChars(data, 0, data.length, 0, 80);
g.dispose();
Thumbnails.of(in).scale(1).watermark(position, bi, opacity).outputQuality(quality).toOutputStream(bout);
return bout.toByteArray();
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
try {
if (in != null) {
in.close();
}
if (bout != null) {
bout.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static byte[] addTextWaterFullScreen(byte[] bytes, int width, int height, int intervalWidth, int intervalHeight, List<String> waterTextList, int fontSize, float opacity, float quality) {
InputStream in = null;
ByteArrayOutputStream bout = null;
try {
in = new ByteArrayInputStream(bytes);
bout = new ByteArrayOutputStream(1024);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
bi = g.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
g.dispose();
g = bi.createGraphics();
Font font = new Font("微软雅黑", Font.BOLD, fontSize);
g.setFont(font);
g.rotate(Math.toRadians(-30), 0, 0);
g.setColor(new Color(0, 0, 0));
int distance = fontSize + 12;
int size = waterTextList.size();
for (int i = 0; i < size; i++) {
char[] data = waterTextList.get(i).toCharArray();
g.drawChars(data, 0, data.length, 0, height / 2 + i * distance);
}
g.dispose();
Thumbnails.Builder<? extends InputStream> builder = Thumbnails.of(in).scale(1);
int wMod = (int) Math.ceil(width / intervalWidth);
int hMod = (int) Math.ceil(height / intervalHeight);
for (int i = 0; i <= wMod; i++) {
for (int j = 0; j <= hMod; j++) {
int x = (i) * intervalWidth - intervalWidth / 2;
int y = (j) * intervalHeight - intervalHeight / 2;
System.out.println(x + "," + y);
builder.watermark(new Coordinate(x, y), bi, opacity);
}
}
builder.outputQuality(quality).toOutputStream(bout);
return bout.toByteArray();
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
try {
if (in != null) {
in.close();
}
if (bout != null) {
bout.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static byte[] rotateImage(byte[] bytes, int width, int height, double rotate) {
InputStream in = null;
ByteArrayOutputStream bout = null;
try {
in = new ByteArrayInputStream(bytes);
bout = new ByteArrayOutputStream(1024);
Thumbnails.of(in).size(width, height).rotate(rotate).toOutputStream(bout);
return bout.toByteArray();
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
try {
if (in != null) {
in.close();
}
if (bout != null) {
bout.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
}
}