標籤:using Image Recognition 使用 res system mes list 網路攝影機 傳遞
(1)C介面中的介面標頭檔
#pragma once#include <iostream>#include <vector>#include <core/core.hpp>#include <highgui/highgui.hpp>#include <imgproc/imgproc.hpp>#ifdef VIDEOPOSITION_EXPORTS#define VIDEOPOSITION_API __declspec(dllexport)#else#define VIDEOPOSITION_API __declspec(dllimport)#endif // VISION_EXPORTSusing namespace std;using namespace cv;typedef void(*Callback_ColorPos)(int color, float fX,float fY);typedef void(*Callback_ImageOK)(int status);extern "C" int VIDEOPOSITION_API ColorPos(Callback_ColorPos GetColorPos,Callback_ImageOK GetImageSttus);extern "C" int VIDEOPOSITION_API OpenCamera(int index);extern "C" int VIDEOPOSITION_API CloseCamera();
我們可以看到,介面標頭檔中有三個函數,其中有一個函數的參數有兩個回調類型。
(2)java中jna回調類的實現
package com.wxyz.scene_demonstration;import com.sun.jna.Callback;/** * 圓餅顏色和位置的回調介面 * * @author dengchaoqun * */public interface ColorPosListener extends Callback {//定義一個介面繼承自jna的Callback類,對應上述標頭檔中Callback_ColorPos回調 /** * 回調方法 * * @param color * 圓餅的顏色 * @param x * 圓餅的x軸座標 * @param y * 圓餅的y軸座標 */ public void Status(int color, float x, float y);//對應C中回調介面中的三個參數,(對應上述標頭檔中Callback_ColorPos回調中的三個參數)
}
(3)java回調介面的實現,實現ColorPosListener這個介面,實現Status方法,當動態庫中有資料時, 資料就會傳遞到Status方法中的三個參數中,這樣java端就可以監聽動態庫中的資料,並處理。
java對應動態庫的介面如下
package com.wxyz.scene_demonstration;import com.sun.jna.Library;import com.sun.jna.Native;/** * 動態規整介面 * * @author dengchaoqun * */public interface SceneDemo2API extends Library { SceneDemo2API INSTANCE = (SceneDemo2API) Native.loadLibrary("VideoPosition", SceneDemo2API.class); public int OpenCamera(int item);// 開啟網路攝影機介面方法 public int ColorPos(ColorPosListener GetColorPos, ImageOKListener GetImage);// 回調擷取資料的方法 public int CloseCamera();// 關閉網路攝影機的方法}
java介面對應的實作類別如下,以及使用demo如下
package com.wxyz.scene_demonstration;@SuppressWarnings("unused")public class SceneDemo2Control { private static boolean cameraStatus = true;// 相機開啟的狀態,預設關閉 private static final float Y_point = 80.0f;// Image Recognition原點在機械臂座標系中y軸方向的座標值 private static final float Z_point = -312.5f;// Image Recognition原點在機械臂座標戲中z軸方向的座標值,也就是吸圓餅的座標值 private static final float s = 339.5f;// 圓餅資料發送點距離機械臂原點的x軸方向的距離,用於計算時間來用。 /** * 回調監聽網路攝影機回傳的圓餅資料,以及圖片是否寫好了的監聽 * * @param listener * 監聽器,使用者在使用的時候需要實現一個ColorPosListener介面,當有資料返回的時候就可以即時擷取動態庫回傳的資料 * image * 監聽器,使用者在使用的時候需要實現一個ImageOKListener介面,20ms拍一張圖片,當圖片寫好後,返回1, * 可更新介面imageView * @return 狀態代碼 該方法是一個延時的方法,在網路攝影機開啟後,會一直執行,在這個過程中只有當網路攝影機關閉後才會返回-1值 */ public static int getColorPos(ColorPosListener listener, ImageOKListener image) { if (cameraStatus == false) { System.out.println("網路攝影機沒開啟,請先成功的開啟網路攝影機!"); return -1; } else { int status = SceneDemo2API.INSTANCE.ColorPos(listener, image); return status; } } /** * 開啟網路攝影機 * * @param item * 表示是哪一個網路攝影機,從0開始 * @return 開啟網路攝影機的狀態,-1表示開啟失敗,0表示開啟成功 */ public static int openCamera(int item) { int status = SceneDemo2API.INSTANCE.OpenCamera(item); if (status == 0) { cameraStatus = true; System.out.println("網路攝影機開啟成功!"); } else { cameraStatus = true; System.out.println("網路攝影機開啟失敗!"); } return status; } /** * 關閉網路攝影機的方法 * * @return 狀態嗎,-1表示關閉失敗,0表示關閉成功 */ public static int closeCamera() { int status = SceneDemo2API.INSTANCE.CloseCamera(); if (status == 0) { cameraStatus = true; System.out.println("網路攝影機關閉成功!"); } else { cameraStatus = true; System.out.println("網路攝影機關閉失敗!"); } return status; } // demo public static void main(String[] args) { openCamera(1); getColorPos(new ColorPosListener() { @Override public void Status(int color, float x, float y) { // color的對應關係,0紅色,1綠色,2藍色,3黃色,4白色,5紫色 x = Math.round(x);// 吸球點的x座標系是固定,需要根據實際的去定位吸球的 y = Math.round(y) - Y_point; if (color == 1) {// 假定兩種顏色分別是1,2 // 調用運動方法,氣嘴吸餅,運動到指定的一邊 } else if (color == 2) { // 調用運動方法,氣嘴吸餅,運動到指定的一邊 } System.out.println(x + "::" + y); } }, new ImageOKListener() { @Override public void GetImage(int imgReady) { // TODO Auto-generated method stub if (imgReady == 1) {// 圖片寫好了 // 更新imageView的值 } else if (imgReady == 0) {// 正在寫圖片 } } }); closeCamera(); }}
java中jna使用回調實現事件監聽器——觀察者模式