Android人脸检测SurfaceTexture无法正常工作

时间:2018-06-22 11:37:47

标签: android

我正在尝试创建一个使用相机检测人脸的android应用程序,但它不显示任何日志!我试图更改SurfaceTexture的数字,但它仍然不显示日志!使用SurfaceView可以正常工作,但是我不想在应用程序内部显示相机预览。

import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.io.IOException;

public class MainActivity extends AppCompatActivity
{
    private Camera mCamera = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mCamera = getCameraInstance();

        Camera.Parameters parameters = this.mCamera.getParameters();
        parameters.setRecordingHint(true);
        this.mCamera.setParameters(parameters);
        try
        {
            this.mCamera.setPreviewTexture(new SurfaceTexture(10));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        this.mCamera.startPreview();
        this.mCamera.setFaceDetectionListener(new Camera.FaceDetectionListener()
        {
            @Override
            public void onFaceDetection(Camera.Face[] faces, Camera camera)
            {
                if (faces.length > 0)
                {
                    Log.d("Face", "Detected");
                }
                else
                {
                    Log.d("Face", "Not detected");
                }
            }
        });

        mCamera.startFaceDetection();
    }

    private Camera getCameraInstance()
    {
        try
        {
            Camera c = Camera.open();
            return c;
        }
        catch (RuntimeException e)
        {
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }

        return null;
    }
}

0 个答案:

没有答案