gpt4 book ai didi

android-studio - 在android studio中使用opencv进行人脸检测。 E/AndroidRuntime:致命异常:main

转载 作者:行者123 更新时间:2023-12-02 17:16:47 28 4
gpt4 key购买 nike

我正在使用opencv做人脸检测应用程序。该应用程序已安装在手机中,但由于致命错误而突然关闭。这是我的MainActivity.java

package com.example.oc2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraActivity;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;

import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.JavaCameraView;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2{
JavaCameraView javaCameraView;
File cascfile;
CascadeClassifier facedetector;
private Mat mRgba,mGrey;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if(!OpenCVLoader.initDebug())
{
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_4_0,this, baseCallback);
}
else
{
try {
baseCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
} catch (IOException e) {
e.printStackTrace();
}
}
javaCameraView.setCvCameraViewListener(this);
}

@Override
public void onCameraViewStarted(int width, int height) {
mRgba=new Mat();
mGrey=new Mat();

}

@Override
public void onCameraViewStopped() {
mGrey.release();
mRgba.release();

}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mRgba=inputFrame.rgba();
mGrey=inputFrame.gray();
MatOfRect facedetection= new MatOfRect();
facedetector.detectMultiScale(mRgba,facedetection);
for(Rect rect: facedetection.toArray())
{
Imgproc.rectangle(mRgba,new Point(rect.x,rect.y),new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(255,0,0));
}
return mRgba;
}

private final BaseLoaderCallback baseCallback=new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) throws IOException {
switch (status)
{
case LoaderCallbackInterface.SUCCESS:
{
InputStream is = getResources().openRawResource(R.raw.haarcascade_frontalface_alt2);
File cascadedir=getDir( "cascade" , Context.MODE_PRIVATE);
cascfile= new File(cascadedir, "haarcascade_frontalface_alt2.xml" );
FileOutputStream fos= new FileOutputStream(cascfile);

byte[] buffer=new byte[4096];
int bytesread;
while((bytesread = is.read(buffer))!=-1)
{
fos.write(buffer,0,bytesread);
}
is.close();
fos.close();
facedetector=new CascadeClassifier(cascfile.getAbsolutePath());
if(facedetector.empty())
{
facedetector=null;
}
else
{
cascadedir.delete();
}
javaCameraView.enableView();

}
break;

default:
{
super.onManagerConnected(status);
}
break;

}

}
};
}
这是我的错误,谁能帮忙?

D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION:mainProcess: com.example.oc2, PID: 19468java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.oc2/com.example.oc2.MainActivity}:java.lang.NullPointerException: Attempt to invoke virtual method 'voidorg.opencv.android.JavaCameraView.enableView()' on a null objectreferenceat android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)at android.os.Handler.dispatchMessage(Handler.java:106)at android.os.Looper.loop(Looper.java:201)at android.app.ActivityThread.main(ActivityThread.java:6864)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void org.opencv.android.JavaCameraView.enableView()'on a null object referenceat com.example.oc2.MainActivity$1.onManagerConnected(MainActivity.java:116)at com.example.oc2.MainActivity.onCreate(MainActivity.java:52)at android.app.Activity.performCreate(Activity.java:7232)at android.app.Activity.performCreate(Activity.java:7221)at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2964)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:201) at android.app.ActivityThread.main(ActivityThread.java:6864) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) I/Process: Sending signal. PID: 19468 SIG: 9


如果有人可以采取措施解决错误,那将非常有帮助。

最佳答案

再见,
我对您的代码有两种感觉:
1-在onCreate Activity 中,您缺少以下行:

javaCameraView = (CameraBridgeViewBase) findViewById(R.id.java_camera_view);
2-您没有检查是否授予了摄像头权限,这以后可能会导致另一个错误[ "it seems that your device does not support camera or it is locked - application will be closed"]
因此,请对此代码段进行测试,但请考虑一下,为了使其正常运行,您需要在启动应用程序[ Settings->Apps->YourApp->Permissions->Camera]之前为应用程序启用相机权限。权限部分必须是 rewritten properly,这只是为了快速测试
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

javaCameraView = (CameraBridgeViewBase) findViewById(R.id.java_camera_view);

// added for black screen
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Permissions granted");
javaCameraView.setCameraPermissionGranted();
javaCameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_FRONT);
javaCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);
javaCameraView.setCvCameraViewListener(this);
} else {
Log.d(TAG, "Troubles");
}
}

祝你有美好的一天,
安东诺

关于android-studio - 在android studio中使用opencv进行人脸检测。 E/AndroidRuntime:致命异常:main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64354355/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com