Java图像调整大小并居中

3

我之前在查找其他内容时,记得看到一种方法可以调整图像大小,但保持图像边缘的某些区域不变,以便图像的圆角始终保持相同,无论缩放后的图像长度如何。经过更多的搜索,我无法找到我记得看到的内容,因此我想知道是否可能实现,并且是否有人有相关信息。

具体来说,我有一个自定义边框,看起来像是带有标签的悬挂制表符。 图像顶部为正方形,底部边缘为圆形。 我想保持底部的圆角,但拉伸中心约90%以容纳更大的标签。 我认为在这段代码中,我可以输入一些插入值,在调整大小方法中实现此目的。

titleLabel = new JLabel(title){
            public void paint(Graphics g){
                g.drawImage(tabImg, 0, 0, this.getSize().width, this.getSize().height, null);
                g.drawString(this.getText(), 1, this.getFont().getSize());
            }
        };

手动完成并不难:只需获取中间图像的像素列,并将其复制多次以达到所需宽度。 - toto2
你可以在这些例子中查找(http://www.google.com/search?q=java+magnifying+glass)。 - trashgod
1个回答

2

好的,考虑到没有被广泛接受的方法来实现这个,我想出了这个解决方案,并将代码发布给任何想做类似事情的人。这段代码将把一张图片分成9个部分。4个角落将保持不变。4条边的中间部分将沿着边缘被拉伸或压缩。中心部分将在两个方向上被拉伸或压缩。当然,对于我来说,这个类的目的是压缩一个带有圆角的大图像,但保留圆角,而当图像简单缩小时,圆角几乎消失了。显然,对于像图片这样的图像来说,这没什么用处,但是对于具有自定义绘制和圆角边缘的组件,这似乎很有效。

enter image description here

这个类没有构造函数,你只需要调用一个修改后的图像即可。使用方法如下:

StretchedImage.stretch(image, new Insets(t,l,b,r), new Dimension(w,h), BufferedImage.TYPE_INT_ARGB); 

使用insets参数确定在单个维度上修改的边缘周围的数量,将返回一个拉伸到所需尺寸的图像,角落保持不变,只有侧面在1个维度上被修改。也许有一种巧妙的方法来遍历图像列表,但是这样我可以更好地看到发生了什么。

public class StretchedImage{

public static Image stretch(Image image, Insets ins, Dimension dim, int hints){
    //debugcode
    //System.out.println(dim); 

    //load image into bufferedImage
    BufferedImage bi = toBufferedImage(image, hints); 

    //create 2d bufferedImage array to hold the 9 images
    Image[][] img = new Image[3][3]; 

    //split Image into 9 subsections
    img[0][0] = bi.getSubimage(0, 0, ins.left, ins.top);
    img[0][1] = bi.getSubimage(ins.left, 0, bi.getWidth() - ins.left - ins.right, ins.top);
    img[0][2] = bi.getSubimage(bi.getWidth() - ins.right, 0, ins.right, ins.top);
    img[1][0] = bi.getSubimage(0, ins.top, ins.left, bi.getHeight() - ins.top - ins.bottom);
    img[1][1] = bi.getSubimage(ins.left, ins.top, bi.getWidth() - ins.left - ins.right, bi.getHeight() - ins.top - ins.bottom);
    img[1][2] = bi.getSubimage(bi.getWidth() - ins.right, ins.top, ins.right, bi.getHeight() - ins.top - ins.bottom);
    img[2][0] = bi.getSubimage(0, bi.getHeight() - ins.bottom, ins.left, ins.bottom);
    img[2][1] = bi.getSubimage(ins.left, bi.getHeight() - ins.bottom, bi.getWidth() - ins.left - ins.right, ins.bottom);
    img[2][2] = bi.getSubimage(bi.getWidth() - ins.right, bi.getHeight() - ins.bottom, ins.right, ins.bottom);

    //determine the width and height of the sections that will be stretched
    //only the center section will have both dimensions changed
    int w = dim.width - ins.left - ins.right;
    int h = dim.height - ins.top - ins.bottom;

    //Stretch the proper sections 
    img[0][1] = img[0][1].getScaledInstance(w, img[0][1].getHeight(null), hints);
    img[1][0] = img[1][0].getScaledInstance(img[1][0].getWidth(null), h, hints);
    img[1][1] = img[1][1].getScaledInstance(w, h, hints);
    img[1][2] = img[1][2].getScaledInstance(img[1][2].getWidth(null), h, hints);
    img[2][1] = img[2][1].getScaledInstance(w, img[2][1].getHeight(null), hints);

    //for loop is debug code
    //for(int i = 0; i < 3; i++){ 
    //  for(int j = 0; j < 3; j++){
    //      System.out.println(i + " " + j + " " + img[i][j].getWidth() + "," + img[i][j].getHeight());
    //  }
    //}

    //create a new bufferedImage to hold the final image
    BufferedImage finalImage = new BufferedImage(dim.width, dim.height, hints);
    Graphics g = finalImage.getGraphics();
    //draw the peices to the final image
    g.drawImage(img[0][0], 0, 0, null);
    g.drawImage(img[0][1], ins.left, 0, null);
    g.drawImage(img[0][2], dim.width - ins.right, 0, null);
    g.drawImage(img[1][0], 0, ins.top, null);
    g.drawImage(img[1][1], ins.left, ins.top, null);
    g.drawImage(img[1][2], dim.width - ins.right, ins.top, null);
    g.drawImage(img[2][0], 0, dim.height - ins.bottom, null);
    g.drawImage(img[2][1], ins.left, dim.height - ins.bottom, null);
    g.drawImage(img[2][2], dim.width - ins.right, dim.height - ins.bottom, null);   

    return (Image)finalImage;
}

// This method returns a buffered image with the contents of an image
public static BufferedImage toBufferedImage(Image image, int hints) {

    BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), hints);
    bi.getGraphics().drawImage(image, 0, 0, null);

    return bi;
}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接