问题是app启动之后按钮点击一次就自动闪退了,我该如何解决
package com.example.opencv;
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 androidx.appcompat.app.AppCompatActivity;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import java.io.IOException;
public class deom_1 extends AppCompatActivity {
private ImageView imageView1,imageView2,imageView3;
private Button button1;
private Mat mat1,mat2,result;
private Bitmap bitmap;
@Override
protected void onDestroy() {
super.onDestroy();
mat1.release();
//mat2.release();
result.release();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initLoadOpenCV();
imageView1 = findViewById(R.id.imageView1);
imageView2 = findViewById(R.id.imageView2);
imageView3 = findViewById(R.id.imageView3);
button1 = findViewById(R.id.button1);
mat1 = new Mat();
// mat2 = new Mat();
result = new Mat();
try {
mat1 = Utils.loadResource(this, R.drawable.gura1);
//mat2 = Utils.loadResource(this, R.drawable.gura2);
} catch (IOException e) {
e.printStackTrace();
return;
}
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Imgproc.cvtColor(mat1,result,Imgproc.COLOR_RGB2GRAY);
Imgproc.threshold(result,mat1,125,255,Imgproc.THRESH_BINARY_INV);
//Imgproc.adaptiveThreshold(result,mat1,225,Imgproc.ADAPTIVE_THRESH_MEAN_C,Imgproc.THRESH_BINARY,13,5);
//Core.bitwise_or(mat1,mat2,result);
bitmap = Bitmap.createBitmap(result.width(),result.height(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mat1,bitmap);
imageView3.setImageBitmap(bitmap);
}
});
}
public void initLoadOpenCV() {
boolean success = OpenCVLoader.initDebug();
if (success) {
Log.d("init", "initLoadOpenCV: openCV load success");
} else {
Log.e("init", "initLoadOpenCV: openCV load failed");
}
}
}