我有一个小小的疑问,我希望你能解决它。基于本地OpenCV的统一Android应用程序
我想创建一个统一的Android应用程序。该应用程序包括激活设备的摄像头并在屏幕上查看它。为此,我想基于基于OpenCV的本机C++代码。
我有生成的代码,但是当我运行应用程序时,我看到了场景,但没有看到图像,我感觉这是因为OpenCV的VideoCapture函数我没有很好地用于android。你能帮助我,我附上代码:
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;
}}
'imgData = new byte [width * height * 4];'你为什么乘以4?您的相机是否提供4声道图像? – zindarod
因为是RGB图像,我试过把3个通道蝙蝠失败。所以我把4个频道 –
'RGB'意味着3个频道。如果你的代码失败了,那是因为其他原因。正如@uelordi建议的,尝试使用Java本地代码捕获图像,使用OpenCV仅处理图像。 – zindarod