私はGAE[JAVA]を使用して画像処理を行っています。GAEはディスクへのファイルの書き込みを許可しないので、アプリにURLの入力を許可し、この指定されたURLが画像を表しているかどうかを確認します。表している場合は、画像の高さと幅を取得します。
誰かがこれを実装する方法を私に示すためのいくつかの解決策を提供しますか?
私はGAE[JAVA]を使用して画像処理を行っています。GAEはディスクへのファイルの書き込みを許可しないので、アプリにURLの入力を許可し、この指定されたURLが画像を表しているかどうかを確認します。表している場合は、画像の高さと幅を取得します。
誰かがこれを実装する方法を私に示すためのいくつかの解決策を提供しますか?
GAE Image API を使用できます。問題を解決するための簡単なサンプルは次のとおりです。
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!");
}
これを実行する完全なサーブレット:
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!");
}
}
}
この方法は奇妙かもしれませんが、JavaScriptを使用できると思います
var img=new Image();
img.src="http://www.google.com.hk/images/nav_logo107.png";
img.height;
img.width;
画像が終了しない場合は、404(見つかりません)の問題が発生します。