[Debut] configure the JavaCV environment in AndroidStudio
Because face recognition is required to participate in a recent competition, but the competition party restricts the use of API cloud service calls provided by a third-party service provider, it is expected to use javacv for implementation, however, Baidu and google search found that all the tutorials were built in eclipse, which had a headache for a few days and were rebuilt in one breath today. This blog is mainly for reference by new friends and is also my first blog.
1. Preparations
2. Establish the environment
3. Test Procedure Preparations
-First, you need to download the package required by javacv on the google website.
-Website: https://code.google.com/archive/p/javacv/downloads
-Friends who cannot open can download http://pan.baidu.com/s/1c2BIhmo with my shared online drive
-Downloaded package:Javacv-0.7-bin.zipw.opencv-2.4.3-android-arm.zip
-Extract the files separatelyJavacv-0.7-bin.zipw.opencv-2.4.3-android-arm.zipTo any directory
-Preparations are completed at the moment.
Environment Construction
1. Use AndroidStudio to create a project, StudioTestJavaCV.
2. Open the AndroidStudio project and you cannot see the libs folder in the project directory in the Android view. Therefore, clickAndroid, Replace itProjectView
SetJavacv-0.7-bin.zipIn the decompressed folderJavacv. jar, javacpp. jarCopy to libs folder
Right-click the project and selectOpen Module Setting <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Character + character "write image description here" src = "http://www.bkjia.com/uploads/allimg/160423/0414555028-2.png" title = "\"/>
3. copy the file. so, in eclipse, you only need to create the armeabi folder under the libs folder. However, because AndroidStudio uses gradle for compilation, it is different from eclipse, so the Setup cannot be completed according to the eclipse method. Therefore, Follow me
In the same Project view, App> src> mainCreate a directory JniLibsDirectory, and then create a directory under jniLibs ArmeabiDirectory
3. Javacv-0.7-bin.zipFind Javacv-android-arm.jarDecompress the package
Copy all. so files in this folder to the armeabi folder created in the previous step.
Then Opencv-2.4.3-android-arm.zipDecompress Libs> armeabiCopy all. so files to the armeabi folder created in the previous step.
The above environment has been set up
Test procedureImage flip:
Note: This code is from the network. If the author asks to stop using it, contact me to delete it now.
Package com. act. studiotestjavacv; import android. support. v7.app. actionBarActivity; import android. OS. bundle; import static com. googlecode. javacv. cpp. opencv_core.IPL_DEPTH_8U; import static com. googlecode. javacv. cpp. opencv_core.cvFlip; import android. graphics. bitmap; import android. graphics. drawable. bitmapDrawable; import android. graphics. drawable. drawable; import android. widget. imageView; import com. googlecode. javacv. cpp. opencv_core.IplImage; public class MainActivity extends ActionBarActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ImageView imageView = (ImageView) findViewById (R. id. img1); imageView. setImageResource (R. drawable. p4); // instantiate the control ImageView img = (ImageView) findViewById (R. id. img); // create an image. p4 is a random image. Here, you can find an image instead of Drawable drawable = idToDrawable (R. drawable. p4); Bitmap bitmap = this. drawableToBitmap (drawable); // converts Bitmap to IplImage iplImage = this. bitmapToIplImage (bitmap); // process the image, such as rotating the image cvFlip (iplImage, iplImage, 0); // convert IplImage to Bitmap bitmap = this. iplImageToBitmap (iplImage); img. setImageBitmap (bitmap);}/*** convert IplImage to Bitmap * @ param iplImage * @ return */public Bitmap handle (IplImage iplImage) {Bitmap bitmap = null; bitmap = Bitmap. createBitmap (iplImage. width (), iplImage. height (), Bitmap. config. ARGB_8888); bitmap. copyPixelsFromBuffer (iplImage. getByteBuffer (); return bitmap;}/*** convert Bitmap to IplImage * @ param bitmap * @ return */public IplImage bitmapToIplImage (Bitmap bitmap) {IplImage iplImage; iplImage = IplImage. create (bitmap. getWidth (), bitmap. getHeight (), IPL_DEPTH_8U, 4); bitmap. copyPixelsToBuffer (iplImage. getByteBuffer (); return iplImage;}/*** converts the resource ID to Drawable * @ param id * @ return */public Drawable idToDrawable (int id) {return this. getResources (). getDrawable (R. drawable. p4);}/*** converts Drawable to Bitmap * @ param drawable * @ return */public Bitmap drawableToBitmap (Drawable drawable) {if (drawable = null) return null; return (BitmapDrawable) drawable ). getBitmap ();}}
You can select an image for testing. The effect is as follows:
The building process of the Android-side javacv environment has ended. This is my first blog. I wrote my blog for the first time and thought too much about it. I was afraid that I would miss the key point too roughly, and I was afraid that it would be too complicated and not a primary or secondary node, in short, it is a good start and can be used as a learning note. It also hopes to serve as a reference for friends who encounter bottlenecks in setting up the javaCV environment on AndroidStudio as before.