[Help!] How to encode and decode from WebcamTexture, and write to file system
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 ???
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