当我在可绘制文件夹中使用照片时,我的应用程序可以检测照片中的面孔。但是当我用相机拍摄照片时,我的应用程序无法检测到脸部。
这是我的代码,可处理可绘制文件夹中的照片:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable=true;
bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.test2, options);
Paint myRectPaint = new Paint();
myRectPaint.setStrokeWidth(5);
myRectPaint.setColor(Color.RED);
myRectPaint.setStyle(Paint.Style.STROKE);
Bitmap tempBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);
tempCanvas.drawBitmap(bitmap, 0,0,null);
FaceDetector faceDetector = new FaceDetector.Builder(getApplicationContext()).setTrackingEnabled(false).build();
if(!faceDetector.isOperational()){
Toast.makeText(getApplicationContext(), "face detection not working", Toast.LENGTH_SHORT).show();
}
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<Face> faces = faceDetector.detect(frame);
for(int i = 0; i < faces.size(); i++){
Face thisFace = faces.valueAt(i);
float x1 = thisFace.getPosition().x;
float y1 = thisFace.getPosition().y;
float x2 = x1 + thisFace.getWidth();
float y2 = y1 + thisFace.getHeight();
tempCanvas.drawRoundRect(new RectF(x1, y1, x2, y2), 2, 2, myRectPaint);
}
imageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));
这是我的照片代码,该照片是在相机中捕获的,无法检测到脸部
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable=true;
bitmap = BitmapFactory.decodeFile(pathToFile, options);
Paint myRectPaint = new Paint();
myRectPaint.setStrokeWidth(5);
myRectPaint.setColor(Color.RED);
myRectPaint.setStyle(Paint.Style.STROKE);
Bitmap tempBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);
tempCanvas.drawBitmap(bitmap, 0,0,null);
FaceDetector faceDetector = new FaceDetector.Builder(getApplicationContext()).setTrackingEnabled(false).build();
if(!faceDetector.isOperational()){
Toast.makeText(getApplicationContext(), "face detection not working", Toast.LENGTH_SHORT).show();
}
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<Face> faces = faceDetector.detect(frame);
for(int i = 0; i < faces.size(); i++){
Face thisFace = faces.valueAt(i);
float x1 = thisFace.getPosition().x;
float y1 = thisFace.getPosition().y;
float x2 = x1 + thisFace.getWidth();
float y2 = y1 + thisFace.getHeight();
tempCanvas.drawRoundRect(new RectF(x1, y1, x2, y2), 2, 2, myRectPaint);
}
imageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));
请帮助。谢谢!