Merge pull request #16277 from Michaelin007/qrcode

https://jira.baeldung.com/browse/BAEL-7723
This commit is contained in:
Maiklins 2024-04-10 22:47:48 +02:00 committed by GitHub
commit 30ee2dfaa6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 72 additions and 1 deletions

View File

@ -4,10 +4,17 @@ import com.baeldung.barcodes.generators.BarbecueBarcodeGenerator;
import com.baeldung.barcodes.generators.Barcode4jBarcodeGenerator;
import com.baeldung.barcodes.generators.QRGenBarcodeGenerator;
import com.baeldung.barcodes.generators.ZxingBarcodeGenerator;
import com.baeldung.barcodes.generators.ZxingBarcodeGeneratorWithText;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import java.awt.image.BufferedImage;
@ -76,6 +83,11 @@ public class BarcodesController {
return okResponse(ZxingBarcodeGenerator.generateCode128BarcodeImage(barcode));
}
@GetMapping(value = "/zxing/qrcode/text", produces = MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<BufferedImage> 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)
public ResponseEntity<BufferedImage> zxingPDF417Barcode(@RequestBody String barcode) throws Exception {
return okResponse(ZxingBarcodeGenerator.generatePDF417BarcodeImage(barcode));

View File

@ -0,0 +1,59 @@
package com.baeldung.barcodes.generators;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class ZxingBarcodeGeneratorWithText {
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, topText, bottomText);
}
public static BufferedImage modifiedQRCode(BitMatrix matrix, String topText, String bottomText) throws IOException {
int matrixWidth = matrix.getWidth();
int matrixHeight = matrix.getHeight();
BufferedImage image = new BufferedImage(matrixWidth, matrixHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixHeight);
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++) {
for (int j = 0; j < matrixHeight; j++) {
if (matrix.get(i, j)) {
graphics.fillRect(i, j, 1, 1);
}
}
}
FontMetrics fontMetrics = graphics.getFontMetrics();
int topTextWidth = fontMetrics.stringWidth(topText);
int bottomTextWidth = fontMetrics.stringWidth(bottomText);
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);
Graphics2D finalGraphics = finalImage.createGraphics();
finalGraphics.setColor(Color.WHITE);
finalGraphics.fillRect(0, 0, finalWidth, finalHeight);
finalGraphics.setColor(Color.BLACK);
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;
}
}