OpenCV android:应用opencv过滤器时应用程序崩溃

问题描述

我正在尝试简单地加载图像,然后尝试使用 OpenCV 对其应用过滤器,但我的应用程序不断崩溃。我的图像在 Drawable 中保存为 test.jpg。

要应用过滤器,我首先使用 ImageView 将图像加载到应用程序。这部分工作正常。然后,在单击按钮时,应将过滤器应用于图像。我一点击按钮,应用就会崩溃。

mainactivity.java 的代码如下:

package com.xyz.hello;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ImageView;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
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){
        Mat img = null;

        try {
            img = Utils.loadResource(getApplicationContext(),R.drawable.test);
        } 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);
    }

} 

另外,我的 .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"
tools:context=".MainActivity"
android:orientation="vertical">


<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/display_message"
    android:onClick="displayToast"
    tools:ignore="HardcodedText" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/test"
    android:id="@+id/img"
    android:adjustViewBounds="true">

</ImageView>

Logcat 文件重复此输出

2021-03-11 22:01:51.979 6551-6623/com.google.android.googlequicksearchBox I/hotwordLSAdapter: getAudioSourceopeningStatus completed: 102
2021-03-11 22:01:51.979 6551-6623/com.google.android.googlequicksearchBox W/ErrorReporter: reportError [type: 211,code: 393244,bug: 0]: errorCode: 393244,engine: 0
2021-03-11 22:01:51.980 6551-6623/com.google.android.googlequicksearchBox I/MicroDetector: Keeping mic open: false
2021-03-11 22:01:51.980 6551-6623/com.google.android.googlequicksearchBox I/MicroDetector: #shutdownAudioWithAudioLibrary
2021-03-11 22:01:51.980 6551-13750/com.google.android.googlequicksearchBox E/DeviceStateChecker: DeviceStateChecker cancelled
2021-03-11 22:01:51.981 6551-6623/com.google.android.googlequicksearchBox I/MicroDetectionWorker: #onError(false)
2021-03-11 22:01:51.982 6551-6623/com.google.android.googlequicksearchBox I/hotwordLSAdapter: stopListeningStatus result: 109

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)