保存图像时出现问题,我得到零 kb

发布于 2024-09-29 06:55:45 字数 1353 浏览 0 评论 0原文

我遇到问题,当我保存图像时,我无法打开它,因为它是空的并且大小为零 kb。我正在从文件夹中读取图像,然后将大小更改为 100x100 并保存,但它不起作用。这是我到目前为止编写的代码:

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

I'm having a problem, when I save my image I can't open it because it's empty and the size is zero kb. I'm reading the image from a folder and then I change the size to 100x100 and save it but it's not working. Here's the code I've written so far:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆° 2024-10-06 06:55:45

尝试将..

String extension = ".jpg";

..更改为..

String extension = "jpg";

显然,添加一个“.”到相关点的文件名。

如果这对您不起作用,请尝试发布 SSCCE

Try changing ..

String extension = ".jpg";

..to..

String extension = "jpg";

Obviously, add a "." to the file name at the relevant point.

If that does not work for you, try posting an SSCCE.

夕嗳→ 2024-10-06 06:55:45

试试这个例子:

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

Try this example:

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"));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文