Scale a BufferedImage the fastest and easiest way

前端 未结 6 572
春和景丽
春和景丽 2020-12-28 15:03

The task: I have some images, I scale them down, and join them to one image. But I have a little problem with the implementation:

The concr

6条回答
  •  遥遥无期
    2020-12-28 15:29

    You can also use OpenCV Java library. It's resize operation is faster than Imgscalr's:

    Test

    Image 5184 x 3456 scaled to 150 x 100 (this is the smaller version because original file is bigger than 2mb):

    Imgscalr

    Dependency:

    
        org.imgscalr
        imgscalr-lib
        4.2
    
    

    Code:

    BufferedImage thumbnail = Scalr.resize(img,
                Scalr.Method.SPEED,
                Scalr.Mode.AUTOMATIC,
                150,
                100);
    

    Result image:

    Average time: 80 millis

    OpenCV

    Dependency:

    
        nu.pattern
        opencv
        2.4.9-4
    
    

    Convert BufferedImage to Mat object (have to):

    BufferedImage img = ImageIO.read(image); // load image
    byte[] pixels = ((DataBufferByte) img.getRaster().getDataBuffer())
                .getData();
    Mat matImg = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC3);
    matImg.put(0, 0, pixels);
    

    Code:

    Imgproc.resize(matImg, resizeimage, sz);
    

    Additional configuration (for windows):

    Add opencv_java249.dll to your JDK's bin directory.

    Result image:

    Average time: 13 millis

    Overall Results

    In the test just "resize" functions times are calculated. Imgscalr resized the given image in 80 milis where OpenCV done the same task in 13 millis. You can find the whole project below here to play around of it a little bit.

    As you asked also easy way, if the performance of the Imgscalr library is good for you then it is deadly-easy. Because to use OpenCV as you see a library file must be located at your all development environments and servers. Also you have to use Mat objects.

    Whole Project

    Pom.xml:

    
        4.0.0
    
        com.btasdemir
        testapp
        0.0.1-SNAPSHOT
        jar
    
        testapp
        http://maven.apache.org
    
        
            UTF-8
        
    
        
            
                junit
                junit
                3.8.1
                test
            
    
            
                org.imgscalr
                imgscalr-lib
                4.2
            
    
            
                nu.pattern
                opencv
                2.4.9-4
            
        
    
        
            
                 
                    org.bytedeco 
                    javacpp 
                    0.9 
                
            
        
    
    
    

    App.java:

    package com.btasdemir.testapp;
    
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferByte;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    import org.imgscalr.Scalr;
    import org.opencv.core.Core;
    import org.opencv.core.CvType;
    import org.opencv.core.Mat;
    import org.opencv.core.Size;
    import org.opencv.highgui.Highgui;
    import org.opencv.imgproc.Imgproc;
    
    
    /**
     * Hello world!
     *
     */
    public class App 
    {
        public static void main( String[] args ) throws IOException
        {
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    
            File image = new File("C:\\your_dir\\test.jpg");
            BufferedImage img = ImageIO.read(image); // load image
            long startTime = System.currentTimeMillis();//imgscalr------------------------------------------------------
            //resize to 150 pixels max
            BufferedImage thumbnail = Scalr.resize(img,
                    Scalr.Method.SPEED,
                    Scalr.Mode.AUTOMATIC,
                    150,
                    100);
    //      BufferedImage thumbnail = Scalr.resize(img,
    //                Scalr.Method.SPEED,
    //                Scalr.Mode.AUTOMATIC,
    //                150,
    //                100,
    //                Scalr.OP_ANTIALIAS);
            System.out.println(calculateElapsedTime(startTime));//END-imgscalr------------------------------------------------------
            File outputfile = new File("C:\\your_dir\\imgscalr_result.jpg");
            ImageIO.write(thumbnail, "jpg", outputfile);
    
    
            img = ImageIO.read(image); // load image
            byte[] pixels = ((DataBufferByte) img.getRaster().getDataBuffer())
                    .getData();
            Mat matImg = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC3);
            matImg.put(0, 0, pixels);
    
            Mat resizeimage = new Mat();
            Size sz = new Size(150, 100);
            startTime = System.currentTimeMillis();//opencv------------------------------------------------------
    
            Imgproc.resize(matImg, resizeimage, sz);
    //      Imgproc.resize(matImg, resizeimage, sz, 0.5, 0.5, Imgproc.INTER_CUBIC);
    
            System.out.println(calculateElapsedTime(startTime));//END-opencv------------------------------------------------------
            Highgui.imwrite("C:\\your_dir\\opencv_result.jpg", resizeimage);
    
    
        }
    
        protected static long calculateElapsedTime(long startTime) {
            long stopTime = System.currentTimeMillis();
            long elapsedTime = stopTime - startTime;
            return elapsedTime;
        }
    }
    

提交回复
热议问题