首页 > 解决方案 > Java中BufferedImage的平铺,任何分辨率

问题描述

我有一门课来分离和加入瓷砖中的图像。当图块的侧面对应于图像的尺寸时,它可以正常工作,即高度 250,图块高度 25。但如果不是,它不会在边界处创建应有的更小的图块。

正确创建比其余的更小的边框瓷砖的问题在哪里?

构造函数:

public EdgeBufferedImage(BufferedImage image, int w, int h){
    this.sourceImg = image;

    this.setCol((int)Math.ceil(image.getWidth()/(double)w));
    this.setRow((int)Math.ceil(image.getHeight()/(double)h));

    this.setWidth(image.getWidth());
    this.setHeight(image.getHeight());

    this.setTilew(w);
    this.setTileh(h);
    this.setMatImg(new BufferedImage[row][col]);
}

方法:图像平铺

public void createSmallImages() {

    int rows = getRow();
    int columns = getCol();
    int smallWidth = getTilew();
    int smallHeight = getTileh();

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {

            if (j == columns - 1) smallWidth = getWidth() - (getTilew() * j);           
            if (i == rows - 1) smallHeight = getHeight() - (getTileh() * i);

            matImg[i][j] = getSourceImg().getSubimage(j * smallWidth, i
                    * smallHeight, smallWidth, smallHeight);
        }
        smallWidth = getTilew();
        smallHeight = getTileh();   
    }   
}

图像加入

public void joinTiles(){

    int rows = getRow();
    int columns = getCol();
    int smallWidth = getTilew();
    int smallHeight = getTileh();

    BufferedImage comb = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g = (Graphics2D) comb.getGraphics();

    g.setColor(Color.RED);

    for (int row = 0; row < rows; row++){
        for (int col = 0; col < columns; col++){

            BufferedImage piece = getMatImg()[row][col];

            if (col == columns - 1) smallWidth = getWidth() - (getTilew() * col);           
            if (row == rows - 1) smallHeight = getHeight() - (getTileh() * row);

            g.drawImage(piece, col * smallWidth, row * smallHeight, smallWidth, smallHeight, null);
            g.drawRect(col * smallWidth, row * smallHeight, smallWidth, smallHeight);

        }
        smallWidth = getTilew();
        smallHeight = getTileh();
    }
    g.dispose();

    setSourceImg(comb); 
}

原图为512*512

整块图片(256*128)

其他平铺尺寸(256*100)的图像

标签: javaimageawtbufferedimage

解决方案


推荐阅读