java - 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
解决方案
推荐阅读
- oracle - AWS Oracle RDS - Directory in external server
- go - 点击 50000 并行请求的 url
- android - 在所有 api 方法中使用通用 url 前缀?
- r - 如何更改 R 中随机森林的分割标准?
- reactjs - React-Native 总是在组件中调用 SwitchNavigator
- sql - 过滤掉 SQL Query 中的重复行
- vue.js - 从子组件 VueJs 添加元素的打开模式
- airflow - 如何在 Airflow 中手动触发来自未来的 dag?
- c# - override inherited event with generic type
- symfony4 - Creating controller-less route in Symfony