我正在开发一个从用户那里接收图像资源的webbapp,但我想验证这些图像要上传到具有600X800等特定尺寸。
我正在努力寻找答案,但我已经没有选择了:)。
如果有人以前做过这件事会很棒。
谢谢你的帮助将是apreciated
答案 0 :(得分:1)
尝试
BufferedImage bi = ImageIO.read (file);
bi.getWidth();//width
bi.getHeight ();//height
答案 1 :(得分:0)
最后我确实找到了这样做的方法:我们首先得到它要上传的文件,然后我使用我在堆栈溢出时找到的创建类,找到dimesions的宽度和高度,我只是玩如果那么,继承人的代码,希望帮助别人!!
public class AddPromo extends WebPage {
/**
*
*/
private FileUploadField fileUpload;
private String UPLOAD_FOLDER = "..\\Promociones\\";
private static final long serialVersionUID = 1L;
@SuppressWarnings({ "rawtypes", "unchecked" })
public AddPromo(){
add(new Label("user",ActiveUser.USER));
//Add Feedback panel
add(new FeedbackPanel("feedback_2"));
//LOGOUT LINK
Logout logoutLink = new Logout("logout");
add(logoutLink);
//Add backlink to SuccesPage
add(new Link("link_atras"){
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void onClick(){
SuccesPage nueva = new SuccesPage("",1);
setResponsePage(nueva);
}
});
//Definitions for date attributes
final Locale selectedLocale = Session.get().getLocale();
add(new StaticImage("image_test",new Model("http://1-ps.googleusercontent.com/h/www.bizreport.com/2011/02/03/200x200xandroid-logo-200x200.jpg.pagespeed.ic.SONOBLzFc5.jpg")));
Form<?> form = new Form<Void>("form") {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit() {
JPEGDim dims = new JPEGDim();
final FileUpload uploadedFile = fileUpload.getFileUpload();
if (uploadedFile != null) {
// write to a new file
File newFile = new File(UPLOAD_FOLDER
+ uploadedFile.getClientFileName());
try {
final Dimension d = dims.getJPEGDimension(newFile);
if (d.getWidth()==1024 || d.getHeight()==768 ) {
try {
if (newFile.exists()) {
newFile.delete();
}
newFile.createNewFile();
uploadedFile.writeTo(newFile);
info("saved file: " + uploadedFile.getClientFileName());
info("Imagen cumple con dimensiones " + d.getHeight() + " x " + d.getWidth());
} catch (Exception e) {
throw new IllegalStateException("Error "+e.toString());
}
} else {
error("Archivo no valido... " + + d.getHeight() + " x " + d.getWidth());
}
} catch (IOException e1) {
// TODO Auto-generated catch block
error(e1.toString());
}
}
}
};
// Enable multipart mode (need for uploads file)
form.setMultiPart(true);
// max upload size, 10k
form.setMaxSize(Bytes.megabytes(10));
form.add(fileUpload = new FileUploadField("fileUpload"));
add(form);
}
class JPEGDim {
public Dimension getJPEGDimension(File f) throws IOException {
FileInputStream fis = new FileInputStream(f);
// check for SOI marker
if (fis.read() != 255 || fis.read() != 216)
throw new RuntimeException("SOI (Start Of Image) marker 0xff 0xd8 missing");
Dimension d = null;
while (fis.read() == 255) {
int marker = fis.read();
int len = fis.read() << 8 | fis.read();
if (marker == 192) {
fis.skip(1);
int height = fis.read() << 8 | fis.read();
int width = fis.read() << 8 | fis.read();
d = new Dimension(width, height);
break;
}
fis.skip(len - 2);
}
fis.close();
return d;
}
}
}