2

I'm using GAE[JAVA] to do some image processing. GAE will not allow write file to disk, so I want my App allow type a URL, then I want to check whether this specified URL represents an image, if it is, I want to get the height and width of it.

Does anyone provide some solution to show me how to implement this?

Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106
yuyue007
  • 1,219
  • 3
  • 14
  • 25

2 Answers2

5

You could use the GAE Image API. A simple sample to solve your problem would be:

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        final InputStream inputStream = url.openStream();
        int read;
        while ((read = inputStream.read()) != -1) {
            baos.write(read);
        }

        final Image image = ImagesServiceFactory.makeImage(baos.toByteArray());

        resp.getWriter().println("image width: " + image.getWidth());
        resp.getWriter().println("image height: " + image.getHeight());
    } catch (final IOException e) {
        resp.getWriter().println("image doesn't exists!");
    } catch (final IllegalArgumentException e) {
        resp.getWriter().println("invalid image!");
    }

Complete Servlet to execute this:

package foo.bar;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesServiceFactory;

@SuppressWarnings("serial")
public class MyFirstWebAppServlet extends HttpServlet {
    @Override
    public void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
        URL url;
        if ("exists".equals(req.getParameter("image"))) {
            url = new URL("https://www.google.com/logos/classicplus.png");
        } else if ("notImage".equals(req.getParameter("image"))) {
            url = new URL("http://www.google.com");
        } else {
            url = new URL("http://foo.bar/image.png");
        }

        resp.setContentType("text/plain");
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            final InputStream inputStream = url.openStream();
            int read;
            while ((read = inputStream.read()) != -1) {
                baos.write(read);
            }

            final Image image = ImagesServiceFactory.makeImage(baos.toByteArray());

            resp.getWriter().println("image width: " + image.getWidth());
            resp.getWriter().println("image height: " + image.getHeight());
        } catch (final IOException e) {
            resp.getWriter().println("image doesn't exists!");
        } catch (final IllegalArgumentException e) {
            resp.getWriter().println("invalid image!");
        }

    }
}
Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106
-2

Maybe this way is strange,but I think you can use javascript

var img=new Image();
img.src="http://www.google.com.hk/images/nav_logo107.png";
img.height;
img.width;

enter image description here

if the image isn't exits,it will throw a 404 (Not Found) problem.

htynkn
  • 104
  • 5
  • I don't want to use JS to processing this task. Of cause, javascript is very easy to handle this task. Any idea of Java code? – yuyue007 Jul 01 '12 at 10:11