Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 用Android studio進行 OpenCV 開發的第一個項目,androidopencv

用Android studio進行 OpenCV 開發的第一個項目,androidopencv

編輯:關於android開發

用Android studio進行 OpenCV 開發的第一個項目,androidopencv


我的天!

折騰了好久終於搭建成功了第一個項目。

項目環境:

Windows 7  家庭普通版  64位

Android studio 1.5.1 

OpenCV-2.4.9-android-sdk

 

基於Android studio上OpenCv開發環境的配置請參考

http://www.cnblogs.com/whieenz/p/5247166.html

項目內容: 


很簡單將一張圖片灰度化。

項目源碼:

xml文件源碼:

<?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/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    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="灰度化"
        android:id="@+id/button" />
</LinearLayout>

java文件代碼:

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("查看原圖"); flag = false; }else{ imageView.setImageBitmap(srcBitmap); button.setText("灰度化"); 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) { super.onManagerConnected(status); switch (status){ case BaseLoaderCallback.SUCCESS: Log.i(TAG, "成功加載"); break; default: super.onManagerConnected(status); Log.i(TAG, "加載失敗"); 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..."); } }

具體流程:

  • 安裝opencv manager到模擬器
  • 注意事項:

     

    項目展示:

     

    1. 上一頁:
    2. 下一頁:
    熱門文章
    閱讀排行版
    Copyright © Android教程網 All Rights Reserved