通過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("處理完畢..."); }