直播开发app,Java修改图片大小尺寸图片缩放
直播开发app,Java修改图片大小尺寸图片缩放
Thumbnailator是Java的开源图像大小调整库,它使用渐进式双线性缩放。它支持JPG,BMP,JPEG,WBMP,PNG和GIF。
通过将以下Maven依赖项添加到我们的pom.xml中,将其包括在我们的项目中:
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.11</version>
</dependency>
工具类ThumbnailsUtils:
import net.coobird.thumbnailator.Thumbnails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ThumbnailsUtils{
private static final Logger logger = LoggerFactory.getLogger(ThumbnailsUtils.class);
/**
* 通过BufferedImage图片流调整图片大小
*/
public static BufferedImage resizeImageOne(BufferedImage originalImage, int targetWidth, int targetHeight) throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Thumbnails.of(originalImage)
.size(targetWidth, targetHeight)
.outputFormat("JPEG")
.outputQuality(1)
.toOutputStream(outputStream);
byte[] data = outputStream.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
return ImageIO.read(inputStream);
}
/**
* BufferedImage图片流转byte[]数组
*/
public static byte[] imageToBytes(BufferedImage bImage) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(bImage, "jpg", out);
} catch (IOException e) {
logger.error("错误信息: ", e);
}
return out.toByteArray();
}
/**
* byte[]数组转BufferedImage图片流
*/
private static BufferedImage bytesToBufferedImage(byte[] ImageByte) {
ByteArrayInputStream in = new ByteArrayInputStream(ImageByte);
BufferedImage image = null;
try {
image = ImageIO.read(in);
} catch (IOException e) {
logger.error("错误信息: ", e);
}
return image;
}
}
以上就是直播开发app,Java修改图片大小尺寸图片缩放, 更多内容欢迎关注之后的文章