C# – unified Android Application Based on native opencv
I have a little question. I hope you can solve it
I want to create an Android Application in a unified way. The application includes activating the camera of the device and viewing it on the screen. For this purpose, I want to use the native C code based on OpenCV
I have the generated code, but when I run the application, I see the scene instead of the image. I think this is because I don't use the videocapture function of OpenCV well. Can you help me? I attach the code:
C :
__declspec(dllexport) void iniciar(int& widt, int& heigh) {
camera.open(0);
if (!camera.isOpened())
{
return;
}
widt = (int)camera.get(CV_CAP_PROP_FRAME_WIDTH);
heigh = (int)camera.get(CV_CAP_PROP_FRAME_HEIGHT);
trueRect.x = 5;
trueRect.y = 5;
trueRect.width = 100;
trueRect.height = 100;
midX = 1;
midY = 1;
wi = 0;
he = 0;
}
__declspec(dllexport)
void video(unsigned char* arr) {
Mat frame;
Mat resi;
Mat dst;//dst image
camera >> frame;
if (frame.empty()) {
return;
}
flip(frame, dst,1);
//resize(dst, resi, Size(width, height));
cv::cvtColor(dst, dst, COLOR_BGR2RGB);
copy(dst.datastart, dst.dataend, arr);
}
C#:
public class camara : MonoBehavIoUr {
[DllImport("NativoPrincipio")]
public static extern void video(byte[] img);
[DllImport("NativoPrincipio")]
public static extern void iniciar(ref int widt, ref int heigh);
WebCamTexture back;
Texture2D textura;
byte[] imgData;
int width = 0;
int height = 0;
// Use this for initialization
void Start () {
back = new WebCamTexture();
//GetComponent<Renderer>().material.mainTexture = back;
//back.Play();
iniciar(ref width, ref height);
}
// Update is called once per frame
void Update ()
{
imgData = new byte[width * height * 4];
video(imgData);
textura = new Texture2D(width, height, TextureFormat.RGB24, false);
textura.LoadRawTextureData(imgData);
textura.Apply();
GetComponent<Renderer>().material.mainTexture = textura;
imgData = null;
textura = null;
}}
resolvent:
It might be better if you already hosted the entire code on GitHub, or if you could create a point
But I think your camera frame is empty
I'll try to enumerate why this happened
>First, the opencv video capture class camera. Open (0) does not work without ffmpeg (it is not easy to cross compile for Android native)
>If you want to use the native camera in C in Android, you need to use the latest NDK of API 24
>I see you are using unity's webcamtexture class, but it is almost possible that the captured texture is empty, so you will see a black screen in the image
Are you trying to search the unity plug-in for opencv? The only thing I see is this (not free): https://www.assetstore.unity3d.com/en/# !/ content/21088
If this doesn't work, is there any way you can paste the Java code of the unified project? Because the camera capture in Java is very easy. I hope these clues can help you
Ondo izan eta animo.
Drooping hair