Android studio is the first project for OpenCV development.
My day!
After a long time, I finally successfully built the first project.
Project Environment:
Windows 7 Family Basic edition 64-bit
Android studio 1.5.1
OpenCV-2.4.9-android-sdk
For more information about how to configure the OpenCv Development Environment on Android studio, see
Http://www.cnblogs.com/whieenz/p/5247166.html
Project content:
It is easy to grayscale an image.
Project source code:
Xml file source code:
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: app = "http://schemas.android.com/apk/res-auto" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/Shanghai" android: paddingRight = "@ dimen/Shanghai" android: paddingTop = "@ dimen/Shanghai" app: layout_behavior = "@ string/appbar_scrolling_view_behavior" tools: context = "com. whieenz. myapplication. mainActivity "tools: showIn =" @ layout/activity_main "android: orientation =" vertical "> <ImageView android: layout_width =" match_parent "android: layout_height =" 450dp "android: id = "@ + id/image" android: src = "@ drawable/genie"/> <Button android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "grayscale" android: id = "@ + id/button"/> </LinearLayout>
Java file code:
Package com. whieenz. myapplication; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. OS. bundle; import android. util. log; import android. view. view; import android. widget. button; import android. widget. imageView; import org. opencv. android. baseLoaderCallback; import org. opencv. android. openCVLoader; import org. opencv. android. utils; import org. opencv. core. mat; import org. opencv. imgproc. imgproc;
Public class MainActivity extends Activity {private ImageView imageView; private Button button; private Bitmap srcBitmap; private Bitmap grayBitmap; private static boolean flag = true; private static boolean isFirst = true; private static final String TAG = "gao_chun"; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. content_main); imageView = (ImageView) findViewById (R. id. image); button = (Button) findViewById (R. id. button); button. setOnClickListener (new MyOnClickListener ();} public class MyOnClickListener implements View. onClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub if (isFirst) {procSrc2Gray (); isFirst = false;} if (flag) {imageView. setImageBitmap (grayBitmap); button. setText ("View Source image"); flag = false;} else {imageView. setImageBitmap (srcBitmap); button. setText ("grayscale"); flag = true ;}}@ Override protected void onResume () {super. onResume (); OpenCVLoader. initAsync (OpenCVLoader. OPENCV_VERSION_2_4_9, getApplicationContext (), mLoaderCallback);} private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback (this) {@ Override public void onManagerConnected (int status. onManagerConnected (status); switch (status) {case BaseLoaderCallback. SUCCESS: Log. I (TAG, "loaded successfully"); break; default: super. onManagerConnected (status); Log. I (TAG, "loading failed"); break ;}}; public void procSrc2Gray () {Mat rgbMat = new Mat (); Mat grayMat = new Mat (); srcBitmap = BitmapFactory. decodeResource (getResources (), R. drawable. genie); grayBitmap = Bitmap. createBitmap (srcBitmap. getWidth (), srcBitmap. getHeight (), Bitmap. config. RGB_565); Utils. bitmapToMat (srcBitmap, rgbMat); // convert original bitmap to Mat, r g B. imgproc. cvtColor (rgbMat, grayMat, Imgproc. COLOR_RGB2GRAY); // rgbMat to gray grayMat Utils. matToBitmap (grayMat, grayBitmap); // convert mat to bitmap Log. I (TAG, "procSrc2Gray sucess... ");}}
Specific process:
Install opencv manager to the simulator
Note:
Project display: