1

我无法用相机拍照并让它检测到任何人脸。我将图片显示在屏幕上,并且可以在图像上清楚地看到我的脸,但从未检测到它。我的 logcat 打印“找不到人脸!”

public void takePictureNoPreview(Context context) {
    camera = openFrontFacingCamera();
    if (camera != null) {
        try {
            SurfaceTexture dummy = new SurfaceTexture(0);
            camera.setPreviewTexture(dummy);
            camera.startPreview();

            camera.takePicture(null, null, this);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }

    } else {
        // booo, failed!
    }
}

@Override
public void onPictureTaken(byte[] data, Camera camera) {
    Log.i(LOG_TAG, "Picture taken");
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
    FaceDetector faceDetector = new FaceDetector(bitmap.getWidth(),
            bitmap.getHeight(), 1);
    Face[] faces = new Face[1];
    int foundFaces = faceDetector.findFaces(bitmap, faces);
    if (foundFaces > 0) {
        Log.i(LOG_TAG, "Found a face!");
    } else {
        Log.i(LOG_TAG, "No face found!");
    }
    camera.release();
    sendImageToActivity(bitmap);
}
4

1 回答 1

0

解决方案是将我的相机图像旋转 90 度。从相机返回的图像偏离了 90 度,这使 FaceDetector 无法正确处理图像。

Matrix matrix = new Matrix();
matrix.postRotate(90);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
于 2012-12-28T21:03:02.740 回答