你也可以使用OpenCV Java库。它的调整大小操作比Imgscalr的更快:
测试
图像 5184 x 3456 缩放到 150 x 100(这是较小的版本,因为原始文件大于 2mb):
Imgscalr
屬地:
<dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>4.2</version>
</dependency>
法典:
BufferedImage thumbnail = Scalr.resize(img,
Scalr.Method.SPEED,
Scalr.Mode.AUTOMATIC,
150,
100);
结果图像:
平均时间:80 毫
开放CV
屬地:
<dependency>
<groupId>nu.pattern</groupId>
<artifactId>opencv</artifactId>
<version>2.4.9-4</version>
</dependency>
将 BufferedImage 转换为 Mat 对象(必须):
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);
法典:
Imgproc.resize(matImg, resizeimage, sz);
其他配置(用于窗口):
将opencv_java249.dll添加到 JDK 的 bin 目录中。
结果图像:
平均时间:13毫
总体结果
在测试中,仅计算“调整大小”函数时间。Imgscalr将给定图像的大小调整为80毫英寸,其中OpenCV在13毫英寸内完成了相同的任务。你可以在这里找到下面的整个项目来稍微玩一下。
正如你问的,也很容易,如果Imgscalr库的性能对你有好处,那么它就非常容易。因为要使用 OpenCV,因为您看到的库文件必须位于所有开发环境和服务器上。此外,您必须使用Mat对象。
整个项目
啪.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.btasdemir</groupId>
<artifactId>testapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>4.2</version>
</dependency>
<dependency>
<groupId>nu.pattern</groupId>
<artifactId>opencv</artifactId>
<version>2.4.9-4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>0.9</version>
</plugin>
</plugins>
</build>
</project>
应用.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;
}
}