Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 保存图像时出现问题我得到零kb_Java_Image_Save_Javax.imageio - Fatal编程技术网

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

Java 保存图像时出现问题我得到零kb,java,image,save,javax.imageio,Java,Image,Save,Javax.imageio,我遇到了一个问题,当我保存图像时,我无法打开它,因为它是空的,并且大小为零kb。我正在从文件夹中读取图像,然后将其大小更改为100x100并保存,但它不起作用。以下是我迄今为止编写的代码: public BufferedImage resizeImageToPreview() { final String SOURCE ="/Library/glassfishv3/glassfishv3/glassfish/domains/domain1/eclipseApps/LaFamilyEar/La

我遇到了一个问题,当我保存图像时,我无法打开它,因为它是空的,并且大小为零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;
 }
试着换衣服

String extension = ".jpg";
…到

String extension = "jpg";
显然,在相关点的文件名中添加一个“.”

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

尝试以下示例:

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

哇,它最有效;-)非常感谢,我正在发布代码,以防有人在寻找相同的答案。再次感谢,这个论坛太棒了。没问题,如果你喜欢这个答案,你应该单击旁边的“接受”按钮来接受它(概述的“v”):)哇,它最有效;-)非常感谢,我正在发布代码,以防有人在寻找相同的答案。再次感谢,这个论坛太棒了。