在我们日常开发的过程中多多少少都会遇到图片的处理,比如:图片旋转,绘制,等比例缩放,剪裁等!在新的工作遇到这个问题,通过网上的经验整理如此工具,提供给大家使用!
图片旋转
在我们在业务开发过程中会用到图片上传功能,在上传后会发现上传的图片翻转或者旋转了,主要原因如下:
- 我们通过
metadata-extractor
来实现获取图片的旋转角。
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>2.9.1</version>
</dependency>
可以通过 阿里中央仓库获取!
- 通过下面就可以获取到对应的旋转角
/**
* 获取图片旋转角度
*
* @param file 上传图片
* @return
*/
public static int getAngle(MultipartFile file) throws Exception {
Metadata metadata = ImageMetadataReader.readMetadata(file.getInputStream());
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
if ("Orientation".equals(tag.getTagName())) {
String orientation = tag.getDescription();
if (orientation.contains("90")) {
return 90;
} else if (orientation.contains("180")) {
return 180;
} else if (orientation.contains("270")) {
return 270;
}
}
}
}
return 0;
}
- 通过
BufferedImage
,AffineTransformOp
来实现图片旋转
BufferedImage srcImage = ImageIO.read(new File("input.jpg"));
double radian = Math.toRadians(30); // 旋转角度为 30 度
AffineTransform transform = new AffineTransform();
transform.rotate(radian, srcImage.getWidth() / 2, srcImage.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
BufferedImage destImage = op.filter(srcImage, null);
图片等比例缩放
- 话不多说直接上源码
/**
* 等比例缩放
*
* @param bufferedImage 图片信息
* @param height 转换长
* @param width 转换高
* @param format 图片类型
* @return InputStream
*/
public static InputStream scaleBuffredImg(BufferedImage bufferedImage, int width, int height, String format) {
try {
int localWidth = bufferedImage.getWidth();
int localHeight = bufferedImage.getHeight();
if (localHeight == height && width == localWidth) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
//输入输出流
ImageIO.write(bufferedImage, format, os);
//这里处理上传到oss上
InputStream stream = new ByteArrayInputStream(os.toByteArray());
return stream;
}
double percentWidth = FinanceUtil.divide(Double.valueOf(String.valueOf(localWidth)), Double.valueOf(String.valueOf(width)), 3);
double percentHeight = FinanceUtil.divide(Double.valueOf(String.valueOf(localHeight)), Double.valueOf(String.valueOf(height)), 3);
//控制缩放大小
double rate = percentWidth < percentHeight ? percentWidth : percentHeight;
boolean widthRate = false;
boolean heightRate = false;
if (rate == percentWidth) {
widthRate = true;
}
if (rate == percentHeight) {
heightRate = true;
}
int newWidth = (int) (localWidth / rate);
if (newWidth < width) {
newWidth = width;
}
int newHeight = (int) (localHeight / rate);
if (newHeight < height) {
newHeight = height;
}
//压缩之后的长度和宽度
BufferedImage finalImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
finalImage.getGraphics().drawImage(bufferedImage.getScaledInstance(newWidth, newHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
BufferedImage bf = new BufferedImage(width, height, finalImage.getType());
int sx1 = 0;
if (newWidth > width) {
sx1 = (newWidth - width) / 2;
}
int sy2 = height;
int sx2 = sx1 + width;
if (sx2 > newWidth) {
sx2 = width;
}
bf.getGraphics().drawImage(finalImage, 0, 0, width, height, sx1, 0, sx2, sy2, null);
ByteArrayOutputStream os = new ByteArrayOutputStream();
//输入输出流
ImageIO.write(bf, format, os);
//这里处理上传到oss上
InputStream stream = new ByteArrayInputStream(os.toByteArray());
return stream;
} catch (Exception e) {
return null;
}
}
图片合并
- 大家可以使用开源组件
<dependency>
<groupId>com.combiner</groupId>
<artifactId>image-combiner</artifactId>
<version>${image.version}</version>
</dependency>
此组件提供了多种图片处理方式
开源项目工具包
具体实现代码可以从下面
gitee
项目地址获取
https://gitee.com/nga-sword/adore-java-util.git
实现代码类
ScaleImgUtil.java
MAVEN
引用方式
<dependency>
<groupId>com.adore.util</groupId>
<artifactId>adore-java-util</artifactId>
<version>1.0.7</version>
</dependency>
注意:此java
工具包包含多个基础使用工具,可能引用与项目中maven
引用版本包冲突,根据具体做移除处理!
图片处理效果
-
原图
-
等比例缩放并裁减