stream、base64、bytes、BufferedImage、Mat对象的相互转换工具类

stream、base64、bytes、BufferedImage、Mat对象的相互转换工具类

  1 package com.sxh.ocr.provider.common.util;
  2 
  3 import org.apache.poi.util.IOUtils;
  4 import org.opencv.core.CvType;
  5 import org.opencv.core.Mat;
  6 import org.opencv.core.MatOfByte;
  7 import org.opencv.imgcodecs.Imgcodecs;
  8 import org.springframework.util.Base64Utils;
  9 import sun.misc.BASE64Decoder;
 10 
 11 import javax.imageio.ImageIO;
 12 import java.awt.*;
 13 import java.awt.image.BufferedImage;
 14 import java.awt.image.DataBufferByte;
 15 import java.io.ByteArrayInputStream;
 16 import java.io.IOException;
 17 import java.io.InputStream;
 18 
 19 /**
 20  * 流处理工具
 21  *
 22  * @author simm
 23  */
 24 public class MyStreamUtils {
 25     /**
 26      * 转换成base64编码
 27      *
 28      * @param inputStream
 29      * @return
 30      * @throws IOException
 31      */
 32     public static String streamToBase64(InputStream inputStream) throws IOException {
 33         return Base64Utils.encodeToString(IOUtils.toByteArray(inputStream));
 34     }
 35 
 36     /**
 37      * 装换回编码
 38      *
 39      * @param correctMat
 40      * @return
 41      */
 42     public static String catToBase64(Mat correctMat) {
 43         return bufferToBase64(toByteArray(correctMat));
 44     }
 45 
 46     /**
 47      * 转换成base64编码
 48      *
 49      * @param buffer
 50      * @return
 51      */
 52     public static String bufferToBase64(byte[] buffer) {
 53         return Base64Utils.encodeToString(buffer);
 54     }
 55 
 56     /**
 57      * base64编码转换成字节数组
 58      *
 59      * @param base64Str
 60      * @return
 61      */
 62     public static byte[] base64ToByteArray(String base64Str) {
 63         return Base64Utils.decodeFromString(base64Str);
 64     }
 65 
 66     /**
 67      * base64 编码转换为 BufferedImage
 68      *
 69      * @param base64
 70      * @return
 71      */
 72     public static BufferedImage base64ToBufferedImage(String base64) {
 73         BASE64Decoder decoder = new sun.misc.BASE64Decoder();
 74         try {
 75             byte[] bytes1 = decoder.decodeBuffer(base64);
 76             ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
 77             return ImageIO.read(bais);
 78         } catch (IOException e) {
 79             e.printStackTrace();
 80         }
 81         return null;
 82     }
 83 
 84     /**
 85      * mat转换成bufferedImage
 86      *
 87      * @param matrix
 88      * @return
 89      */
 90     public static byte[] toByteArray(Mat matrix) {
 91         MatOfByte mob = new MatOfByte();
 92         Imgcodecs.imencode(".jpg", matrix, mob);
 93         return mob.toArray();
 94     }
 95 
 96     /**
 97      * mat转换成bufferedImage
 98      *
 99      * @param matrix
100      * @return
101      */
102     public static BufferedImage toBufferedImage(Mat matrix) throws IOException {
103         byte[] buffer = toByteArray(matrix);
104         ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
105         return ImageIO.read(bais);
106     }
107 
108     /**
109      * base64转Mat
110      *
111      * @param base64
112      * @return
113      * @throws IOException
114      */
115     public static Mat base642Mat(String base64) throws IOException {
116 
117         return bufImg2Mat(base64ToBufferedImage(base64), BufferedImage.TYPE_3BYTE_BGR, CvType.CV_8UC3);
118     }
119 
120     /**
121      * BufferedImage转换成Mat
122      *
123      * @param original 要转换的BufferedImage
124      * @param imgType  bufferedImage的类型 如 BufferedImage.TYPE_3BYTE_BGR
125      * @param matType  转换成mat的type 如 CvType.CV_8UC3
126      */
127     public static Mat bufImg2Mat(BufferedImage original, int imgType, int matType) {
128         if (original == null) {
129             throw new IllegalArgumentException("original == null");
130         }
131         // Don't convert if it already has correct type
132         if (original.getType() != imgType) {
133             // Create a buffered image
134             BufferedImage image = new BufferedImage(original.getWidth(), original.getHeight(), imgType);
135             // Draw the image onto the new buffer
136             Graphics2D g = image.createGraphics();
137             try {
138                 g.setComposite(AlphaComposite.Src);
139                 g.drawImage(original, 0, 0, null);
140                 original = image;
141             }catch (Exception e){
142                 e.printStackTrace();
143             }finally {
144                 g.dispose();
145             }
146         }
147         byte[] pixels = ((DataBufferByte) original.getRaster().getDataBuffer()).getData();
148         Mat mat = Mat.eye(original.getHeight(), original.getWidth(), matType);
149         mat.put(0, 0, pixels);
150         return mat;
151     }
152 }

 项目地址:https://github.com/git-simm/simm-framework