michaelin007 2024-03-31 12:12:48 +00:00
parent 9e46a49f20
commit 878751f8ef
2 changed files with 64 additions and 4 deletions

View File

@ -1,9 +1,6 @@
package com.baeldung.barcodes;
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.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
@ -76,6 +73,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("name") String name, @RequestParam("info") String info) throws Exception {
return okResponse(ZxingBarcodeGeneratorWithText.createQRwithText(barcode, name, info));
}
@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,58 @@
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.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class ZxingBarcodeGeneratorWithText {
public static BufferedImage createQRwithText(String data, String name, String info) throws WriterException, IOException {
QRCodeWriter barcodeWriter = new QRCodeWriter();
BitMatrix matrix = barcodeWriter.encode(data, BarcodeFormat.QR_CODE, 200, 200);
return modifiedQRCode(matrix, name, info);
}
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)) + 10;
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() + 5, null);
finalGraphics.drawString(topText, (finalWidth - topTextWidth) / 2, fontMetrics.getAscent() + 5);
finalGraphics.drawString(bottomText, (finalWidth - bottomTextWidth) / 2, finalHeight - fontMetrics.getDescent() - 5);
return finalImage;
}
}