我正在尝试创建一个将读取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>
答案 0 :(得分:0)
在清单中添加相同的权限。
答案 1 :(得分:0)
为时已晚,但这会为您提供帮助。
此后,您已经设置好相机并添加了预览和其他所有内容。只需致电
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