search

Home  >  Q&A  >  body text

图形学 - Java修改图片尺寸,总是报内存溢出怎么解决?

大家讲道理大家讲道理2911 days ago758

reply all(5)I'll reply

  • 黄舟

    黄舟2017-04-18 10:06:01

    GraphicsMagick+im4java can handle it. GraphicsMagick does not need to read the entire image into the memory. It is much more efficient than using the native one. You can search and see. In the past, we used GraphicsMagick to process image cropping

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:06:01

    You can try it ImageMagick

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:06:01

    You can try it, I’m not sure if it will work either

    int w = 400;
    int h = 300;
    BufferedImage oldImg = ImageIO.read(new File("F:\sample.jpg"));
    Image t = oldImg.getScaledInstance(w, h, Image.SCALE_FAST);
    BufferedImage newImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = newImg.createGraphics();
    g.drawImage(t, 0, 0, w, h, null);
    g.dispose();
    ImageIO.write(newImg, "jpg", new File("F:\small.jpg"));

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:06:01

        File srcFile = new File(srcImgPath);  
        Image srcImg = ImageIO.read(srcFile);  
        BufferedImage buffImg = null;  
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
        buffImg.getGraphics().drawImage(  
                srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0,  
                0, null);  
    
        ImageIO.write(buffImg, "JPEG", new File(distImgPath));  

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:06:01

    The idea can be changed. Obviously the compressed image can be stored in the memory, and large images should be partially read and processed before being put into the compressed graphics. In general, this problem is not difficult, just change the idea.

    reply
    0
  • Cancelreply