Example of configuring and using OpenCV in Android Studio

Source: Internet
Author: User

Simply put OpenCV into Android studio and use it to provide a library of Java.

There are some strange questions about how to use C++,jni, and it is appreciated that experienced bloggers can guide me.

Complete the example with Java first. Well, step-by-step.

First, the configuration OPENCV environment

1, download opencv4android. is the official zip package.

2, Android Studio import moudle, select OpenCV under the Sdk/java.

3, select File--->project Structure, in the app module dependencies column, click on the upper right corner of the green Plus, add opencvlibrary, click OK.

4, the import of the OPENCV project open Build.gradle changed to the app with the open Build.gradle consistent.

1) compilesdkversion

2) buildtoolsversion

3) Minsdkversion

4) targetsdkversion (match its content to the information in the Build.gradle in the app folder).

At this point, the OPENCV environment is well-configured.

Second, the use of OPENCV

In activity, first add:

static {        system.loadlibrary ("Opencv_java3");    }

To reference the OpenCV library, and then just call the library directly.

Third, the example code:

Mainactivity

public class Mainactivity extends Appcompatactivity {private Button btn;    Private ImageView img;    Private Bitmap Srcbitmap;    Private Bitmap Graybitmap;    private static Boolean flag = TRUE;        private static Boolean IsFirst = true;    static {system.loadlibrary ("opencv_java3");        } @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        img = (ImageView) Findviewbyid (r.id.img);        BTN = (Button) Findviewbyid (R.ID.BTN);    Btn.setonclicklistener (New Processclicklistener ()); The//OPENCV library loads and initializes the successful callback function private Baseloadercallback mloadercallback = new Baseloadercallback (this) {@Overri De public void onmanagerconnected (int status) {//TODO auto-generated Method stub switch (st                    ATUs) {case baseloadercallback.success:log.i ("TAG", "successfully loaded");              Break  default:super.onManagerConnected (status);                    LOG.I ("TAG", "Load Failed");            Break    }        }    };        public void Procsrc2gray () {Mat Rgbmat = new Mat ();        Mat Graymat = new Mat ();        Srcbitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.hh);        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, GrayB ITMAP);    Convert Mat to Bitmap log.i ("TAG", "Procsrc2gray sucess ..."); } public class Processclicklistener implements view.onclicklistener{@Override public void OnClick (View V                {//TODO auto-generated Method stub if (IsFirst) {Procsrc2gray ();    IsFirst = false;        } if (flag) {Img.setimagebitmap (GRAYBITMAP);                Btn.settext ("View original");            Flag = false;                }else{Img.setimagebitmap (SRCBITMAP);                Btn.settext ("grayscale");            Flag = true; }        }    }}

Layout file

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "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/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "tools:context=" com.example.binbin.opencvnight.MainActivity "> <textview android:layout_ Width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparenttop= "true" Androi d:text= "OpenCV"/> <imageview android:id= "@+id/img" android:layout_width= "Wrap_content" Andro    id:layout_height= "Wrap_content" android:layout_centerinparent= "true" android:background= "@drawable/hh"/>  <button android:id= "@+id/btn"      Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_below= "@id/img "Android:layout_centerhorizontal=" true "android:text=" grayscale "/>" </RelativeLayout>

  

Four

Detailed graphic tutorials can be seen: http://blog.csdn.net/gao_chun/article/details/49359535

There is a little bit of a small step, load the library should be opencv_java3, in my case.

Example of configuring and using OpenCV in Android Studio

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.