安卓相机黑屏
Posted
技术标签:
【中文标题】安卓相机黑屏【英文标题】:Android Camera Black Screen 【发布时间】:2016-11-26 09:52:09 【问题描述】:我正在尝试创建一个将读取 QR 码的应用程序,此代码间歇性地工作,但它会给出权限错误并且不会加载。我相信我已经修复了这些,但现在我得到了一个黑屏。没有错误所以我不知道在哪里看,但我觉得它与权限有关。
package com.sample.qrcodereadervision.Activity;
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import com.sample.qrcodereadervision.R;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.PermissionUtils;
import permissions.dispatcher.RuntimePermissions;
@RuntimePermissions
public class QRcodeReader extends AppCompatActivity
public final String TAG = getClass().getSimpleName();
private CameraSource cameraSource;
/** UI Parts : Preview screen */
private SurfaceView cameraView;
/** UI Parts : decoding results */
private TextView barcodeInfo;
private static final int REQUEST_CAMERA = 0x00000011;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
Log.d("QRcodeReader", "onCreate");
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED)
requestCameraPermission();
@Override
protected void onStart()
super.onStart();
Log.d(TAG, "onStart");
barcodeInfo = (TextView) findViewById(R.id.code_info);
cameraView = (SurfaceView) findViewById(R.id.camera_view);
startQRcodeReader();
// startCameraSource();
@Override
protected void onPause()
super.onPause();
Log.d(TAG, "onPause");
//@Override
//public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
// Log.d(TAG, "onRequestPermissionsResult");
// QRcodeReaderPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
//
private void startQRcodeReader()
Log.d(TAG, "startQRcodeReader");
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();
cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setAutoFocusEnabled(true)
.build();
QRcodeReaderPermissionsDispatcher.startCameraSourceWithCheck(QRcodeReader.this);
//requestCameraPermission();
//QR Code reading
barcodeDetector.setProcessor(
new Detector.Processor<Barcode>()
@Override
public void release()
@Override
public void receiveDetections(Detector.Detections<Barcode> detections)
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size() != 0)
barcodeInfo.post(
new Runnable()
public void run()
barcodeInfo.setText(
barcodes.valueAt(0).displayValue
);
);
);
@NeedsPermission(Manifest.permission.CAMERA)
void startCameraSource()
Log.d(TAG, "startCameraSource");
Toast.makeText(this, "\n" +
"Start the camera source", Toast.LENGTH_SHORT)
.show();
try
releaseCameraAndPreview();
if (cameraSource != null)
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
cameraSource.start(cameraView.getHolder());
return;
catch (IOException | SecurityException e)
Log.w(TAG, e);
@SuppressWarnings("unused")
@OnPermissionDenied(Manifest.permission.CAMERA)
void deniedPermission()
Log.d(TAG, "deniedPermission");
if (PermissionUtils.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA))
Toast.makeText(this, "\n" +
"It failed to camera startup .", Toast.LENGTH_SHORT)
.show();
@SuppressWarnings("unused")
@OnShowRationale(Manifest.permission.CALL_PHONE)
void showRationalForStorage(final PermissionRequest request)
Log.d(TAG, "showRationalForStorage");
Toast.makeText(this, "\n" +
"The camera of the use permit is required", Toast.LENGTH_SHORT)
.show();
request.proceed();
private void releaseCameraAndPreview()
if (cameraSource != null)
cameraSource.release();
cameraSource = null;
private void requestCameraPermission()
Log.i(TAG, "CAMERA permission has NOT been granted. Requesting permission.");
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA))
Log.i(TAG,
"Displaying camera permission rationale to provide additional context.");
Snackbar.make(findViewById(R.id.code_info), R.string.permission_camera_rationale,
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.ok, new View.OnClickListener()
@Override
public void onClick(View view)
ActivityCompat.requestPermissions(QRcodeReader.this,
new String[]Manifest.permission.CAMERA,
REQUEST_CAMERA);
)
.show();
else
ActivityCompat.requestPermissions(this, new String[]Manifest.permission.CAMERA,
REQUEST_CAMERA);
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
@NonNull int[] grantResults)
Log.d(TAG, "onRequestPermissionsResult");
switch (requestCode)
case REQUEST_CAMERA:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED)
QRcodeReaderPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
startCameraSource();
else
finish();
break;
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.qrcodereadervision">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus"/>
<application
android:theme="@style/AppTheme"
android:label="@string/app_name"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:supportsRtl="true">
<activity android:name=".Activity.QRcodeReader">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="barcode"/>
</application>
</manifest>
【问题讨论】:
请也分享清单。另外,您是在模拟器上还是在真机上进行测试? 您获得了权限吗?在清单中 我使用的是真实设备。带有 Android N 的 Nexus 6、带有 6.0.1 的 Nexus 7 平板电脑和带有 6.0 的 HTC M8。我已经拥有您在上面看到的所有权限。还是黑屏。 @Sobo 你有没有弄清楚黑屏是怎么回事?我对 Android N 也有同样的问题 @Sobo 遇到同样的问题,请问你处理好了吗? 【参考方案1】:很晚了,但它会帮助你。
之后,您已设置好相机并添加了预览和其他所有内容。只需拨打电话
recreate();
像这样在您的相机活动中使用示例。
camera = prepareCamera(camera); // a method to get camera instance
preview = createCameraPreview(camera) // a method which returns object of surfaceview
showCameraPreview(preview); // adding preview to the frame
recreate(); // <---This line should be called every time we setup a camera preview
【讨论】:
【参考方案2】:在清单中添加相同的权限。
【讨论】:
以上是关于安卓相机黑屏的主要内容,如果未能解决你的问题,请参考以下文章