完成 Android Studio + OpenCV 的安裝和設置,便可以使用編程測試 OpenCV 功能。
Android Studio + OpenCV 測試程式 |
- 操作系統:Windows 7 64-bit 版本
- 開發環境:Android Studio 4.0.1 版本
- Gradle 版本:6.1.1
- 手機測試版本:API 19
- 原程式:C:\Development\Development_Android\Android_Project\OpenCV
- 程式:C:\Development\Development_Android\Android_Project\OpenCV
- OpenCV 版本:opencv 3.4.3版本 Android
package bugworkshop.blogspot.opencv;
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;
import android.graphics.Bitmap; import android.view.View; import android.widget.ImageView; import android.widget.Toast;
import org.opencv.android.OpenCVLoader; import org.opencv.android.Utils; import org.opencv.core.Mat; import org.opencv.imgproc.Imgproc;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
OpenCVLoader.initDebug(); }
public void displayToast(View v){
Toast.makeText(MainActivity.this,"OpenCV Change Color", Toast.LENGTH_SHORT).show();
Mat img = null;
try { img = Utils.loadResource(getApplicationContext(), R.drawable.dog); } catch (IOException e) { e.printStackTrace(); }
Imgproc.cvtColor(img, img, Imgproc.COLOR_RGB2BGRA);
Mat img_result = img.clone(); Imgproc.Canny(img, img_result, 80, 90); Bitmap img_bitmap = Bitmap.createBitmap(img_result.cols(), img_result.rows(),Bitmap.Config.ARGB_8888); Utils.matToBitmap(img_result, img_bitmap); ImageView imageView = findViewById(R.id.img); imageView.setImageBitmap(img_bitmap); } } |
app / res / layout / active_main.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:onClick="displayToast" tools:context=".MainActivity">
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
<Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="48dp" android:layout_marginTop="10dp" android:layout_marginBottom="123dp" android:onClick="displayToast" android:text="Display Toast Message" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" />
<ImageView android:id="@+id/img" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="78dp" android:adjustViewBounds="true" android:src="@drawable/dog" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button" app:layout_constraintVertical_bias="0.033" />
</androidx.constraintlayout.widget.ConstraintLayout> |
沒有留言:
張貼留言