-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from HaegyeongKim01/week11-haegyeong
refactor: qr 수정, shared/cards/{cardId/ 경로 변경
- Loading branch information
Showing
9 changed files
with
227 additions
and
146 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
66 changes: 35 additions & 31 deletions
66
src/main/java/com/devcard/devcard/card/controller/rest/QrController.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 |
---|---|---|
@@ -1,31 +1,35 @@ | ||
package com.devcard.devcard.card.controller.rest; | ||
|
||
import com.devcard.devcard.card.dto.QrResponseDto; | ||
import com.devcard.devcard.card.service.QrServiceImpl; | ||
import com.google.zxing.WriterException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.io.IOException; | ||
|
||
@RestController | ||
public class QrController { | ||
|
||
private final QrServiceImpl qrServiceImpl; | ||
|
||
@Autowired | ||
public QrController(QrServiceImpl qrServiceImpl) { | ||
this.qrServiceImpl = qrServiceImpl; | ||
} | ||
|
||
@GetMapping("/cards/{card_id}/qrcode") | ||
public ResponseEntity<QrResponseDto> createQR(@PathVariable (name = "card_id") Long cardId) throws IOException, WriterException { | ||
String qrUrl = qrServiceImpl.createQr(cardId); | ||
|
||
return ResponseEntity.ok() | ||
.body(new QrResponseDto(qrUrl)); | ||
} | ||
} | ||
package com.devcard.devcard.card.controller.rest; | ||
|
||
import com.devcard.devcard.card.service.QrServiceImpl; | ||
import com.google.zxing.WriterException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
|
||
@RestController | ||
public class QrController { | ||
|
||
private final QrServiceImpl qrServiceImpl; | ||
|
||
@Autowired | ||
public QrController(QrServiceImpl qrServiceImpl) { | ||
this.qrServiceImpl = qrServiceImpl; | ||
} | ||
|
||
@GetMapping("/cards/{card_id}/qrcode-image") | ||
public ResponseEntity<byte[]> generateQrImage(@PathVariable(name = "card_id") Long cardId) throws IOException, WriterException { | ||
ByteArrayOutputStream qrImageStream = qrServiceImpl.generateQrImageStream(cardId); | ||
|
||
return ResponseEntity.ok() | ||
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"qrcode.png\"") | ||
.contentType(MediaType.IMAGE_PNG) | ||
.body(qrImageStream.toByteArray()); | ||
} | ||
} |
6 changes: 0 additions & 6 deletions
6
src/main/java/com/devcard/devcard/card/dto/QrResponseDto.java
This file was deleted.
Oops, something went wrong.
117 changes: 36 additions & 81 deletions
117
src/main/java/com/devcard/devcard/card/service/QrServiceImpl.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 |
---|---|---|
@@ -1,81 +1,36 @@ | ||
package com.devcard.devcard.card.service; | ||
|
||
import com.google.zxing.BarcodeFormat; | ||
import com.google.zxing.MultiFormatWriter; | ||
import com.google.zxing.WriterException; | ||
import com.google.zxing.client.j2se.MatrixToImageWriter; | ||
import com.google.zxing.common.BitMatrix; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
@Service | ||
public class QrServiceImpl implements QrService{ | ||
|
||
private static final int QR_SIZE_WIDTH = 200; | ||
private static final int QR_SIZE_HEIGHT = 200; | ||
|
||
@Value("${qr.domain.uri}") | ||
private String domainUri; | ||
|
||
@Value("${qr.code.directory}") | ||
private String qrCodeDirectory; | ||
|
||
/** | ||
* @param cardId QR로 만들 명함 ID | ||
* @return QR 코드 IMAGE 파일 이름만 반환 | ||
*/ | ||
@Override | ||
public String createQr(Long cardId) throws WriterException, IOException { | ||
|
||
// QR URL - QR 코드 정보 URL | ||
String url = generateQrUrl(cardId); | ||
|
||
// QR Code - BitMatrix: qr code 정보 생성 | ||
BitMatrix bitMatrix = generateQrCode(url); | ||
|
||
// Setting QR Image File Name, Path | ||
String qrFileName = generateQrFileName(cardId); | ||
Path qrPath = generateQrFilePath(qrFileName); | ||
|
||
// Save QR | ||
saveQrCodeImage(bitMatrix, qrPath); | ||
|
||
return domainUri + "qrcodes/" + qrFileName; | ||
} | ||
|
||
private String generateQrUrl(Long cardId) { | ||
return domainUri + "cards/" + cardId + "/view"; | ||
} | ||
|
||
private BitMatrix generateQrCode(String url) throws WriterException { | ||
try { | ||
return new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, QR_SIZE_WIDTH, QR_SIZE_HEIGHT); | ||
} catch (WriterException e) { | ||
throw e; | ||
} | ||
} | ||
|
||
private String generateQrFileName(Long cardId) { | ||
return "card_id_" + cardId + ".png"; | ||
} | ||
|
||
private Path generateQrFilePath(String qrFileName) { | ||
return Paths.get(qrCodeDirectory + qrFileName); | ||
} | ||
|
||
private void saveQrCodeImage(BitMatrix bitMatrix, Path qrPath) throws IOException { | ||
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { | ||
MatrixToImageWriter.writeToStream(bitMatrix, "png", out); | ||
Files.createDirectories(qrPath.getParent()); | ||
Files.write(qrPath, out.toByteArray()); | ||
} catch (IOException e) { | ||
throw e; | ||
} | ||
} | ||
} | ||
package com.devcard.devcard.card.service; | ||
|
||
import com.google.zxing.BarcodeFormat; | ||
import com.google.zxing.MultiFormatWriter; | ||
import com.google.zxing.WriterException; | ||
import com.google.zxing.client.j2se.MatrixToImageWriter; | ||
import com.google.zxing.common.BitMatrix; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
|
||
@Service | ||
public class QrServiceImpl { | ||
|
||
private static final int QR_SIZE_WIDTH = 200; | ||
private static final int QR_SIZE_HEIGHT = 200; | ||
|
||
@Value("${qr.domain.uri}") | ||
private String domainUri; | ||
|
||
public ByteArrayOutputStream generateQrImageStream(Long cardId) throws WriterException, IOException { | ||
// QR URL 생성 | ||
String url = domainUri + "shared/cards/" + cardId; | ||
|
||
// QR Code 생성 | ||
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, QR_SIZE_WIDTH, QR_SIZE_HEIGHT); | ||
|
||
// QR Code 이미지를 메모리 스트림에 저장 | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream); | ||
|
||
return outputStream; | ||
} | ||
} |
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
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
Oops, something went wrong.