人脸检测Android版本兼容性问题

时间:2015-12-09 10:36:37

标签: android android-studio face-detection android-sdk-manager

我在APK中有以下类代码,进行人脸检测并在相机预览上绘制自定义视图。人脸检测在My" Samsung-S3"但我测试了几个其他Android手机,脸部检测永远不会开始。为什么这样/如何让它工作? (请不要介意缩进)

     public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private MyDrawing md;

public CameraPreview(Context context, Camera camera) {
    super(context);
    mCamera = camera;
    mHolder = getHolder();
    mHolder.addCallback(this);
    // deprecated setting, but required on Android versions prior to 3.0
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void refreshCamera(Camera camera) {
    if (mHolder.getSurface() == null) {
        // preview surface does not exist
        return;
    }
    // stop preview before making changes
    try {
        mCamera.stopPreview();
    } catch (Exception e) {
        // ignore: tried to stop a non-existent preview
    }
    // set preview size and make any resize, rotate or
    // reformatting changes here
    // start preview with new settings
    setCamera(camera);
    try {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();
        //startFaceDetection();
    } catch (Exception e) {
        Log.d(VIEW_LOG_TAG, "Error starting camera preview: " + e.getMessage());
    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // If your preview can change or rotate, take care of those events here.
    // Make sure to stop the preview before resizing or reformatting it.
    refreshCamera(mCamera);
}

public void setCamera(Camera camera) {
    //method to set a camera instance
    mCamera = camera;
    mCamera.setFaceDetectionListener(faceDetectionListener);
    startFaceDetection();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
     mCamera.release();
}

private Camera.FaceDetectionListener faceDetectionListener = new Camera.FaceDetectionListener() {

    @Override
    public void onFaceDetection(Camera.Face[] faces, Camera c) {

        if (faces.length > 0) {

            Log.d("FaceDetection", "face detected X and Y are as: " + faces.length +
                    " Face 1 Location X: " + faces[0].rect.centerX() +
                    "Y: " + faces[0].rect.centerY() +" LIES IN "+(MyDrawing.w-MyDrawing.radius) +"--"+(MyDrawing.w+MyDrawing.radius));

            if(faces[0].rect.centerX()>=0 && faces[0].rect.centerX()<115 )
            {
                Log.d("ALERT = ", "Detection Started" );
                AndroidVideoCaptureExample.capture.setText("Recording/ stopNsave ");
                AndroidVideoCaptureExample.faceDetect();
            }

        } else {
            Log.d("FaceDetection", "circle cordinates are as: " + (MyDrawing.w-MyDrawing.radius) +"cX"+ MyDrawing.radius+"cY");

        }
    }
};

public void startFaceDetection(){
    // Try starting Face Detection
    Camera.Parameters params = mCamera.getParameters();

    // start face detection only *after* preview has started
    if (params.getMaxNumDetectedFaces() > 0){
        // camera supports face detection, so can start it:
        mCamera.startFaceDetection();
    }
}

支持Min的SDK设置为10

1 个答案:

答案 0 :(得分:0)

问题在于在Android Manifest文件中设置TargetSDK。在将4.4.4(KitKat)用作Target之前,我已将其设置为最新的Android M.现在工作正常.Bingo。