我正在使用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;
}
}
};
}
这是我的错误,谁能帮忙?如果有人可以采取措施解决错误,那将非常有帮助。
最佳答案
再见,
我对您的代码有两种感觉:
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");
}
}
祝你有美好的一天,
安东诺