求助,无法在第二个方法中调用第一个方法的函数,不知道该怎么做。
public void initFaceDetector() throws IOException{
InputStream inputStream=getResources().openRawResource(R.raw.lbpcascade_frontalface);
File cascadeDir=this.getDir("cascade", Context.MODE_PRIVATE);
File file=new File(cascadeDir.getAbsoluteFile(),"lbpcascade_frontalface.xml");
FileOutputStream outputStream=new FileOutputStream(file);
byte[] buff=new byte[1024];
int len=0;
while ((len=inputStream.read(buff))!=-1){
outputStream.write(buff,0,len);
}
inputStream.close();
outputStream.close();
CascadeClassifier faceDetector=new CascadeClassifier(file.getAbsolutePath());
}
private void faceDetectDemo(Mat src,Mat dst) {
Mat gray=new Mat();
Imgproc.cvtColor(src,gray,Imgproc.COLOR_BGRA2GRAY);
MatOfRect face=new MatOfRect();
faceDetector
}
最后一句是非法的,在第二个方法中无法找到该函数。
CascadeClassifier faceDetector=new CascadeClassifier(file.getAbsolutePath()); 这个又不是全局变量怎么调用啊,在一个方法中要调用另一个方法的成员,那这个成员要是全局变量才可以。
你第二个方法private,调用public里的成员好像不行,你可以把private改成public,应该就可以了
这个....faceDetector是局部变量阿,没办法直接调用的,只存在于initFaceDetector这个方法,方法结束就成为可回收的变量了。解决就是写成全局,
CascadeClassifier faceDetector;
public void initFaceDetector() throws IOException{....}
这样才可以。
方法内的成员变量无法在方法外访问。你可以定义为类的成员变量访问。或者使用返回值的方式使用
public CascadeClassifier initFaceDetector() throws IOException{
InputStream inputStream=getResources().openRawResource(R.raw.lbpcascade_frontalface);
File cascadeDir=this.getDir("cascade", Context.MODE_PRIVATE);
File file=new File(cascadeDir.getAbsoluteFile(),"lbpcascade_frontalface.xml");
FileOutputStream outputStream=new FileOutputStream(file);
byte[] buff=new byte[1024];
int len=0;
while ((len=inputStream.read(buff))!=-1){
outputStream.write(buff,0,len);
}
inputStream.close();
outputStream.close();
return new CascadeClassifier(file.getAbsolutePath());
}
private void faceDetectDemo(Mat src,Mat dst) {
Mat gray=new Mat();
Imgproc.cvtColor(src,gray,Imgproc.COLOR_BGRA2GRAY);
MatOfRect face=new MatOfRect();
CascadeClassifier faceDetector = this.initFaceDetector();
}