切分和組合圖片(二)

切分和組合圖片(二)

組合步驟:
1. 初始化有多少小圖片
2. 加載小圖片到緩存中
3. 初始化大圖片存儲器
4. 組合小圖片到大圖片
5. 生成大圖片文件

小圖片源:


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[i] = new File("img" + i + ".jpg");  
        }  
  
       //緩存圖片文件
        BufferedImage[] buffImages = new BufferedImage[chunks];  
        for (int i = 0; i < chunks; i++) {  
            buffImages[i] = ImageIO.read(imgFiles[i]);  
        }  
        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"));  
    }

生成的大圖片:
















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章