jenly1314 / WeChatQRCode

⛄ 基于OpenCV开源的微信二维码引擎移植的二维码扫码识别库
https://jenly1314.github.io/WeChatQRCode/
Apache License 2.0
607 stars 120 forks source link

Mat img_mat = imread(....)输入图片流的问题 #45

Closed lymgithub closed 7 months ago

lymgithub commented 8 months ago

Mat img_mat = imread(....)不支持中文,有没有什么方法输入图片流 网上的方法大多是针对 org.opencv.core.Mat 的

有没有办法 将 InputStream 转为 org.bytedeco.opencv.opencv_core.Mat

jenly1314 commented 8 months ago
  public static Mat toMat(InputStream is) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream(is.available());
        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        is.close();

        Mat encoded = new Mat(1, os.size(), CvType.CV_8U);
        encoded.put(0, 0, os.toByteArray());
        os.close();

        Mat decoded = Imgcodecs.imdecode(encoded, -1);
        encoded.release();

        return decoded;
    }