Home > Software engineering >  Based on OpenCV and QT, build the Android image processing program
Based on OpenCV and QT, build the Android image processing program

Time:09-25

The original address http://www.cnblogs.com/jsxyhelu/p/8286475.html
Were reviewed,
How to collect pictures? In the Windows environment, we can use dshow, under Linux, also has the basic class libraries, such as ffmpeg again, opencv own videocapture also provides the foundation support, then under the andoird, use must be Android's own correlation function, since Android is based on Java language, if we want to invoke the Android correlation function, then you must through JNI method,
There can be divided into two kinds, one is relatively complete function directly in Java implementation, in qt, only need to call this function can be; Another kind is to use the qt's own jni mechanisms, such as this, open the camera, and collect images, we first introduce the second approach, let everybody into the fastest,

Second, through the JNI open camera
A, add a header file and namespace, public variables and macros:
#include
#include
#include
#include
#include
#include
Using the namespace CV;
Using the namespace QtAndroid;

QString strFetchImage="";
QString selectedFileName="";

# define CHECK_EXCEPTION () \
If (env - & gt; ExceptionCheck ()) \
{\
QDebug () & lt; & lt; "The exception occured"; \
Env - & gt; ExceptionClear (); \
}

Which is important to note that CHECK_EXCEPTION is used to check whether there is abnormal, the Android system it is very important and necessary when using JNI,

B, add the callback class, mainly is in after a series of abnormal judgment, get the imagepath, this kind of integration from ResultReceiver:
The class ResultReceiver: public QAndroidActivityResultReceiver
{
Public: ResultReceiver (QString imagePath, QLabel * view) : m_imagePath (imagePath), m_imageView (view) {}
Void handleActivityResult (int receiverRequestCode, int the resultCode, const QAndroidJniObject & amp; Data) {
QDebug () & lt; <"HandleActivityResult, requestCode -" & lt; If (the resultCode==1 & amp; & ReceiverRequestCode==1) {
QDebug () & lt; <"The captured image to -" & lt; QDebug () & lt; <"Captured image exist -" & lt; M_imageView - & gt; SetPixmap (QPixmap (m_imagePath)); }
}
QString m_imagePath;
QLabel * m_imageView;
};

C, add controls to trigger events, in general we choose pressed event
D, photo code
//open the camera, acquisition images
VoidMainWindow: : on_btn_capture_pressed ()
{
The UI - & gt; LbMain - & gt; SetScaledContents (true);//show the images automatically zoom
B_canSave=false;//picture without collection is complete, it is not can save
//reference JNI
QAndroidJniEnvironmentenv;
//create used to open the camera content
QAndroidJniObjectaction=QAndroidJniObject: : fromString (" android. Media. Action. IMAGE_CAPTURE "); QAndroidJniObject (intent (" android/content/intent, "" (Ljava/lang/String;) V ", the action. Object ());
//set the img path
QStringdate=QDateTime: : currentDateTime (), toString (" yyyyMMdd_hhmmss ");
QAndroidJniObjectfileName=QAndroidJniObject: : fromString (date + ". JPG ");
QAndroidJniObjectsavedDir=QAndroidJniObject: : callStaticObjectMethod (" android/OS/Environment ", "external.getexternalstoragedirectory", "() Ljava/IO/File;" );
//using exception handling CHECK_EXCEPTION
CHECK_EXCEPTION ()
QDebug () & lt; <" SavedDir - "& lt; QAndroidJniObjectsavedImageFile (" Java/IO/File ", "(Ljava/IO/File; Ljava/lang/String;) V ", savedDir. Object (), fileName. Object ());
CHECK_EXCEPTION ()
QDebug () & lt; <" SavedImageFile - "& lt; QAndroidJniObjectsavedImageUri=QAndroidJniObject: : callStaticObjectMethod (" android/net/Uri ", "fromFile", "(Ljava/IO/File) Landroid/net/Uri;" ,
SavedImageFile. Object ());
CHECK_EXCEPTION ()

//the output path passed
QAndroidJniObjectmediaStoreExtraOutput=QAndroidJniObject: : getStaticObjectField (" android/provider/MediaStore ", "EXTRA_OUTPUT", "Ljava/lang/String;" );
CHECK_EXCEPTION ()
QDebug () & lt; <" MediaStore. EXTRA_OUTPUT - "& lt; Intent. CallObjectMethod (
"PutExtra", "(Ljava/lang/String; Landroid/OS/Parcelable;) Landroid/content/Intent;" , mediaStoreExtraOutput. Object (),
SavedImageUri. Object ());

//for collecting pictures the absolute path and display
ResultReceiver * ResultReceiver=newResultReceiver (savedImageFile. ToString (), UI - & gt; LbMain);
StartActivity (intent, 1, resultReceiver);
//get back to the absolute address of (note that this sentence must be written in CHECK_EXCEPTION)
StrFetchImage=savedImageFile. ToString ();
}
Finally collected images address stored in strFetchImage

E, the processing code to write, because I here mainly for the image processing operations, so must be conducted combining with the relevant function OpenCV
//image processing operations
VoidMainWindow: : on_btn_process_pressed ()
{
B_canSave=false;
If (strFetchImage!="")
{
The UI - & gt; LbMain - & gt; SetScaledContents (false);
Matsrc=https://bbs.csdn.net/topics/imread (strFetchImage toStdString ());
Matsrc2;
Matrotated;
////////////////////////////the main algorithm/////////////////////////////
CV: : resize (SRC, src2, CV: : Size (720100));//standard size
Matsrc_gray;
Matsrc_all=src2. Clone ();

Matthreshold_output;
Vector Contours and contours2;
Vector//pretreatment
CvtColor (src2, src_gray, CV_BGR2GRAY);
The blur (src_gray src_gray, Size (3, 3));//fuzzy, remove burrs
Threshold (src_gray threshold_output, 100255, THRESH_OTSU);
//add tip
The UI - & gt; Lb_info - & gt; SetText (" began to look for contour!" );
//looking for contour
//the first parameter is the input image binarization of
2//the second parameter is the memory storage, FindContours find outline on the memory,
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related