简体   繁体   中英

Java : Determine if String is an URL(without www or http), take its screenshot

I am working on a Spring-MVC webapplication in which we are trying to get a screenshot of an URL. Currently I am using PhantomJS for that task, but it's too slow(>10seconds). Also, the URL's have to be with http/https and www for detecting that it's an URL. As this is a chat application, there can be simple URL's which users add like helloworld.com . Any help would be nice. Thank you.

Code:

 String[] words = message.split(" ");
                for( String item : words ){
                     boolean val = ResourceUtils.isUrl(item);
                    if(val){
                        urlIdentifier = calcUrl(item);
                        break;
                    }else {
                        System.out.println("Url false is "+item);
                    }
                }

                if(urlIdentifier!=null){
                    replies.setPreviewIdentifier(urlIdentifier);
                    input.put("preview_identifier",urlIdentifier);
                }

Method to calculate screenshot :

   private String calcUrl(String website){
        try {
            String identifier = String.valueOf(new BigInteger(130, random).toString(32));
            String previewLocation = msg + "chatthumbs/" + identifier ;

            Process proc = Runtime.getRuntime().exec("phantomjs --ssl-protocol=any " +
                    "/home/deploy/phantom/rasterizepdf.js " +" "+website+" " +previewLocation);
            proc.waitFor();
            BufferedImage image = ImageIO.read(new File("/home/akshay/testme.png"));
            if(image!=null){

                if (image.getWidth() > image.getHeight()) {
                    image = Scalr.resize(image, Scalr.Mode.FIT_TO_HEIGHT, 250, 250);
                } else {
                    image = Scalr.resize(image, Scalr.Mode.FIT_TO_WIDTH, 250, 250);
                }
                image = Scalr.crop(image, 250, 250);
                ImageIO.write(image, "png", new File(previewLocation));
            }
            return identifier;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

Any help would be nice. Thank you.

  1. (a) I think the process of taking a screen shot is taking time. Is this code running on the same device as the chat screen? Why not use java.awt.Robot to take the screen shot ? or just save the text why do you need a screen shot?

(b) Is the system too busy/ Use on a SSD to see if faster?

(c) But curious as to the end application, is this part of a web app? How will your code run on the client systems? Or will you java agent be installed on all user systems that monitors the web page and takes screen shots? Then why use a web page, use a java app to chat, and parse the text as typed.

  1. Parsing the text. What if user types/ pastes a long message? Are you parsing everything once or on change? Think of ways to improve that if it seems to be a problem. Ignore if not the immediate issue now. Also if the msg is too long the parsing can take a lot of time. maybe process after every key press or change event (if paste) keep local js copy of previous text to get diff?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM