【问题标题】:Mat to Byte[] conversion not working in javaMat到Byte []的转换在java中不起作用
【发布时间】:2015-02-10 08:15:20
【问题描述】:

我试图在 java 中将 Mat 转换为字节数组。在将 Mat 转换为 Byte[] 并将 Byte[] 转换为 Mat 时,我无法保留 Mat 的原始值。 Mat .get 接受 byte[] 作为无效的参数。任何人都可以帮助我吗?

用例:获取视频的每一帧并通过 kafka 将其发送给包含 byte[] 消息的消费者,然后消费者将接收字节数组并转换为 Mat 并将其保存为图像。

我在java中遇到过类似的帖子,但没有找到解决方案。 here

查看我的代码:

`       System.loadLibrary("opencv_java249");
        MatOfByte webcam_image = new MatOfByte();
        VideoCapture capture = new VideoCapture(
                "/home/jishnu/CodeT/TrainingDataSet/Video.mp4");
        System.out.println("Frame Grabber started");
        byte[] frameArray;
        int i=0;
        if (capture.isOpened()) {
            while (true) {
                capture.read(webcam_image);
                frameArray = new byte[(int) (webcam_image.total() * webcam_image
                    .channels())];
            if (!webcam_image.empty()) {                    
            //  System.out.print(".");  
                webcam_image.get(0,0,frameArray);

                producer.send(new KeyedMessage<String, byte[]>("imageTopic",frameArray));

                //Below statements are only for debugging
                System.out.println(frameArray.length);      
                MatOfByte inputframe=new MatOfByte(frameArray);
                boolean b=  Highgui.imwrite("/home/jishnu/CodeT/Today7.jpeg", inputframe);

                if(b){System.out.println("save image success");}
                else System.out.println("save image failed");

                inputframe.fromArray(frameArray);
                b=  Highgui.imwrite("/home/jishnu/CodeT/Today6.bmp",inputframe);

                if(b){System.out.println("save image success");System.exit(0);}
                else System.out.println("save image failed");

            } else {
                System.out.println(" --(!) No captured frame -- Break!");

`

【问题讨论】:

    标签: java opencv


    【解决方案1】:

    它对我有用。我使用apachi-common 来读写文件。

    public static void save(Mat mat, String name)
            {
                File file = new File(path, name);
                int length = (int) (mat.total() * mat.elemSize());
                byte buffer[] = new byte[length];
                mat.get(0, 0, buffer);
                try
                    {
                        FileUtils.writeByteArrayToFile(file, buffer);
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }
            }
    
     public static Mat load(String name)
            {
                File file = new File(path, name);
                byte[] buffer = new byte[0];
                try
                    {
                        buffer = FileUtils.readFileToByteArray(file);
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                Mat mat = new Mat(row, col, type);
                mat.put(0, 0, buffer);
                return mat;
            }
    

    【讨论】:

      【解决方案2】:

      OpenCV 版本:4.5.1

      回馈:https://www.tutorialspoint.com/how-to-convert-opencv-mat-object-to-bufferedimage-object-using-java

      public static void main(String[] args) throws IOException {
          String filePath = ".jpg";
          System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
          Mat mat=Imgcodecs.imread(filePath); 
      
          MatOfByte matOfByte = new MatOfByte();
          Imgcodecs.imencode(".jpg", mat, matOfByte);
          byte[] byteArray = matOfByte.toArray();
          
          Path path = Paths.get("D:\\yyy2.jpg");
          Files.write(path, byteArray);
          //InputStream inputStream = new ByteArrayInputStream(byteArray);
          }
      

      【讨论】:

        【解决方案3】:

        如果有人遇到同样的错误。 我使用 Mat.put(frameArray) 从字节创建 Mat 而不是 MatOfByte inputframe=new MatOfByte(frameArray); 现在一切正常,我不知道为什么 MatofByte 构造函数不起作用。

        在 JavaCV 的更高版本中已修复。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-09
          • 2016-07-30
          • 2020-10-31
          • 2012-05-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多