我正在制作自定义相机应用程序。这需要表面视图并在表面视图中打开相机。
但是当在marshmallow上运行相同的应用程序时,我正在使用Runtime permission
来授予相机权限。我允许对话框消失但黑屏显示,相机无法打开。
我也试过在允许之后再次调用活动,但它不起作用。
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (preview) {
camera.stopFaceDetection();
camera.stopPreview();
preview = false;
}
if (camera != null) {
preview = true;
// Parameters
Parameters parameters = camera.getParameters();
parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
List<Camera.Size> sizeList = camera.getParameters().getSupportedPreviewSizes();
bestSize = sizeList.get(0);
for (int i = 1; i < sizeList.size(); i++) {
if ((sizeList.get(i).width * sizeList.get(i).height) >
(bestSize.width * bestSize.height)) {
bestSize = sizeList.get(i);
}
}
parameters.setPictureSize(bestSize.width, bestSize.height);
camera.setParameters(parameters);
camera.startPreview();
camera.startFaceDetection();
camera.setFaceDetectionListener(faceDetectionListener);
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
customsurfaceview();
}
public void customsurfaceview() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
Log.e("PLAYGROUND", "Permission is not granted, requesting");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 123);
}
Log.e("Entered Surace Created", "----------------");
}
public void SetPreview_onSurface() {
if (camera == null)
return;
camera = Camera.open();
try {
camera.setPreviewDisplay(surfaceView.getHolder());
} catch (IOException e) {
e.printStackTrace();
Log.e("printStackTrace", "--" + e.getMessage());
}
camera_button = (ImageView) findViewById(R.id.camera_button);
camera_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (capture_chek == 0) {
camera.cancelAutoFocus();
camera.takePicture(shutterCallback, null, pictureCallback);
capture_chek = 1;
}
}
});
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (camera != null) {
camera.release();
camera = null;
preview = false;
}
}
答案 0 :(得分:0)
这个问题可能太老了,但是可以帮助其他会遇到与我一样的问题的人。我的解决方法是...
在OnCreate()中,我删除了setContentView(R.layout ...),然后仅在授予许可的情况下,才在OnCreate()和onRequestPermissionsResult()中调用initializeUiAndCamera()。
在initializeUiAndCamera()中,我调用了setContentView(R.layout ...),并夸大了所需的所有视图。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.CAMERA), REQUEST_CAMERA)
else
initializeUiAndCamera()
}
private fun initializeUiAndCamera() {
setContentView(R.layout.activity_qr_scanner)
surfaceView = findViewById(R.id.surfaceView)
//...
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
when (requestCode) {
REQUEST_CAMERA -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)
initializeScanner()
else
Toast.makeText(applicationContext, "Camera Permission denied", Toast.LENGTH_SHORT).show()
}
}
}
TLDR;仅在授予权限时才增加视图。