opencv for android官方教程Word下载.docx
《opencv for android官方教程Word下载.docx》由会员分享,可在线阅读,更多相关《opencv for android官方教程Word下载.docx(15页珍藏版)》请在冰豆网上搜索。
![opencv for android官方教程Word下载.docx](https://file1.bdocx.com/fileroot1/2022-11/15/2d5203c8-e6be-4e02-af08-581ccc0a282c/2d5203c8-e6be-4e02-af08-581ccc0a282c1.gif)
ThistutorialalsoassumesyouhaveOpenCV4AndroidSDKalreadyinstalledonyourdevelopmentmachineandOpenCVManageronyourtestingdevicecorrespondingly.Ifyouneedhelpwithanyofthese,youmayconsultourOpenCV4AndroidSDKtutorial.
Ifyouencounteranyerrorafterthoroughlyfollowingthesesteps,feelfreetocontactusviaOpenCV4AndroiddiscussiongrouporOpenCVQ&
Aforum.We’lldoourbesttohelpyouout.
UsingOpenCVLibraryWithinYourAndroidProject
InthissectionwewillexplainhowtomakesomeexistingprojecttouseOpenCV.Startingwith2.4.2releaseforAndroid,OpenCVManagerisusedtoprovideappswiththebestavailableversionofOpenCV.Youcangetmoreinformationhere:
AndroidOpenCVManagerandintheseslides.
Java
ApplicationDevelopmentwithAsyncInitialization
Usingasyncinitializationisarecommendedwayforapplicationdevelopment.ItusestheOpenCVManagertoaccessOpenCVlibrariesexternallyinstalledinthetargetsystem.
1.AddOpenCVlibraryprojecttoyourworkspace.UsemenuFile->
Import->
Existingprojectinyourworkspace.
PressBrowsebuttonandlocateOpenCV4AndroidSDK(OpenCV-2.4.9-android-sdk/sdk).
2.InapplicationprojectaddareferencetotheOpenCVJavaSDKinProject->
Properties->
Android->
Library->
AddselectOpenCVLibrary-2.4.9.
InmostcasesOpenCVManagermaybeinstalledautomaticallyfromGooglePlay.Forthecase,whenGooglePlayisnotavailable,i.e.emulator,developerboard,etc,youcaninstallitmanuallyusingadbtool.SeeHowtoselecttheproperversionofOpenCVManagerfordetails.
Thereisaverybasecodesnippetimplementingtheasyncinitialization.Itshowsbasicprinciples.Seethe“15-puzzle”OpenCVsamplefordetails.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
publicclassSample1JavaextendsActivityimplementsCvCameraViewListener{
privateBaseLoaderCallbackmLoaderCallback=newBaseLoaderCallback(this){
@Override
publicvoidonManagerConnected(intstatus){
switch(status){
caseLoaderCallbackInterface.SUCCESS:
{
Log.i(TAG,"
OpenCVloadedsuccessfully"
);
mOpenCvCameraView.enableView();
}break;
default:
super.onManagerConnected(status);
}
};
publicvoidonResume()
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6,this,mLoaderCallback);
...
}
ItthiscaseapplicationworkswithOpenCVManagerinasynchronousfashion.OnManagerConnectedcallbackwillbecalledinUIthread,wheninitializationfinishes.Pleasenote,thatitisnotallowedtouseOpenCVcallsorloadOpenCV-dependentnativelibsbeforeinvokingthiscallback.LoadyourownnativelibrariesthatdependonOpenCVafterthesuccessfulOpenCVinitialization.DefaultBaseLoaderCallbackimplementationtreatapplicationcontextasActivityandcallsActivity.finish()methodtoexitincaseofinitializationfailure.Tooverridethisbehavioryouneedtooverridefinish()methodofBaseLoaderCallbackclassandimplementyourownfinalizationmethod.
ApplicationDevelopmentwithStaticInitialization
AccordingtothisapproachallOpenCVbinariesareincludedintoyourapplicationpackage.Itisdesignedmostlyfordevelopmentpurposes.Thisapproachisdeprecatedfortheproductioncode,releasepackageisrecommendedtocommunicatewithOpenCVManagerviatheasyncinitializationdescribedabove.
1.AddtheOpenCVlibraryprojecttoyourworkspacethesamewayasfortheasyncinitializationabove.UsemenuFile->
Existingprojectinyourworkspace,pressBrowsebuttonandselectOpenCVSDKpath(OpenCV-2.4.9-android-sdk/sdk).
2.IntheapplicationprojectaddareferencetotheOpenCV4AndroidSDKinProject->
AddselectOpenCVLibrary-2.4.9;
3.Ifyourapplicationprojectdoesn’thaveaJNIpart,justcopythecorrespondingOpenCVnativelibsfrom<
OpenCV-2.4.9-android-sdk>
/sdk/native/libs/<
target_arch>
toyourprojectdirectorytofolderlibs/<
.
IncaseoftheapplicationprojectwithaJNIpart,insteadofmanuallibrariescopyingyouneedtomodifyyourAndroid.mkfile:
addthefollowingtwocodelinesafterthe"
include$(CLEAR_VARS)"
andbefore"
includepath_to_OpenCV-2.4.9-android-sdk/sdk/native/jni/OpenCV.mk"
1
2
OPENCV_CAMERA_MODULES:
=on
OP