Java实现图片比率缩放
短信预约 -IT技能 免费直播动态提醒
本文实例为大家分享了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();
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341