本文使用javaCV封装后的OpenCv
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.5</version>
</dependency>
在openCv中有一函数用于读取图片 imread(),保存图片imwrite()
由于javaCv二次封装,所以函数目录和openCv目录不同,javaCv函数在opencv_imgcodecs中,因为javaCv目前没有完整的文档所以只能查看源代码一个个找,后面自己使用别的功能时是需要自己看源代码的,反正挺苦逼的。
public static void main(String[] args) {
Mat mat = opencv_imgcodecs.imread("D:\\leiningen\\cat.jpg");
ImageViewer imageViewer = new ImageViewer(mat);
imageViewer.imshow();
}
ImageViewer 我着实没有找到在javaCv中相应的类,不过找到一个opencv_highgui.imshow()方法但是用不了,我看了里面的注释需要搭配线程使用反正我没搞出来。所以我也把这个类ImageViewer源代码贴出来吧(我放在最后了)。
public static void main(String[] args) {
Mat mat = opencv_imgcodecs.imread("D:\\leiningen\\cat.jpg");
opencv_imgcodecs.imwrite("D:\\leiningen\\cat1.png",mat);
}
从代码中可以看到陌生的对象Mat,这个是矩阵的意思,这个对象是openCv的核心,简单介绍一下。
首先我们来创建一个矩阵开始。
public static void main(String[] args) {
//创建RGB颜色当前是蓝色
Scalar scalar = new Scalar(255, 0, 0, 0); // R G B 0
//Size:width、height
Mat mat = new Mat(new Size(100,200), opencv_core.CV_8UC3,scalar);
ImageViewer imageViewer = new ImageViewer(mat);
imageViewer.imshow();
}
上面代码片先是读取在保存,唯一区别就是保存时用的格式使用的是png,不需要进行图像编码和令人发狂的字节操作
按照官方说法:
JPEG可以使用CV_IMWRITE_JPEG_OUALITY参数,参数值范围在0 ~ 100 (值越大图像质量越高)默认值是95。
PNG,可以使用0 ~ 9 作为压缩程度的参数值,值越大图像质量越小,压缩时间越长,默认值是3.
package com.cloud.iot.util;
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.opencv.opencv_core.Mat;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.nio.ByteBuffer;
public class ImageViewer {
private JLabel imageView;
private Mat image;
private String windowName;
public ImageViewer(Mat image) {
this.image = image;
}
public ImageViewer(Mat image, String windowName) {
this.image = image;
this.windowName = windowName;
}
/**
* 窗口展示
*/
public void imshow() {
setSystemLookAndFeel();
Image loadedImage = toBufferedImage(image);
JFrame frame = createJFrame(windowName, image.arrayWidth(), image.arrayHeight());
imageView.setIcon(new ImageIcon(loadedImage));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 用户点击窗口关闭
}
private void setSystemLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
private JFrame createJFrame(String windowName, int width, int height) {
JFrame frame = new JFrame(windowName);
imageView = new JLabel();
final JScrollPane imageScrollPane = new JScrollPane(imageView);
imageScrollPane.setPreferredSize(new Dimension(width, height));
frame.add(imageScrollPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
return frame;
}
private Image toBufferedImage(Mat matrix) {
int type = BufferedImage.TYPE_BYTE_GRAY;
if (matrix.channels() > 1) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
ByteBuffer byteBuffer = matrix.getByteBuffer();
byte[] b = new byte[byteBuffer.remaining()];
byteBuffer.get(b,0,b.length);
BufferedImage image = new BufferedImage(matrix.cols(), matrix.rows(), type);
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, 0, b.length);
return image;
}
}
今天就到这·······································································over