OpenCVForUnity icon indicating copy to clipboard operation
OpenCVForUnity copied to clipboard

[Help!] How to encode and decode from WebcamTexture, and write to file system

Open cedric911217 opened this issue 5 years ago • 1 comments

Hi, I need to encode and decode an image from "webcamtexture", and write to file system, I program a test code, but always write fail, here is my test code

WebCamTexture webCamTexture = m_webcamMgr.GetWebcamTexture(); int width = webCamTexture.width; int height = webCamTexture.height; Mat imgMat = new Mat(height, width, CvType.CV_8UC3); byte[] imgByteBuf = new byte[imgMat.total() * imgMat.channels()]; MatOfByte imgMatByte = new MatOfByte(imgByteBuf); int[] para = { Imgcodecs.IMWRITE_JPEG_QUALITY}; MatOfInt paraMat = new MatOfInt(para);

if (Imgcodecs.imencode(".jpg", imgMat, imgMatByte, paraMat)) Debug.Log("compress success"); else Debug.Log("compress fail"); imgByteBuf = imgMatByte.toArray(); Mat outbuf = new Mat(1, imgByteBuf.Length, CvType.CV_8UC1); MatUtils.copyToMat<byte>(imgByteBuf, outbuf); Mat result = Imgcodecs.imdecode(outbuf, Imgcodecs.IMREAD_COLOR);

if (!Imgcodecs.imwrite("test.jpg", result)) Debug.Log("write success"); else Debug.Log("write fail");

imwrite alway return false, Could you help me ???

cedric911217 avatar Jan 11 '21 02:01 cedric911217

The save path may not be appropriate. Could you try the following code?

Imgcodecs.imwrite( Application.persistentDataPath + "/test.jpg", result)

https://github.com/EnoxSoftware/OpenCVForUnity/blob/ef064e959f02f527119c9f55a0f275b071d9dc61/Assets/OpenCVForUnity/Examples/MainModules/imgcodecs/ImwriteScreenCaptureExample/ImwriteScreenCaptureExample.cs#L38

EnoxSoftware avatar Jan 11 '21 10:01 EnoxSoftware