接下來要介紹的就是使用 OpenCV 配置於 Arduino Studio
Farmer發現因為有指令不能用,可以debug並開發,但是就是沒有作用,
後來終於上網查到一樣的原因,對方版本是3.1,結果一樣的問題,
Farmer嘗試改3.0,就正常了Orz ..
Android Studio 2.1.1
配置語言 Java
- OpenCV downloads http://opencv.org/downloads.html
下載OpenCV 3.0.0 Android SDK - 匯入 OpenCV
Android Studio -> File -> New -> Improt Module -> 選擇 OpenCV 路徑(sdk/java)
3. 更改build.gradle,Module : App 和 OpenCV 的以下四項要相同
a) compileSdkVersion
b) buildToolsVersion
c) minSdkVersion
d) targetSdkVersion
上 build.gradle (Module : App),下 build.gradle (Module : OpenCV)
4. 加入 openCVLibrary File -> Project Structure -> app -> Dependencies -> + -> 3.Module dependency
-> openCVLibary320(版本不一樣後面號碼就不同)
照理來說,這邊就是OK啦,應該不會有出現問題了,缺失的東西記得要補安裝
來試試看 Android Studio 上建立程式 Let GO~
Layout
<ImageView
android:layout_width="match_parent"
android:layout_height="450dp"
android:id="@+id/image"
android:src="@drawable/圖片名稱,第一個字不可大寫(放置專案的drawable內)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="灰度化"
android:id="@+id/button"/>
Main
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.圖片名稱);
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 程式之前,記得先去 play 商店,
抓opencv manager
因為這種設定方式是需要 opencv manager 的,
聽說有可以不用使用,不過先讓小的去查查資料,在上傳給各位看官~~~
以上內容皆為轉載,轉載位置於
http://www.zendei.com/article/4945.html
沒有留言:
張貼留言