Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat: ImageUtil 추가 #10

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum ErrorCode {

// Server
SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "내부 서버 문제입니다."),
FILE_IO_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "파일 입출력 에러입니다."),

// Bad Request Error
NOT_EXIST_PARAM(HttpStatus.BAD_REQUEST, "요청에 필요한 데이터가 없습니다."),
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/com/grimpan/emodiary/unit/ImageUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.grimpan.emodiary.unit;

import com.grimpan.emodiary.exception.CommonException;
import com.grimpan.emodiary.exception.ErrorCode;
import jakarta.xml.bind.DatatypeConverter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

@Component
@RequiredArgsConstructor
public class ImageUtil {
public void decoder(String base64, String imagePath) {
String data = base64.split(",")[0];

byte[] imageBytes = DatatypeConverter.parseBase64Binary(data);

try {
BufferedImage bufImg = ImageIO.read(new ByteArrayInputStream(imageBytes));

ImageIO.write(bufImg, "jpg", new File(imagePath));
} catch (IOException e) {
throw new CommonException(ErrorCode.FILE_IO_ERROR, null);
}
}

public String encoder(String imagePath) {
String base64 = null;

try {
BufferedImage bufImg = ImageIO.read(new File(imagePath));

java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream();

ImageIO.write(bufImg, "jpg", os);

base64 = DatatypeConverter.printBase64Binary(os.toByteArray());
} catch (IOException e) {
throw new CommonException(ErrorCode.FILE_IO_ERROR, null);
}

return base64;
}
}
Loading