- Home /
Question by
alpertungakin · Dec 25, 2020 at 01:40 PM ·
c#androidcamerapermissions
Could not open the camera with EmguCV 4.x on Android.
Hello everyone,
I am trying to use the phone camera via EmguCV. Everything works fine in the editor but doesn't work after building. Here is my script:
using UnityEngine;
using UnityEngine.UI;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.Features2D;
using Emgu.CV.UI;
using Emgu.CV.Util;
using System;
using System.IO;
using System.Collections;
using UnityEngine.Android;
public class cam1 : MonoBehaviour {
private int frameWidth;
private int frameHeight;
private VideoCapture cvCapture;
private Image<Bgr, byte> currentFrame;
public Texture2D tex;
public RawImage mRawImage;
GameObject dialog = null;
void Start () {
#if PLATFORM_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Camera)){
Permission.RequestUserPermission(Permission.Camera);
}
#endif
cvCapture = new VideoCapture();
cvCapture.FlipHorizontal = true;
frameWidth = (int) cvCapture.GetCaptureProperty (Emgu.CV.CvEnum.CapProp.FrameWidth);
frameHeight = (int) cvCapture.GetCaptureProperty (Emgu.CV.CvEnum.CapProp.FrameHeight);
}
void Update () {
var frame = cvCapture.QueryFrame().ToImage<Bgr, byte>();
MemoryStream _memory = new MemoryStream ();
frame.Bitmap.Save(_memory, frame.Bitmap.RawFormat);
tex = new Texture2D (frameWidth, frameHeight, TextureFormat.RGB24, false);
gameObject.GetComponent<RawImage> ().rectTransform.sizeDelta = new Vector2 (frameWidth, frameHeight);
tex.LoadImage (_memory.ToArray ());
tex.Apply ();
gameObject.GetComponent<RawImage>().texture = tex;
}
}
I'm using Unity 2018.4.30f1 and EmguCV 4.x. Thanks for your help.
Comment