使用OpenCV库,您可以执行图像处理操作,如图像滤波、几何图像变换、颜色空间转换、直方图等。
每当使用Imgcodecs类的imread()方法读取图像的内容时,结果会被读入Matrix对象中。
您可以使用imwrite()方法来写入/保存图像。该方法接受两个参数,分别是:
File - 表示要存储结果的文件路径的字符串值。
Img - 包含要保存图像的数据的矩阵对象。
以下Java示例将 cat.jpg图像的内容作为灰度图像读取,并使用另一个名称重新保存。
import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; public class WritingImages { 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 ="D://images//cat.jpg"; Mat matrix = Imgcodecs.imread(file); System.out.println("Image Loaded"); String file2 = "D://images//sample_resaved.jpg"; //Writing the image Imgcodecs.imwrite(file2, matrix); System.out.println("Image Saved"); } }
Input: cat.jpg
Output: sample_resaved.jpg