※ 此程式碼並非優化版本,僅供個人學習用途,若有任何問題請告知。
※ 準備2個圖檔:a.jpg, b.png。
![]() |
a.jpg(96x128) |
![]() |
b.png(144x192) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import javax.imageio.ImageIO; | |
public class imagecut { | |
public static void main(String[] args) throws IOException { | |
BufferedImage bi = ImageIO.read(new File("src\\k\\a.jpg")); | |
BufferedImage bi2 = ImageIO.read(new File("src\\k\\b.png")); | |
for (int i = 0; i < 12; i++) { //依據原圖ZxN切成ZN塊 | |
//圖片是W*Y W/Z=小圖的寬 Y/N=小圖的高 //getSubimage(X, Y, 寬, 高) | |
BufferedImage biNew = bi.getSubimage(32*(i%3), 32*(i/4), 32, 32); | |
ImageIO.write(biNew, "png", new File("src\\k\\p\\a\\"+i+".png")); | |
BufferedImage biNew2 = bi2.getSubimage(48*(i%3), 48*(i/4), 48, 48); | |
ImageIO.write(biNew2, "png", new File("src\\k\\p\\b\\"+i+".png")); | |
} | |
} | |
} |
結果如下:
沒有留言:
張貼留言