我遇到了同样的问题。令人惊讶的是,玩游戏并不容易做到这一点。这是一个相关的控制器,它在 imagescalr 库的帮助下从外部路径提供缩略图。
package controllers;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.io.output.ByteArrayOutputStream;
import play.mvc.Controller;
import play.mvc.Result;
import play.Play;
import javax.imageio.*;
import static org.imgscalr.Scalr.*;
public class ServeThumbnail extends Controller {
public static Result at(String filename) {
response().setContentType("image");
ByteArrayOutputStream img_stream = null;
try {
File file = new File(Play.application().configuration().getString("home_dir") + "/pictures/"+filename);
BufferedImage thumbnail = createThumbnail(ImageIO.read(file));
img_stream = new ByteArrayOutputStream();
ImageIO.write(thumbnail, "jpg", img_stream);
} catch (FileNotFoundException e) {
return badRequest("image not found");
} catch (IOException e) {
return badRequest("image not found");
}
return ok(img_stream.toByteArray());
}
public static BufferedImage createThumbnail(BufferedImage img) {
img = resize(img, Method.ULTRA_QUALITY, 125, OP_ANTIALIAS, OP_BRIGHTER);
return pad(img, 4);
}
}