2012-08-07 69 views
0

IM開發webbapp至極接收來自用戶的圖像資源,但我想驗證這些圖像上傳到具有特定的尺寸一樣600X800等驗證圖像尺寸

我想鰭的答案,但進出口運行出於選擇:)。

如果以前有人這樣做會很好。

感謝您的幫助將apreciated

回答

1

嘗試

BufferedImage bi = ImageIO.read (file); 
bi.getWidth();//width 
bi.getHeight();//height 
+0

它工作正常,我自己貼上面的作品,但只支持JPG答案,跛腳!如果我嘗試上傳一個PNG它不工作:) – 2012-08-10 16:36:16

+0

但你的!謝謝! – 2012-08-10 19:35:59

0

最後我發現做到這一點的方法:我們首先要獲取其將要上傳,然後我用一個創建的類至極我發現這裏對堆棧溢出的文件,找到尺寸的寬度和高度,我只是玩如果是的話,繼承人的代碼,希望可以幫助別人!

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; 
    } 

} 

}