|
@@ -0,0 +1,112 @@
|
|
|
+package com.test.xiaoshi.test.common.util;
|
|
|
+
|
|
|
+import org.opencv.core.CvType;
|
|
|
+import org.opencv.core.Mat;
|
|
|
+import org.opencv.core.MatOfByte;
|
|
|
+import org.opencv.imgcodecs.Imgcodecs;
|
|
|
+
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import javax.xml.bind.DatatypeConverter;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.awt.image.DataBufferByte;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author Tang_J on 2022/4/20 下午 02:00
|
|
|
+ * @Description TODO
|
|
|
+ **/
|
|
|
+public class MatUtil {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Mat 转换成 byte[]
|
|
|
+ *
|
|
|
+ * @param mat Mat对象
|
|
|
+ * @return byte[]
|
|
|
+ */
|
|
|
+ public static byte[] toByteArray(Mat mat) {
|
|
|
+ MatOfByte mob = new MatOfByte();
|
|
|
+ Imgcodecs.imencode(".jpg", mat, mob);
|
|
|
+ return mob.toArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * byte[] 转 图片Base64
|
|
|
+ *
|
|
|
+ * @param bytes byte[]
|
|
|
+ * @return Base64
|
|
|
+ */
|
|
|
+ public static String byteArray2Base64(byte[] bytes) {
|
|
|
+ return DatatypeConverter.printBase64Binary(bytes);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Mat 转 图片Base64
|
|
|
+ *
|
|
|
+ * @param mat Mat
|
|
|
+ * @return 图片Base64
|
|
|
+ */
|
|
|
+ public static String mat2Base64(Mat mat) {
|
|
|
+ return byteArray2Base64(toByteArray(mat));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Mat 转换成 BufferedImage
|
|
|
+ *
|
|
|
+ * @param mat Mat
|
|
|
+ * @return BufferedImage
|
|
|
+ */
|
|
|
+ public static BufferedImage toBufferedImage(Mat mat) throws IOException {
|
|
|
+ byte[] buffer = toByteArray(mat);
|
|
|
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
|
|
|
+ return ImageIO.read(inputStream);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Base64 转换成 Mat
|
|
|
+ *
|
|
|
+ * @param base64 图片Base64
|
|
|
+ * @return Mat
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * BufferedImage 转换成 Mat
|
|
|
+ *
|
|
|
+ * @param original 要转换的BufferedImage
|
|
|
+ * @param imgType bufferedImage的类型 如 BufferedImage.TYPE_3BYTE_BGR
|
|
|
+ * @param matType 转换成mat的type 如 CvType.CV_8UC3
|
|
|
+ * https://wenku.baidu.com/view/3cd5e54b5bfafab069dc5022aaea998fcc2240a1.html
|
|
|
+ */
|
|
|
+ public static Mat bufImg2Mat(BufferedImage original, int imgType, int matType) {
|
|
|
+ if (original == null) {
|
|
|
+ throw new IllegalArgumentException("original == null");
|
|
|
+ }
|
|
|
+ // Don't convert if it already has correct type
|
|
|
+ if (original.getType() != imgType) {
|
|
|
+ // Create a buffered image
|
|
|
+ BufferedImage image = new BufferedImage(original.getWidth(), original.getHeight(), imgType);
|
|
|
+ // Draw the image onto the new buffer
|
|
|
+ Graphics2D g = image.createGraphics();
|
|
|
+ try {
|
|
|
+ g.setComposite(AlphaComposite.Src);
|
|
|
+ g.drawImage(original, 0, 0, null);
|
|
|
+ original = image;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ g.dispose();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ byte[] pixels = ((DataBufferByte) original.getRaster().getDataBuffer()).getData();
|
|
|
+ Mat mat = Mat.eye(original.getHeight(), original.getWidth(), matType);
|
|
|
+ mat.put(0, 0, pixels);
|
|
|
+ return mat;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|