-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✨ Feature: 이미지 평균값 추출 기능 구현 * ✨ Feature: 일기 생성 로직에서 평균값 추출 로직 호출 * 📝 Docs: DiaryRequest example 작성
- Loading branch information
1 parent
e16c46e
commit 09e027d
Showing
5 changed files
with
61 additions
and
6 deletions.
There are no files selected for viewing
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
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
46 changes: 46 additions & 0 deletions
46
moodoodle-api/src/main/java/zzangdol/moodoodleapi/diary/implement/ImageColorAnalyzer.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package zzangdol.moodoodleapi.diary.implement; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import javax.imageio.ImageIO; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ImageColorAnalyzer { | ||
|
||
public String analyzeAverageColorAsHex(String imageUrl) { | ||
try { | ||
int[] averageColor = getAverageColor(imageUrl); | ||
return rgbToHex(averageColor[0], averageColor[1], averageColor[2]); | ||
} catch (Exception e) { | ||
return "#ffffff"; | ||
} | ||
} | ||
|
||
private int[] getAverageColor(String imageUrl) throws IOException { | ||
URL url = new URL(imageUrl); | ||
BufferedImage image = ImageIO.read(url); | ||
|
||
long sumR = 0, sumG = 0, sumB = 0; | ||
int width = image.getWidth(); | ||
int height = image.getHeight(); | ||
int count = width * height; | ||
|
||
for (int x = 0; x < width; x++) { | ||
for (int y = 0; y < height; y++) { | ||
int pixel = image.getRGB(x, y); | ||
sumR += (pixel >> 16) & 0xff; | ||
sumG += (pixel >> 8) & 0xff; | ||
sumB += pixel & 0xff; | ||
} | ||
} | ||
|
||
return new int[]{(int) (sumR / count), (int) (sumG / count), (int) (sumB / count)}; | ||
} | ||
|
||
private String rgbToHex(int red, int green, int blue) { | ||
return String.format("#%02x%02x%02x", red, green, blue); | ||
} | ||
|
||
} |
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
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