我在unity3d上有一个AR应用程序 . 该应用程序有一个AR相机,一个2D标记,一个3D图像目标 . 该应用程序的用户senoria如下:

团结一致

1. Start program, AR cameras is a live camera. It gets frames.
2. User click on screen.
3. Save clicked screen as an png file and calculate x-y mouse position.
4. Call c++ functon by loading dll

在C代码中,1 . 加载保存的图像2.绘制一个圆心,其中心为点(x,y)3 . 将图像保存为新的命名

C#代码:

public class DefaultTrackableEventHandler : MonoBehaviour,
                                        ITrackableEventHandler
{

//Lets make our calls from the Plugin
[DllImport ("DllExample")]
private static extern bool drawRect(int x, int y); 
void Update () {

    //check if the screen is touched / clicked   
    if((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)   || (Input.GetMouseButtonDown(0)))
    {
        Application.CaptureScreenshot("Screenshot.png");
        if(!drawRect (100, 100)) {
            Debug.Log("DLL returns false");
        }
   }
}

C代码:

#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>        
#include <fstream>
#include <iostream>

 extern "C" __declspec(dllexport) bool drawRect(int x, int y);

 BOOL APIENTRY DllMain( HMODULE hModule,
                   DWORD  ul_reason_for_call,
                   LPVOID lpReserved
                 )
 {
 switch (ul_reason_for_call)
 {
  case DLL_PROCESS_ATTACH:
  case DLL_THREAD_ATTACH:
  case DLL_THREAD_DETACH:
  case DLL_PROCESS_DETACH:
    break;
  }
  return TRUE;
  }
__declspec(dllexport) bool drawRect(int x, int y)
{                                                      
cv::Mat bgr = cv::imread("Screenshot.png", 1);     

if (bgr.empty()) {
    std::ofstream myfile;
    myfile.open("log.txt");
    myfile << "Screenshot.png cannot loaded!\n";
    myfile.close();
    return false;
}

cv::circle(bgr, cv::Point(x, y), 5, cv::Scalar(255, 0, 0), 5);
cv::imwrite("Result.png", bgr);
return true;
}

我在visual stduio 2013上构建了c代码,它返回下面的消息 .

1>------ Build started: Project: DllExample, Configuration: Debug Win32 ------
 1>  dllmain.cpp
 1>  LINK : C:\Users\proje\documents\visual studio    2013\Projects\DllExample\Debug\DllExample.dll not found or not built by the last   incremental link; performing full link
 1>     Creating library C:\Users\proje\documents\visual studio 2013\Projects\DllExample\Debug\DllExample.lib and object C:\Users\proje\documents\visual studio 2013\Projects\DllExample\Debug\DllExample.exp
 1>  DllExample.vcxproj -> C:\Users\proje\documents\visual studio 2013\Projects\DllExample\Debug\DllExample.dll
 ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

当我点击屏幕时, .dll 第一次返回false值 . c代码没有理解为什么 .