0

At the very begining - sorry for my english.

I'm developing a play-application in java and deploy it to heroku. I like to create a picture (a QRCode to be precisely), store it temporary and display it on the next page. I do know about herokus ephemeral filesystem, but if I understand right, on the cedar stack, I am able to create files wherever I like, as long as it's ok, that they won't be stored for a long time. The app just needs to generate a QR, I scan it and the file may be deleted.

It seems as if the file is not created. Any ideas of how I can manage to temporary save and show my QRs?

Controller

public class Application extends Controller {

    private static String workingDirectory = "public/images/";
    public static Result qrCode() {
        String msg = "I am a QR-String";
        BufferedImage image = (BufferedImage) QR.stringToImage(msg);

        String imgPath = workingDirectory+"posQR.png";

        try{
            File outputfile = new File(imgPath);
            ImageIO.write(image,"png",outputfile);
        }catch(IOException e){
            e.printStackTrace();
        }
        return ok(views.html.qrCode.render());
    }
}

View qrCode

<img src="@routes.Assets.at("images/posQR.png")">

Edit 1

Stored the image as tempFile an pass it to the view. On heroku an local the view contains the exact absolute path, but the image won't load. Any ideas left?

Controller

public class Application extends Controller {

    private static String workingDirectory = "public/images/";
    public static Result qrCode() {
        String msg = "I am a QR-String";
        BufferedImage image = (BufferedImage) QR.stringToImage(msg);
    File outputfile = null;
        String imgPath = workingDirectory+"posQR.png";

        try{
            outputfile = File.createTempFile("posQR",".png");
            ImageIO.write(image,"png",outputfile);
        }catch(IOException e){
            e.printStackTrace();
        }
            return ok(views.html.qrCode.render(outputfile.getAbsolutePath()));
}

View qrCode

@(qrPath: String)
...
<img id="qr" src=@qrPath>

2 Answers2

0

Does workingDirectory end with file separator?

String imgPath = workingDirectory+"posQR.png";
Leos Literak
  • 8,805
  • 19
  • 81
  • 156
  • Yes, it is private static String workingDirectory = "public/images/"; – erdbeerchen Aug 14 '13 at 17:17
  • Method write shall return boolean value as result. What is it? Btw try outputfile.getAbsolutePath() and outputfile.canWrite() as well. Another possibility is File.createTempFile(). – Leos Literak Aug 14 '13 at 20:04
  • Return-value of write is true, so is canWrite(). On both, local and heroku. And both absolute paths end with /public/images/posQR.png – erdbeerchen Aug 15 '13 at 09:56
  • Try File.createTempFile() instead of manually placing file. This method must work, otherwise something is utterly wrong. – Leos Literak Aug 15 '13 at 15:33
  • The TempFile is created. How can I access it in the temp directory with this ugly numeric part in it? – erdbeerchen Aug 15 '13 at 19:45
  • Good, you know that file can be created on that device. You should pass file name to your business logic (scanning QR). Or if it is not possible and you must rely on fixed name, then create file in same directory as temp file. – Leos Literak Aug 15 '13 at 20:45
  • View received the exact absolute path, but keeps saying "not found". Watch my edit – erdbeerchen Aug 15 '13 at 21:44
  • Can you locate and open that file using some other application? If you can, then compile simple java class that opens this existing file. – Leos Literak Aug 16 '13 at 05:55
  • Wouldn't this be similar to using renderBinary() in Play 1.x? I still haven't find out how to use this in 2.x. – erdbeerchen Aug 16 '13 at 06:35
  • I used @Application.searchImage(qrPath) to log, whether image existed and could even read the contained QR-String. At least, there seemed to be something wrong with the view. – erdbeerchen Aug 19 '13 at 09:08
-1

This helped me: Play! framework 2.0: How to display multiple image?

Finaly I did it. The trick was not to do src=path but src=getImage(path). Still strange, but now it works.

routes

GET /tmp/*filepath  controllers.Application.getImage(filepath: String)

Application

public class Application extends Controller {

public static Result qrCode() {
    String msg = "I am a QR-String";
    BufferedImage image = (BufferedImage) QR.stringToImage(msg);
    File outputfile = null;

    try{
        outputfile = File.createTempFile("posQR",".png");
        ImageIO.write(image,"png",outputfile);
    }catch(IOException e){
        e.printStackTrace();
    }
    return ok(views.html.qrCode.render(outputfile.getAbsolutePath()));
}
...

public static Result getImage(String imgPath){
    return ok(new File(imgPath));
}
}

view qrCode

@(qrPath: String)
...
<img src="@routes.Application.getImage(qrPath)"/>

Thanks for your help :D

Community
  • 1
  • 1