调整缓冲图像大小而不创建新实例(java)
我想知道是否有一种方法可以在不创建另一个图像的新实例的情况下调整BuffereImage的大小。我想知道这一点,因为我认为每当我想为我的应用程序调整BuffereImage的大小时,创建一个新的映像是低效的。以下是我看到的一些代码,它们解释了我不想要的东西:调整缓冲图像大小而不创建新实例(java),java,image,user-interface,bufferedimage,buffered,Java,Image,User Interface,Bufferedimage,Buffered,我想知道是否有一种方法可以在不创建另一个图像的新实例的情况下调整BuffereImage的大小。我想知道这一点,因为我认为每当我想为我的应用程序调整BuffereImage的大小时,创建一个新的映像是低效的。以下是我看到的一些代码,它们解释了我不想要的东西: public static BufferedImage resize(BufferedImage img, int newW, int newH) { Image tmp = img.getScaledInstance(newW,
public static BufferedImage resize(BufferedImage img, int newW, int newH) {
Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = dimg.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
return dimg;
}
publicstaticbufferedimage比例(BufferedImage src,intw,inth)
{
BuffereImage img=
新的BuffereImage(w,h,BuffereImage.TYPE_INT_RGB);
int x,y;
int ww=src.getWidth();
int hh=src.getHeight();
int[]ys=新的int[h];
对于(y=0;y
private BufferedImage resize(BufferedImage src,int targetSize){
if(targetSize我认为getScaledInstance()
方法是一种资源高效的方法,因为它只复制所有内容。而且它不必再次计算所有内容。是的,但它返回一个图像,而不是一个缓冲区图像该图像甚至可以包含对img和新大小的引用…新图像实例的类名是什么?不,是缩放实例的类名
public static BufferedImage scale(BufferedImage src, int w, int h)
{
BufferedImage img =
new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
int x, y;
int ww = src.getWidth();
int hh = src.getHeight();
int[] ys = new int[h];
for (y = 0; y < h; y++)
ys[y] = y * hh / h;
for (x = 0; x < w; x++) {
int newX = x * ww / w;
for (y = 0; y < h; y++) {
int col = src.getRGB(newX, ys[y]);
img.setRGB(x, y, col);
}
}
return img;
}
private BufferedImage resize(BufferedImage src, int targetSize) {
if (targetSize <= 0) {
return src; //this can't be resized
}
int targetWidth = targetSize;
int targetHeight = targetSize;
float ratio = ((float) src.getHeight() / (float) src.getWidth());
if (ratio <= 1) { //square or landscape-oriented image
targetHeight = (int) Math.ceil((float) targetWidth * ratio);
} else { //portrait image
targetWidth = Math.round((float) targetHeight / ratio);
}
BufferedImage bi = new BufferedImage(targetWidth, targetHeight, src.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); //produces a balanced resizing (fast and decent quality)
g2d.drawImage(src, 0, 0, targetWidth, targetHeight, null);
g2d.dispose();
return bi;
}