为什么我的Surfaceview不显示相机预览?

时间:2018-07-01 17:52:09

标签: java android camera

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_barcode_scanner);
    setSupportActionBar(findViewById(R.id.toolbar));
    BarcodeDetector detector = new BarcodeDetector.Builder(this)
            .setBarcodeFormats(Barcode.QR_CODE)
            .build();
    CameraSource cameraSource = new CameraSource.Builder(this, detector)
            .setFacing(CameraSource.CAMERA_FACING_BACK)
            .setAutoFocusEnabled(true)
            .setRequestedFps(60f)
            .build();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        Log.d(TAG, "onCreate: the barcode scanner does not have the appropriate permissions");
        return;
    }
    try {
        cameraSource.start(findViewById(R.id.camera_view));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

这是我的onCreate()代码。我认为这与权限无关,因为我允许我的应用访问手机的摄像头。我试图弄清楚为什么我的曲面视图只显示一个黑色正方形,而不是正确显示相机预览。 任何帮助将不胜感激,谢谢。

0 个答案:

没有答案