0

我在 APK 中有以下类代码,进行面部检测并在相机预览上绘制自定义视图。面部检测在我的“Samsung-S3”上运行良好,但我在其他几部从未启动面部检测的安卓手机上对其进行了测试。为什么会这样/如何让它工作?(请不要介意缩进)

     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();
    }
}

最小支持的 SDK 设置为 10

4

1 回答 1

0

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

于 2015-12-10T05:52:18.867 回答