Question

Final update (please see comments)

I have given up on implementing my own QR generator due to GAE limitations. The former Google Charts API services still can be used, and also replicated using ZXing's servlet. For more details, see this link. Thank you for the answers.


Update 2 (see original question below)

So I dug into the source of ZXing and they seem to use BufferedImage in all of their processes. My question now could be phrased as:

  • Is there any way to use ZXing with Google App Engine?
  • Is there any way to generate a QR code in a servlet that could be deployed to Google App Engine?

Update (see original question below)

The following line causes the error apparently:

MatrixToImageWriter.writeToStream(encoded, "png", outs);

It seems to be using BufferedImage. Is it possible to convert a BitMatrix to a byte[] without doing this step?


Original question

I am trying to create a servlet on Google App Engine to return a QR code with given parameters. So far I have created solutions both with QRGen and ZXing which work perfectly when testing in App Engine local development mode. My problem is that both of these implementations fail after I deploy my servlet to App Engine, saying either

Could not initialize class com.google.apphosting.runtime.security.shared.stub.java.awt.image.BufferedImage

or

java.awt.Image is a restricted class. Please see the Google App Engine developer's guide for more details.

I don't understand completely how these tools work internally, what I know that java.awt.image classes are not on the Class whitelist. My question is that is there any way to get around this security feature, and actually return a QR code from a servlet. Here is my latest implementation (which works fine in development mode, but also fails on a deployed servlet) using ZXing (it seems to offer more options than QRGen):

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
    String returnString = "myString";

    Writer writer = new QRCodeWriter();
    BitMatrix encoded = null;
    try
    {
        encoded = writer.encode(returnString, BarcodeFormat.QR_CODE, 300, 300);
    } 
    catch (WriterException ex)
    {
        Logger.getLogger(QRService.class.getName()).log(Level.SEVERE, null, ex);
    }

    ByteArrayOutputStream outs = new ByteArrayOutputStream();
    MatrixToImageWriter.writeToStream(encoded, "png", outs);
    byte[] out = outs.toByteArray();

    Blob qrImage = new Blob(out);
    resp.setContentType("image/jpeg");
    resp.getOutputStream().write(qrImage.getBytes());
}
Was it helpful?

Solution

Here, look into the Google Charts API

https://developers.google.com/chart/infographics/docs/qr_codes

It seems it allows you to create a QR code with simple HTTP GET request. To create an HTTP GET request in App Engine, try this:

https://developers.google.com/appengine/docs/java/urlfetch/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top