decrease image resolution in java

前端 未结 4 967
[愿得一人]
[愿得一人] 2020-12-01 15:09

I need to reduce the size of image(not the width and height) using Java program. Is their any good API available for this?

I need to reduce the size from 1MB to abou

4条回答
  •  孤城傲影
    2020-12-01 15:21

    According to this blog post: http://i-proving.com/2006/07/06/java-advanced-imaging/ you can use the Java Advanced Imaging Library to do what you want. The following sample code should provide you a good starting point. This will resize the image, both in height and width, as well as image quality. Once your image is of the desired file size, you can scale it back up to your desired pixel height and width when you display the image.

    // read in the original image from an input stream
    SeekableStream s = SeekableStream.wrapInputStream(
      inputStream, true);
    RenderedOp image = JAI.create("stream", s);
    ((OpImage)image.getRendering()).setTileCache(null);
    
    // now resize the image
    
    float scale = newWidth / image.getWidth();
    
    RenderedOp resizedImage = JAI.create("SubsampleAverage", 
        image, scale, scale, qualityHints);
    
    
    // lastly, write the newly-resized image to an
    // output stream, in a specific encoding
    
    JAI.create("encode", resizedImage, outputStream, "PNG", null);
    

提交回复
热议问题