2

I have a webserver that creates a QRcode. During the process, I get a BarcodeQRCode object from which I can get the image (.getImage()).

I am not sure how I can send back to the client this image. I don't want to save it in a file but just send back data in response to JSON request. For information, I have a similar case from which I get a PDF file that works great:

private ByteArrayRepresentation getPdf(String templatePath, JSONObject json) throws IOException, DocumentException, WriterException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfStamper stamper = new PdfStamper(..., baos);
    // setup PDF content...
    return new ByteArrayRepresentation(baos.toByteArray(), MediaType.APPLICATION_PDF);
}

Is there a way to do something similar more or less like:

private ByteArrayRepresentation getImage(JSONObject json) throws IOException, DocumentException, WriterException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Image qrCode = getQRCode(json); /// return the BarcodeQRCode.getImage()

    ImageIO.write(qrCode, "png", baos);
    return new ByteArrayRepresentation(baos.toByteArray(), MediaType.IMAGE_PNG);
    }

But this is not working. I get: argument mismatch; Image cannot be converted to RenderedImage.

EDIT

No compilation error after modification as proposed below. However, the returned image seems to be empty (or at least not normal). I put the error-free code if anyone has an idea what is wrong:

    @Post("json")
    public ByteArrayRepresentation accept(JsonRepresentation entity) throws IOException, DocumentException, WriterException {
        JSONObject json = entity.getJsonObject();
        return createQR(json);
    }

    private ByteArrayRepresentation createQR(JSONObject json) throws IOException, DocumentException, WriterException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Image codeQR = getQRCode(json);
        BufferedImage buffImg = new BufferedImage(codeQR.getWidth(null), codeQR.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
        buffImg.getGraphics().drawImage(codeQR, 0, 0, null);

        return new ByteArrayRepresentation(baos.toByteArray(), MediaType.IMAGE_PNG);
    }

    private Image getQRCode(JSONObject json) throws IOException, DocumentException, WriterException {
        JSONObject url = json.getJSONObject("jsonUrl");
        String urls = (String) url.get("url");
        BarcodeQRCode barcode = new BarcodeQRCode(urls, 200, 200, null);
        Image codeImage = barcode.createAwtImage(Color.BLACK, Color.WHITE);

        return codeImage;
    }
Trichophyton
  • 625
  • 5
  • 21
  • `Image qrCode = getQRCode(json);` Here try to use class `RenderedImage` instead of `Image`. `MediaType.IMAGE_PNG` expects you to send a `RenderedImage` object. But, you're passing an `Image` object. – Sridhar Oct 03 '17 at 08:45
  • Since, `BufferedImage` implements `RenderedImage`, you can simply convert your `Image` to `BufferedImage`. – Sridhar Oct 03 '17 at 08:53
  • I tried but I get the error below (BufferedImage varibale -> symbol not found). :-/ – Trichophyton Oct 03 '17 at 08:57
  • Always import a class before using it `import java.awt.image.BufferedImage` – Sridhar Oct 03 '17 at 09:06
  • Yes I was mistaken, I had an import for java.awt.image.RenderedImage (my mistake) – Trichophyton Oct 03 '17 at 09:09

2 Answers2

2

First, convert the Image to RenderedImage:

BufferedImage buffImg = new BufferedImage(qrCode.getWidth(null), qrCode.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
buffImg.getGraphics().drawImage(qrCode, 0, 0, null);
Robert D. Mogos
  • 900
  • 7
  • 14
  • I got this error: cannot find symbol BufferedImage buffImg = new BufferedImage(qrCode.getWith(), qrCode.getHeight(), BufferedImage.TYPE_4BYTE_ARGB); symbol: variable BufferedImage – Trichophyton Oct 03 '17 at 08:55
  • Did you try to import the class? `import java.awt.image.BufferedImage` – Robert D. Mogos Oct 03 '17 at 08:59
  • I had '.RenderedImage' you're right :-). But now I have other 3 errors "cannot find symbols": 1) method getWith() (qrCode.getWith()) -> variable qrCode of type Image. --- 2) variable TYPE_4BYTE_ARGB --- and 3)maybe the last one explaining the others: incompatible types: com.itextpdf.text.Image cannot be converted to java.awt.Image buffImg.getGraphics().drawImage(qrCode, 0, 0, null); – Trichophyton Oct 03 '17 at 09:06
  • com.itextpdf.text.pdf is used for the BarcodeQRCode class – Trichophyton Oct 03 '17 at 09:08
  • Use method, `createAwtImage(Color.BLACK, Color.WHITE)` instead of `getImage()` from class `BarcodeQRCode` in your method `getQRCode(json)` Also change type of `qrCode` to `java.awt.Image` instead of `com.itextpdf.text.Image` – Sridhar Oct 03 '17 at 09:13
  • unfortunately no :-(... I am not sure to be succesfull in change the type of image. I imported java.awt.Image, it is recognized as returned type in function but now when trying to create a new Object... – Trichophyton Oct 03 '17 at 09:43
  • @Trichophyton could you please provide the code of method `getQRCode(json);`, so that I can see what was the problem. Also, Since your actual problem (Image cannot be converted to RenderedImage) is solved by the answer posted by @RobertD.Mogos, please accept the answer. – Sridhar Oct 03 '17 at 11:14
  • @Trichophyton I updated my answer, you shouldn't have the compile errors now – Robert D. Mogos Oct 03 '17 at 11:51
  • Thanks a lot, it is fixed. However it seems that the returned image is empty or damaged when using it after I get it back from the server. Do you know if I made something wrong? This is not the case when I retrieve it from a PDF (the QR code is normal) so I think the problem must be located during the image formatting process or probably close to it. – Trichophyton Oct 03 '17 at 13:57
  • @Trichophyton try to save your image locally first, and see if what you get from `getQRCode(json);` is correct. Add this and check: `ImageIO.write(codeQR, "png", new File("/YOUR_PATH/test2.png"));` Replace `YOUR_PATH` with the ones on your machine – Robert D. Mogos Oct 03 '17 at 15:07
  • The QR code is written to file and works perfectly. The problem in fact is that I realized that I had forgotten to make ImageIO.write(codeQR, "png", baos))... But I can get it, sorry and thanks again for your patience and help! Really appreciated!! – Trichophyton Oct 03 '17 at 16:24
0

If you use com.itextpdf.text.Image you can use this code

BarcodeQRCode qrcode = new BarcodeQRCode("testo testo testo", 1, 1, null);
Image image = qrcode.createAwtImage(Color.BLACK, Color.WHITE);

BufferedImage buffImg = new BufferedImage(image.getWidth(null), image.getWidth(null), BufferedImage.TYPE_4BYTE_ABGR);
buffImg.getGraphics().drawImage(image, 0, 0, null);
buffImg.getGraphics().dispose();

File file = new File("tmp.png");
ImageIO.write(buffImg, "png", file);

I hope you have been helpful

Enrico

Enrico
  • 31
  • 3