How do you create a thumbnail image out of a JPEG in Java?

后端 未结 13 2289
日久生厌
日久生厌 2020-11-29 16:51

Can someone please help with some code for creating a thumbnail for a JPEG in Java.

I\'m new at this, so a step by step explanation would be appreciated.

相关标签:
13条回答
  • 2020-11-29 17:25

    I've used Thumbnailator! It solved my problem with two lines of code.

    https://github.com/coobird/thumbnailator

    0 讨论(0)
  • 2020-11-29 17:25

    I have gone through a blog according to which you have following options -

    1. For simple RGB files use ImageScalr . ImageIO class is used for reading files and ImageScalr to create thumbnails
    2. For supporting RGB + CYMK, use ImageIO and JAI (Java Advanced Imaging) API for reading files and ImageScalr to create thumbnail.
    3. In case you don’t know what file formats, color mode you are going to deal with, safest option is to use ImageMagick.

    Here is link that gives a complete answer with code snippets.

    0 讨论(0)
  • 2020-11-29 17:26

    the Java code above (with the scale / getCompatibleImage methods) worked great for me, but when I deployed to a server, it stopped working, because the server had no display associated with it -- anyone else with this problem can fix it by using: BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

    instead of BufferedImage bi = getCompatibleImage(w, h);

    and deleting the getCompatibleImage method

    (later note -- it turns out this works great for most images, but I got a bunch from my companys marketing department that are 32 bit color depth jpeg images, and the library throws an unsupported image format exception for all of those :( -- imagemagick / jmagick are starting to look more appealing)

    0 讨论(0)
  • 2020-11-29 17:26

    I know this is a pretty old post. I have been looking for a solution to generate the thumbnail so end up using this

    Thumbnails.of(originalImage).scale(0.25).asBufferedImage();
    

    if you are using for mobile would suggest to set the scale to 0.45

    Thumbnails.of(originalImage).scale(0.45).asBufferedImage();
    

    https://github.com/coobird/thumbnailator

    This is certainly much faster using the Graphics2D as have tested the both options.

    0 讨论(0)
  • 2020-11-29 17:28

    The JMagick library (and implementation of ImageMagick in Java) will have what you need.

    0 讨论(0)
  • 2020-11-29 17:28

    There are many image processing frameworks available that you can do this with just a few lines. The example below generates the thumbnails in different resolutions (given a width as reference) using Marvin Framework. The three thumbnails were generated in 92 ms.

    input:

    output:

    import static marvin.MarvinPluginCollection.*;
    
    MarvinImage image = MarvinImageIO.loadImage("./res/input.jpg");
    MarvinImage scaledImage = new MarvinImage(1,1);
    
    scale(image, scaledImage, 250);
    MarvinImageIO.saveImage(scaledImage, "./res/output_x250.jpg");
    
    scale(image, scaledImage, 150);
    MarvinImageIO.saveImage(scaledImage, "./res/output_x150.jpg");
    
    scale(image, scaledImage, 50);
    MarvinImageIO.saveImage(scaledImage, "./res/output_x50.jpg");
    
    0 讨论(0)
提交回复
热议问题