amin這個例子,使用了比較複雜高階的qml技巧,可是也有侷限性。下面分3個部分,分別是界面部分,算法部分和擴展部分,簡單地對這個問題進行理解。c++
由衷感謝:http://amin-ahmadi.com/quick-camera-cv/ 給本程序不少借鑑
1、qml界面部分:
一、專門生成了用於提示的dialog
/*
只有一個OK的彈出界面,目前這個彈出界面只給捕獲的時候使用
*/
Dialog
{
property alias text
: msgDlgLabel.text
id
: messageDialog
modal
:
true
x
: (parent.width
- width)
/
2
y
: (parent.height
- height)
/
2
standardButtons
: Dialog.Ok
//上面只有一個label
Label
{
id
: msgDlgLabel
font.bold
:
true
wrapMode
: Text.Wrap
width
: parent.width
//點擊事件
onLinkActivated
:
{
if(link
==
"圖像處理")
{
tabBar.currentIndex
=
1
}
messageDialog.close()
}
}
}
二、總體界面採用swipe和page以及footer的形式,達到了簡潔高效
/*
主要的swipview界面
*/
SwipeView
{
id
: swipeView
currentIndex
:
0
anchors.rightMargin
:
0
anchors.bottomMargin
:
0
anchors.leftMargin
:
0
anchors.topMargin
:
0
anchors.fill
: parent
//TabBar和swipview要對應起來
onCurrentIndexChanged
:
{
tabBar.setCurrentIndex(swipeView.currentIndex)
}
//視頻預覽
Page
{……
以及
/*
主要的footer操做
*/
footer
: TabBar {
id
: tabBar
currentIndex
:
0
padding
:
10
三、使用了HTML增長文本顯示效果,而且是能夠切換的
Text
{
id
: helpText
wrapMode
: Text.Wrap
anchors.left
: parent.left
anchors.right
: parent.right
text
:
"由衷感謝:<a href=\"http://amin-ahmadi.com/quick-camera-cv/\">http://amin-ahmadi.com/quick-camera-cv</a>"
+
"給本程序不少借鑑"
+
"<br>"
+
"<br>"
+
"做者博客:"
+
"<br>"
+
"<a href=\"jsxyhelu.cnblogs.com\">jsxyhelu.cnblogs.com</a>"
+
"<br>"
+
"<a href=\"jsxyhelu.cnblogs.com\"><img src=\"http://images2015.cnblogs.com/blog/508489/201607/508489-20160731065441684-483128601.png\" alt=\"歡迎訪問!\"></a>"
+
"<br>"
+
"<b>版權</b>"
+
"本程序使用 <a href=\"http://qt.io/\">Qt Framework</a> 做爲GUI"
+
"<br>"
+
"同時使用 <a href=\"http://opencv.org/\">OpenCV</a> 作圖像處理算法."
+
"<br>"
+
"程序採用ICO來自<a href=\"http://flaticon.com/\">Flat Icon</a>."
onLinkActivated
:
{
Qt.openUrlExternally(link);
}
}
應該這樣講,有這段代碼做爲例子,那麼這種樣式的程序在界面上基本不成問題。
2、算法實現部分:
qml是弱語法,比較相似lambda,因此這種語言的使用對於習慣c語言的我來講有難度,想要精通須要時間;另外一個方面,由於須要和OpenCV進行交互,因此更復雜一點。本例中綜合使用了qml使用c++封裝出來的對象,以及「信號、槽」機制等;在
攝像頭獲取和圖片採集實現中,
硬件層綜合使用了qml和qcamera,捕獲使用了
QCameraImageCapture
,具體這樣用
在qml中,使用
//攝像頭選擇對話框
ComboBox
{
id
: cameraCombo
Layout.fillWidth
:
true
Layout.fillHeight
:
true
model
: QtMultimedia.availableCameras
textRole
:
"displayName"
delegate
: ItemDelegate
{
text
: modelData.displayName
}
onCurrentIndexChanged
:
{
camera.stop()
camera.deviceId
= model[currentIndex].deviceId
camera.start()
}
}
這樣能夠得到全部可用攝像頭的句柄,而後直接傳遞到c++中
//調用qcamera進行圖像採集
void QCvImageProcessor
:
:setCamera(QVariant v)
{
QObject
*o
= qvariant_cast
<QObject
*
>(v);
camera
= qvariant_cast
<QCamera
*
>(o
-
>property(
"mediaObject"));
camera
-
>setCaptureMode(QCamera
:
:CaptureStillImage);
imageCapture
=
new QCameraImageCapture(camera);
camera
-
>focus()
-
>setFocusMode(QCameraFocus
:
:ContinuousFocus);
camera
-
>focus()
-
>setFocusPointMode(QCameraFocus
:
:FocusPointAuto);
//直接在這裏設置動做級聯
connect(imageCapture,
&QCameraImageCapture
:
:imageSaved, [
this](
int id,
const QString
&fileName)
{
Q_UNUSED(id);
processSavedImage(fileName);
});
}
void QCvImageProcessor
:
:capture()
{
if(imageCapture
-
>isReadyForCapture())
{
//注意這裏得到一個可用的圖片地址的方法
imageCapture
-
>capture(QStandardPaths
:
:writableLocation(QStandardPaths
:
:PicturesLocation));
}
else
{
emit errorOccured(
"Camera is not ready to capture.");
}
}
仍是使用
QCameraImageCapture,QCamera來完成捕獲。
因爲在andoird中,videocapture不能給使用,那麼qcamera做爲qt專屬,來實現攝像頭採集功能是很是合適的,這裏給出了具體系統方法。
3、進一步擴展部分:
QCameraImageCapture只能捕獲靜態圖片,可是做爲一個完整的圖像處理程序,必定要可以處理並顯示實時的視頻數據,如何解決?繼續探索!
感謝閱讀至此,但願有所幫助。
附件列表