Java下BufferedImage處理PNG圖片的ARGB


通過MultipartFile傳入png圖片,並通過BufferedImage進行處理。

@SneakyThrows
public void picture(MultipartFile multipartFile) {
    //讀取圖片
    System.out.println("正在讀取...");
    BufferedImage bufferedImage = null;
    try {
        bufferedImage = ImageIO.read(multipartFile.getInputStream());
    } catch (Exception e) {
        e.printStackTrace();
    }
    int width = bufferedImage.getWidth();
    int height = bufferedImage.getHeight();
    int minx = bufferedImage.getMinX();
    int miny = bufferedImage.getMinY();
    //處理圖片
    System.out.println("正在處理...");
    for (int i = minx; i < width; i++) {
        for (int j = miny; j < height; j++) {
            int pixel = bufferedImage.getRGB(i, j);//獲取顏色
            int alpha = pixel >> 24 & 0xff;//獲取alpha
            int red = pixel & 0xff0000 >> 16;//獲取紅色
            int green = pixel & 0xff00 >> 8;//獲取綠色
            int blue = pixel & 0xff;//獲取藍色
            int color = (alpha << 24) | (red << 16) | (green << 8) | blue;//將argb還原成整數
            bufferedImage.setRGB(i, j, color);//設置顏色
        }
    }
    //保存圖片
    File file = new File("test.png");
    ImageIO.write(bufferedImage, "png", file);
    System.out.println("處理完畢...");
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM