From 4a35b225e76befce142ea0433c316e3f2f7b67de Mon Sep 17 00:00:00 2001 From: michaelin007 Date: Mon, 1 Apr 2024 09:56:31 +0000 Subject: [PATCH] https://jira.baeldung.com/browse/BAEL-7723 --- .../com/baeldung/barcodes/BarcodesController.java | 4 ++-- .../generators/ZxingBarcodeGeneratorWithText.java | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/barcodes/BarcodesController.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/barcodes/BarcodesController.java index e734b666db..a1318c0519 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/barcodes/BarcodesController.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/barcodes/BarcodesController.java @@ -84,8 +84,8 @@ public class BarcodesController { } @GetMapping(value = "/zxing/qrcode/text", produces = MediaType.IMAGE_PNG_VALUE) - public ResponseEntity zxingCodeQRcodeText(@RequestParam("barcode") String barcode, @RequestParam("name") String name, @RequestParam("info") String info) throws Exception { - return okResponse(ZxingBarcodeGeneratorWithText.createQRwithText(barcode, name, info)); + public ResponseEntity zxingCodeQRcodeText(@RequestParam("barcode") String barcode, @RequestParam("toptext") String toptext, @RequestParam("bottomtext") String bottomtext) throws Exception { + return okResponse(ZxingBarcodeGeneratorWithText.createQRwithText(barcode, toptext, bottomtext)); } @PostMapping(value = "/zxing/pdf417", produces = MediaType.IMAGE_PNG_VALUE) diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/barcodes/generators/ZxingBarcodeGeneratorWithText.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/barcodes/generators/ZxingBarcodeGeneratorWithText.java index 60ae1b427d..e6ebcf9bae 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/barcodes/generators/ZxingBarcodeGeneratorWithText.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/barcodes/generators/ZxingBarcodeGeneratorWithText.java @@ -13,10 +13,10 @@ import java.io.IOException; public class ZxingBarcodeGeneratorWithText { - public static BufferedImage createQRwithText(String data, String name, String info) throws WriterException, IOException { + public static BufferedImage createQRwithText(String data, String topText, String bottomText) throws WriterException, IOException { QRCodeWriter barcodeWriter = new QRCodeWriter(); BitMatrix matrix = barcodeWriter.encode(data, BarcodeFormat.QR_CODE, 200, 200); - return modifiedQRCode(matrix, name, info); + return modifiedQRCode(matrix, topText, bottomText); } public static BufferedImage modifiedQRCode(BitMatrix matrix, String topText, String bottomText) throws IOException { @@ -40,7 +40,7 @@ public class ZxingBarcodeGeneratorWithText { FontMetrics fontMetrics = graphics.getFontMetrics(); int topTextWidth = fontMetrics.stringWidth(topText); int bottomTextWidth = fontMetrics.stringWidth(bottomText); - int finalWidth = Math.max(matrixWidth, Math.max(topTextWidth, bottomTextWidth)) + 10; + int finalWidth = Math.max(matrixWidth, Math.max(topTextWidth, bottomTextWidth)) + 1; int finalHeight = matrixHeight + fontMetrics.getHeight() + fontMetrics.getAscent() + 1; BufferedImage finalImage = new BufferedImage(finalWidth, finalHeight, BufferedImage.TYPE_INT_RGB); @@ -49,9 +49,9 @@ public class ZxingBarcodeGeneratorWithText { finalGraphics.fillRect(0, 0, finalWidth, finalHeight); finalGraphics.setColor(Color.BLACK); - finalGraphics.drawImage(image, (finalWidth - matrixWidth) / 2, fontMetrics.getAscent() + 5, null); - finalGraphics.drawString(topText, (finalWidth - topTextWidth) / 2, fontMetrics.getAscent() + 5); - finalGraphics.drawString(bottomText, (finalWidth - bottomTextWidth) / 2, finalHeight - fontMetrics.getDescent() - 5); + finalGraphics.drawImage(image, (finalWidth - matrixWidth) / 2, fontMetrics.getAscent() + 2, null); + finalGraphics.drawString(topText, (finalWidth - topTextWidth) / 2, fontMetrics.getAscent() + 2); + finalGraphics.drawString(bottomText, (finalWidth - bottomTextWidth) / 2, finalHeight - fontMetrics.getDescent() - 2); return finalImage; }