0

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?

  • Hi, did you try to return byte array? Like that ```java BufferedImage image = /*image*/ ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write(image, "png", bos); // format can be change. byte[] bytes = bos.toByteArray(); return ResponseEntity.ok(bytes); ``` – halil ibrahim binol Dec 15 '21 at 08:12
  • Hi Halil İbrahim, when I use another method that returns byte[], it works. But BufferedImage has some extra features and for this reason I tried to return it. So, it it not possible to return BufferedImage? –  Dec 15 '21 at 08:51
  • It is possible to return `BufferedImage` on controller by creating `BufferedImageHttpMessageConverter` bean. But i think it will also not ask to user to download. – halil ibrahim binol Dec 15 '21 at 10:55
  • Yes, you are right. I exactly created and add it to `Application.java`. However, I get **No converter for [class java.awt.image.BufferedImage] with preset Content-Type 'null'** error. I think I am missing something. I just add it to `Application.java` as my update, but do not call it from anywhere. Is that ok? –  Dec 15 '21 at 10:58
  • Did you set `contentType` on your `okResponse` method? If so can you try remove the content type. There is an issue about it. https://github.com/spring-projects/spring-framework/issues/23205 Edit: you can try remove the "produces = MediaType.IMAGE_PNG_VALUE" on @GetMapping – halil ibrahim binol Dec 15 '21 at 11:17
  • I fixed the problem by converting `BufferedImage` to ByteArray. However, I am not sure if I should keep `"produces = MediaType.IMAGE_PNG_VALUE"` on `@GetMapping`. Should I keep it? –  Dec 15 '21 at 13:33
  • If the generated image is JPEG than it will be stay. BTW did you try to remove content type? – halil ibrahim binol Dec 15 '21 at 13:39
  • Generated image is png and I use `MediaType.IMAGE_PNG_VALUE`. Then I think it is ok. –  Dec 15 '21 at 15:47
  • I did not try to remove content type. But if I should remove, I can? –  Dec 15 '21 at 15:47

0 Answers0