如何使用Java缩放图像。(How to scale an image using Java.)
优质
小牛编辑
134浏览
2023-12-01
问题描述 (Problem Description)
如何使用Java缩放图像。
解决方案 (Solution)
以下是使用Java缩放图像的程序。
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class ScalingAnImage {
public static void main(String args[]) {
//Loading the OpenCV core library
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
//Reading the Image from the file and storing it in to a Matrix object
String file = "C:/opencv/logo.jpg";
Mat src = Imgcodecs.imread(file);
//Creating an empty matrix to store the result
Mat dst = new Mat();
//Scaling the Image
Imgproc.resize(src, dst, new Size(src.rows()/3, src.rows()/3), 0, 0,
Imgproc.INTER_AREA);
//Writing the image
Imgcodecs.imwrite("C:/opencv/scale_output.jpg", dst);
System.out.println("Image Processed");
}
}
输入 (Input)
![OpenCV复制输入](https://imgs.xnip.cn/cj/docimg/846/4f2e4a5b-264a-4dad-8f4d-6f5dee38db7a.jpg)
输出 (Output)
![比例输出](https://imgs.xnip.cn/cj/docimg/846/2a49505d-b086-4cec-8eb1-f2e9602b1fa8.jpg)