Skip to content

Commit

Permalink
✨ Feature: 이미지 평균값 추출 기능 구현 (#47)
Browse files Browse the repository at this point in the history
* ✨ Feature: 이미지 평균값 추출 기능 구현

* ✨ Feature: 일기 생성 로직에서 평균값 추출 로직 호출

* 📝 Docs: DiaryRequest example 작성
  • Loading branch information
ahnsugyeong authored Apr 21, 2024
1 parent e16c46e commit 09e027d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import zzangdol.emotion.domain.Emotion;
import zzangdol.moodoodleapi.diary.implement.DiaryCommandService;
import zzangdol.moodoodleapi.diary.implement.DiaryQueryService;
import zzangdol.moodoodleapi.diary.implement.ImageColorAnalyzer;
import zzangdol.moodoodleapi.diary.presentation.dto.request.DiaryCreateRequest;
import zzangdol.moodoodleapi.diary.presentation.dto.request.DiaryUpdateRequest;
import zzangdol.moodoodleapi.diary.presentation.dto.response.DiaryResponse;
import zzangdol.moodoodleapi.diary.presentation.dto.response.DiaryListResponse;
import zzangdol.moodoodleapi.diary.presentation.dto.response.DiaryResponse;
import zzangdol.user.domain.User;

@RequiredArgsConstructor
Expand All @@ -20,14 +21,12 @@ public class DiaryFacade {
private final DiaryCommandService diaryCommandService;
private final DiaryQueryService diaryQueryService;
// private final TextEmotionAnalysisModelClient textEmotionAnalysisModelClient;
// private final ImageColorAnalyzer imageColorAnalyzer;
private final ImageColorAnalyzer imageColorAnalyzer;

public Long createDiary(User user, DiaryCreateRequest request) {
// TODO List<Emotion> emotions = textEmotionAnalysisModelClient.analyzeEmotion(request.getContent());
List<Emotion> emotions = new ArrayList<>();

// TODO String color = imageColorAnalyzer.analyzeColor(request.getImageUrl());
String color = "";
String color = imageColorAnalyzer.analyzeAverageColorAsHex(request.getImageUrl());
return diaryCommandService.createDiary(user, request, color, emotions).getId();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import zzangdol.diary.domain.Diary;
import zzangdol.moodoodleapi.diary.presentation.dto.response.DiaryResponse;
import zzangdol.moodoodleapi.diary.presentation.dto.response.DiaryListResponse;
import zzangdol.moodoodleapi.diary.presentation.dto.response.DiaryResponse;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DiaryMapper {
Expand Down
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);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package zzangdol.moodoodleapi.diary.presentation.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -12,8 +13,13 @@
@AllArgsConstructor
public class DiaryCreateRequest {

@Schema(example = "2024-04-21T21:10")
private LocalDateTime date;

@Schema(example = "오늘은 오랜만에 친구들과 놀이공원에 다녀왔다. 정말 신나는 하루였다. 집에 돌아오는 길에는 조금 허전하고 아쉬운 마음도 들었지만, 오늘 하루 친구들과 함께 할 수 있어서 정말 행복했다.")
private String content;

@Schema(example = "https://github.com/ahnsugyeong/ahnsugyeong/assets/88311377/5913c88f-ecde-495c-8c36-71a43e1c8a8d")
private String imageUrl;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package zzangdol.moodoodleapi.diary.presentation.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -12,7 +13,10 @@
@AllArgsConstructor
public class DiaryUpdateRequest {

@Schema(example = "2024-04-20T21:10")
private LocalDateTime date;

@Schema(example = "오늘은 오랜만에 친구들과 놀이공원에 다녀왔다. 츄러스도 먹고 정말 신나는 하루였다. 집에 돌아오는 길에는 조금 허전하고 아쉬운 마음도 들었지만, 오늘 하루 친구들과 함께 할 수 있어서 정말 행복했다.")
private String content;

}

0 comments on commit 09e027d

Please sign in to comment.