Camersource在华为设备上

时间:2018-06-08 23:04:14

标签: android

我在我的移动应用程序中使用Camerasource并且它在所有设备上都能正常工作,除了华为设备它有一个黑色窗口虽然我把相机权限检查但它甚至没有显示它,甚至当我尝试打开手动从设备本身获得相机的权限问题没有解决。 你能告诉我这是什么问题吗?

修改

这些是相机权限检查:

public void askForContactPermission() throws IOException {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(ResultActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {


            if (ActivityCompat.shouldShowRequestPermissionRationale(ResultActivity.this,
                    Manifest.permission.CAMERA)) {
                AlertDialog.Builder builder = new AlertDialog.Builder(ResultActivity.this);
                builder.setTitle("Contacts access needed");
                builder.setPositiveButton(android.R.string.ok, null);
                builder.setMessage("please confirm Contacts access");//TODO put real question
                builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @TargetApi(Build.VERSION_CODES.M)
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        requestPermissions(
                                new String[]
                                        {Manifest.permission.CAMERA}
                                , RequestCameraPermissionID);
                    }
                });
                builder.show();




            } else {



                ActivityCompat.requestPermissions(ResultActivity.this,
                        new String[]{Manifest.permission.CAMERA},
                        RequestCameraPermissionID);

                // MY_PERMISSIONS_REQUEST_CAMERA is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        } else {
            cameraSource.start(cameraView.getHolder());
        }
    } else {
        cameraSource.start(cameraView.getHolder());
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case RequestCameraPermissionID: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                try {
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(ResultActivity.this, new String[]{Manifest.permission.CAMERA}, RequestCameraPermissionID);
                        return;
                    }else {
                        cameraSource.start(cameraView.getHolder());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }


            } else {
                Toast.makeText(this, "No permission for camera", Toast.LENGTH_SHORT).show();
                ActivityCompat.requestPermissions(ResultActivity.this, new String[]{Manifest.permission.CAMERA}, RequestCameraPermissionID);

            }
            return;
        }


    }
}

这是代码中的camerasource用法:

cameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer)
                .setFacing(CameraSource.CAMERA_FACING_BACK)
                .setRequestedPreviewSize(1280, 1024)
                .setRequestedFps(2.0f)
                .setAutoFocusEnabled(true)
                .build();


        cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {


            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                try {
                    askForContactPermission();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }


            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                cameraSource.stop();
            }
        });

和清单中的权限:

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>

0 个答案:

没有答案