1:使用thumbnailator
thumbnailator是java的开源图像大小调整库,它使用渐进式双线性缩放。它支持jpg,bmp,jpeg,wbmp,png和gif。
通过将以下maven依赖项添加到我们的pom.xml中,将其包括在我们的项目中:
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;
}
}
2:graphics2d 自带的方法
public static bufferedimage scaleimage(bufferedimage originalimage, int targetwidth, int targetheight) {
int type = originalimage.gettype() == 0 ? bufferedimage.type_int_argb : originalimage.gettype();
bufferedimage scaledimage = new bufferedimage(targetwidth, targetheight, type);
graphics2d g = scaledimage.creategraphics();
// calculate the ratio between the original and scaled image size
double scalex = (double) targetwidth / originalimage.getwidth();
double scaley = (double) targetheight / originalimage.getheight();
double scale = math.min(scalex, scaley);
// now we perform the actual scaling
int newwidth = (int) (originalimage.getwidth() * scale);
int newheight = (int) (originalimage.getheight() * scale);
int x = (targetwidth - newwidth) / 2;
int y = (targetheight - newheight) / 2;
g.drawimage(originalimage.getscaledinstance(newwidth, newheight, image.scale_smooth), x, y, null);
g.dispose();
return scaledimage;
} public static void main(string[] args) throws exception {
// 读取原始图片
file originalfile = new file("d:\\test1\\schoollogo.png");
bufferedimage originalimage = imageio.read(originalfile);
// 设定目标宽高
int targetwidth = 1000; // 两倍放大
int targetheight = 1000; // 两倍放大
// 调用 resizeimageone 方法进行放大
bufferedimage resizedimage = thumbnailsutils.scaleimage(originalimage, targetwidth, targetheight);
// 将放大后的图片保存到文件
file outputfile = new file("d:\\test1\\big.png");
imageio.write(resizedimage, "png", outputfile);
}3:前两种我在使用 imagecombiner 项目的时候 不生效;
// 创建新的 bufferedimage,并设置绘制质量
bufferedimage resizedimage = new bufferedimage(newwidth, newheight, bufferedimage.type_int_rgb);
graphics2d g2d = resizedimage.creategraphics();
g2d.setrenderinghint(renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear);
// 绘制原始图像到新的 bufferedimage,并进行缩放
image scaledimage = image.getscaledinstance(newwidth, newheight, image.scale_smooth);
g2d.drawimage(scaledimage, 0, 0, null);
g2d.dispose();
// 保存新的图片
string outputimagepath = "c:\\users\\up1.jpg"; // 替换为实际的输出路径
imageio.write(resizedimage, "jpg", new file(outputimagepath));
system.out.println("图片尺寸调整完成!");总结
到此这篇关于java调整图片大小的3种方式小结的文章就介绍到这了,更多相关java调整图片大小内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论