I have the following implementation to create QR Code and try to return generated code as html response (ask user to open or save the generated image) instead of saving to file in a directory. However, I could not manage to perform that. I use the following example on Baeldung: https://www.baeldung.com/java-generating-barcodes-qr-codes
public static BufferedImage generateQRCodeImage(String barcodeText) throws Exception {
QRCodeWriter barcodeWriter = new QRCodeWriter();
BitMatrix bitMatrix =
barcodeWriter.encode(barcodeText, BarcodeFormat.QR_CODE, 200, 200);
return MatrixToImageWriter.toBufferedImage(bitMatrix);
}
I use the following REST Controller method:
@RestController
@RequestMapping("/barcodes")
public class BarcodesController {
@GetMapping(value = "/barbecue/ean13/{barcode}", produces = MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<BufferedImage> barbecueEAN13Barcode(@PathVariable("barcode") String barcode)
throws Exception {
return okResponse(BarbecueBarcodeGenerator.generateEAN13BarcodeImage(barcode));
}
//...
}
However, I cannot get the image as HTTP response. So, how can I fix it?
Update: Here is my Application.java:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args)
}
@Bean
public HttpMessageConverter<BufferedImage> createImageHttpMessageConverter() {
return new BufferedImageHttpMessageConverter();
}
}
Is that enough? Or should I call it from anywhere?