灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:3361回复:0

切分和组合图片(二)

楼主#
更多 发布于:2013-05-12 12:52

切分和组合图片(二)
组合步骤:
1. 初始化有多少小图片
2. 加载小图片到缓存中
3. 初始化大图片存储器
4.
组合小图片到大图片
5. 生成大图片文件

小图片源:
 
 

[java]
int rows = 4;   //初始化有小图片的数量  
        int cols =
4;  
        int chunks = rows * cols;  
  
        int chunkWidth,
chunkHeight;  
        int type;  
        //读取图片文件  
        File[]
imgFiles = new File[chunks];  
        for (int i = 0; i < chunks; i++)
{  
            imgFiles = new File("img" + i + ".jpg");  
        
}  
  
       //缓存图片文件  
        BufferedImage[] buffImages = new
BufferedImage[chunks];  
        for (int i = 0; i < chunks; i++)
{  
            buffImages = ImageIO.read(imgFiles);  
        
}  
        type = buffImages[0].getType();  
        chunkWidth =
buffImages[0].getWidth();  
        chunkHeight =
buffImages[0].getHeight();  
  
        //初始化最终的图片缓存器  
        
BufferedImage finalImg = new BufferedImage(chunkWidth*cols, chunkHeight*rows,
type);  
  
        int num = 0;  
        for (int i = 0; i <
rows; i++) {  
            for (int j = 0; j < cols; j++)
{  
                finalImg.createGraphics().drawImage(buffImages[num],
chunkWidth * j, chunkHeight * i, null);  
                
num++;  
            }  
        }  
        
System.out.println("图片组合完");  
        ImageIO.write(finalImg, "jpeg", new
File("b.jpg"));  
    }
int rows = 4;   //初始化有小图片的数量
        int cols = 4;
        int chunks
= rows * cols;
 
        int chunkWidth, chunkHeight;
        int
type;
        //读取图片文件
        File[] imgFiles = new
File[chunks];
        for (int i = 0; i < chunks; i++) {
            
imgFiles = new File("img" + i + ".jpg");
        }
 
      
//缓存图片文件
        BufferedImage[] buffImages = new
BufferedImage[chunks];
        for (int i = 0; i < chunks; i++)
{
            buffImages = ImageIO.read(imgFiles);
        
}
        type = buffImages[0].getType();
        chunkWidth =
buffImages[0].getWidth();
        chunkHeight =
buffImages[0].getHeight();
 
        //初始化最终的图片缓存器
        
BufferedImage finalImg = new BufferedImage(chunkWidth*cols, chunkHeight*rows,
type);
 
        int num = 0;
        for (int i = 0; i < rows;
i++) {
            for (int j = 0; j < cols; j++) {
                
finalImg.createGraphics().drawImage(buffImages[num], chunkWidth * j, chunkHeight
* i, null);
                num++;
            }
        
}
        System.out.println("图片组合完");
        ImageIO.write(finalImg,
"jpeg", new File("b.jpg"));
    }

喜欢0 评分0
游客

返回顶部