ML Kit FaceDetection CameraSourcePreview为空

时间:2018-11-22 13:04:50

标签: android firebase firebase-mlkit

我已经在我的应用中实现了Firebase ML Kit,用于人脸检测。在演示应用程序中,我在其中创建了一个新的Firebase项目并添加了google json文件,其工作正常。但是在现有应用程序(Playstore应用程序上的应用程序)中,它为CameraSourcePreviewgraphicOverlay声明返回null。以下是我在应用程序中使用的代码:

public class ChildModeActivity extends BaseActivity {
    private CameraSource cameraSource = null;
    private CameraSourcePreview preview;
    private GraphicOverlay graphicOverlay;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_child_mode);       
        preview = (CameraSourcePreview) findViewById(R.id.childCameraPreview);
        if (preview == null) {
            Log.d("TAG_FACE", "Preview is null");
        }
        graphicOverlay = (GraphicOverlay) findViewById(R.id.childFaceOverlay);
        if (graphicOverlay == null) {
            Log.d("TAG_FACE", "graphicOverlay is null");
        }       
        if (allPermissionsGranted()) {
        createCameraSource();
        } else {
            getRuntimePermissions();
        }
    }
    private void startCameraSource() {
        if (cameraSource != null) {
            try {
                if (preview == null) {
                    Log.d("TAG_FACE", "resume: Preview is null");
                }
                if (graphicOverlay == null) {
                    Log.d("TAG_FACE", "resume: graphOverlay is null");
                }
                preview.start(cameraSource, graphicOverlay);
            } catch (IOException e) {
                Log.e("TAG_FACE", "Unable to start camera source.", e);
                cameraSource.release();
                cameraSource = null;
            }
        }
    }

    private void createCameraSource() {
        try {
        // If there's no existing cameraSource, create one.
            if (cameraSource == null) {
                Log.i("TAG_FACE", "Camera Source is Null so create new one");
                cameraSource = new CameraSource(this, graphicOverlay);
                cameraSource.setFacing(CameraSource.CAMERA_FACING_FRONT);
            //  cameraSource = new CameraSource(this);
                cameraSource.setMachineLearningFrameProcessor(new FaceDetectionProcessor());
            }
        }catch (Exception e){
            Log.i("TAG_FACE" , " Create Camera Source Exception:" + e.getMessage());
        }
    }

在上面的代码中,我的BaseActivity扩展了AppCompatActivity

每当我运行该应用程序时,都会在日志中看到“预览为空”和“ graphicOverlay为空”。预览不显示。

0 个答案:

没有答案