2010-10-29 26 views
2

J'ai un problème, quand je sauve mon image je ne peux pas l'ouvrir parce qu'il est vide et la taille est zéro kb. Je lis l'image d'un dossier et puis je change la taille à 100x100 et l'enregistre mais cela ne fonctionne pas. Voici le code que je l'ai écrit jusqu'à présent: ""problème avec l'enregistrement de l'image je reçois zéro kb

public BufferedImage resizeImageToPreview() { 

final String SOURCE ="/Library/glassfishv3/glassfishv3/glassfish/domains/domain1/eclipseApps/LaFamilyEar/LaFamily_war/temp"; 
    File source = new File(SOURCE); 

    BufferedImage image = null; 

    //Read images and convert them to BufferedImages 
    for (File img : source.listFiles()) { 
    try { 

    image = ImageIO.read(img); 

    } catch (IOException e) { 
    e.printStackTrace(); 
    } 
    //Get image width and height 
    int w = image.getWidth(); 
    int h = image.getHeight(); 

    //Change the width and height to the image to 100x100 
// BufferedImage dimg = new BufferedImage(100, 100, image.getType()); 

    //Create graphics to be able to paint or change your image 
    Graphics2D g = image.createGraphics(); 

    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
    g.drawImage(image, 0, 0, 100, 100, 0, 0, w, h, null); 
    g.dispose(); 

    String extension = ".jpg"; 
    File dest = new File("/Users/ernestodelgado/Kurs_Java/EjbWorkspace/LaFamily/WebContent/small/"+img.getName()); 
    try { 

    ImageIO.write(image, extension, dest); 

    } catch (IOException e) { 
    e.printStackTrace(); 
    } 

    } 

    return image; 
} 

Répondre

1

Essayez de changer ..

String extension = ".jpg"; 

..pour ..

String extension = "jpg"; 

De toute évidence, ajoutez un au nom du fichier au point pertinent. Si cela ne fonctionne pas pour vous, essayez de publier un SSCCE.

+0

wow ça a marché u da best ;-) Merci beaucoup je poste le code au cas où quelqu'un cherche la même réponse merci encore, ce forum est génial. – madcoderz

2

Essayez cet exemple:

public class MakeSmaller { 
    public static void main(String... args) throws MalformedURLException, 
    IOException { 

     String url = "http://actionstalk.com/wp-content/uploads/2007/11/google_logo_3600x1500.jpg"; 
     BufferedImage orig = ImageIO.read(new URL(url)); 

     BufferedImage scaled = new BufferedImage(50, 50, orig.getType()); 

     Graphics2D g = (Graphics2D) scaled.getGraphics(); 
     g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
       RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
     g.drawImage(orig, 0, 0, scaled.getWidth(), scaled.getHeight(), null); 
     g.dispose(); 

     ImageIO.write(scaled, "jpg", new File("test.jpg")); 
    } 
} 
+0

wow cela a fonctionné u da meilleur ;-) merci beaucoup je poste le code au cas où quelqu'un cherche la même réponse merci encore, ce forum est génial. – madcoderz

+0

Pas de problème, si vous avez aimé la réponse, vous devez l'accepter en cliquant sur le bouton Accepter à côté de celui-ci (le «v» décrit). :) – dacwe