Python 官方文档:入门教程 => 点击学习
本文实例为大家分享了Java实现图片比率缩放的具体代码,供大家参考,具体内容如下 通过Thumbnails实现图片缩放 需要导入pom依赖,可以到中央仓库获取最小的工具包 <d
本文实例为大家分享了Java实现图片比率缩放的具体代码,供大家参考,具体内容如下
通过Thumbnails实现图片缩放
需要导入pom依赖,可以到中央仓库获取最小的工具包
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
//读取图片
BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(out.toByteArray()));
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
Thumbnails.of(bufferedImage).size(750,1344).outputFORMat("png").toOutputStream(out2);//缩放图片
InitImage("缩放图", out2.toByteArray());//显示图片
java API实现图片缩放
调用方法
InitImage("自定义压缩图", zoomBySize(750,1334,bufferedImage,"png"));//调用方法
具体方法实现1
public static byte[] zoomBySize(int width, int height, BufferedImage img, String ext) throws IOException {
//横向图
if (img.getWidth() >= img.getHeight()) {
double ratio = CalculateZoomRatio(width, img.getWidth());
//获取压缩对象
BufferedImage newbufferedImage = zoomByScale(ratio, img, ext);
//当图片大于图片压缩高时 再次缩放
if (newbufferedImage.getHeight() > height) {
ratio = CalculateZoomRatio(height, newbufferedImage.getHeight());
newbufferedImage = zoomByScale(ratio, img, ext);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(newbufferedImage, ext, out);
return out.toByteArray();
}
//纵向图
if (img.getWidth() < img.getHeight()) {
double ratio = CalculateZoomRatio(height, img.getHeight());
//获取压缩对象
BufferedImage newbufferedImage = zoomByScale(ratio, img, ext);
//当图片宽大于图片压缩宽时 再次缩放
if (newbufferedImage.getHeight() > height) {
ratio = CalculateZoomRatio(width, newbufferedImage.getWidth());
newbufferedImage = zoomByScale(ratio, img, ext);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(newbufferedImage, ext, out);
return out.toByteArray();
}
//以上都不符合时 强制缩放
Image _img = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.drawImage(_img, 0, 0, null);
graphics.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, ext, out);
return out.toByteArray();
}
public static BufferedImage zoomByScale(double scale, BufferedImage img, String ext) throws IOException {
//获取缩放后的长和宽
int _width = (int) (scale * img.getWidth());
int _height = (int) (scale * img.getHeight());
//获取缩放后的Image对象
Image _img = img.getScaledInstance(_width, _height, Image.SCALE_DEFAULT);
//新建一个和Image对象相同大小的画布
BufferedImage image = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
//获取画笔
Graphics2D graphics = image.createGraphics();
//将Image对象画在画布上,最后一个参数,ImageObserver:接收有关 Image 信息通知的异步更新接口,没用到直接传空
graphics.drawImage(_img, 0, 0, null);
//释放资源
graphics.dispose();
return image;
}
private static double CalculateZoomRatio(int divisor, int dividend) {
return BigDecimal.valueOf(divisor).divide(BigDecimal.valueOf(dividend), 6, BigDecimal.ROUND_HALF_UP).doubleValue();
}
实现方法2
public static byte[] zoomByScale(int width, int height, BufferedImage img, String ext) throws IOException {
//横向图
if (img.getWidth() >= img.getHeight()) {
double ratio = CalculateZoomRatio(width, img.getWidth());
//获取压缩对象
BufferedImage newbufferedImage = zoomByScale(ratio, img, ext);
//当图片大于图片压缩高时 再次缩放
if (newbufferedImage.getHeight() > height) {
ratio = CalculateZoomRatio(height, newbufferedImage.getHeight());
newbufferedImage = zoomByScale(ratio, img, ext);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(newbufferedImage, ext, out);
return out.toByteArray();
}
//纵向图
if (img.getWidth() < img.getHeight()) {
double ratio = CalculateZoomRatio(height, img.getHeight());
//获取压缩对象
BufferedImage newbufferedImage = zoomByScale(ratio, img, ext);
//当图片宽大于图片压缩宽时 再次缩放
if (newbufferedImage.getHeight() > height) {
ratio = CalculateZoomRatio(width, newbufferedImage.getWidth());
newbufferedImage = zoomByScale(ratio, img, ext);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(newbufferedImage, ext, out);
return out.toByteArray();
}
//以上都不符合时 强制缩放
Image _img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
// 获取缩放比例
double wr=0,hr=0;
wr = width * 1.0 / img.getWidth();
hr = height * 1.0 / img.getHeight();
AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wr, hr), null);
_img = ato.filter(img, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write((BufferedImage) _img,ext,out);//写入缩减后的图片
return out.toByteArray();
}
public static BufferedImage zoomByScale(double scale, BufferedImage img, String ext) throws IOException {
//获取缩放后的长和宽
int _width = (int) (scale * img.getWidth());
int _height = (int) (scale * img.getHeight());
//设置缩放目标图片模板
Image _img = img.getScaledInstance(_width, _height, Image.SCALE_SMOOTH);
//缩放图片
AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(scale, scale), null);
_img = ato.filter(img, null);
return (BufferedImage) _img;
}
private static double CalculateZoomRatio(int divisor, int dividend) {
return BigDecimal.valueOf(divisor).divide(BigDecimal.valueOf(dividend), 6, BigDecimal.ROUND_HALF_UP).doubleValue();
}
--结束END--
本文标题: Java实现图片比率缩放
本文链接: https://www.lsjlt.com/news/146875.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0