我正在尝试使用相机和移动视觉API设备扫描QR码。为了做到这一点,我创建了一个SurfaceView
并获取了SurfaceView
的图像流并启用了BarcodeDetector
,然后是所需的方法来读取检测到的QR码。以下是活动的代码:
QrScanActivity.class
public class QrScanActivity extends AppCompatActivity
{
private SurfaceView cameraView;
private TextView barcodeInfo;
private BarcodeDetector barcodeDetector;
private CameraSource cameraSource;
private static final String TAG = "QR_ACTIVITY";
private static final int REQUEST_CAMERA = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr_scan);
cameraView = (SurfaceView) findViewById(R.id.camera_view);
barcodeInfo = (TextView) findViewById(R.id.code_info);
barcodeDetector =
new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();
cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setRequestedPreviewSize(640, 480)
.build();
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (ActivityCompat.checkSelfPermission(QrScanActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestCameraPermission();
}
try {
cameraSource.start(cameraView.getHolder());
} catch (IOException ie) {
Log.e("CAMERA SOURCE", ie.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
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() { // Use the post method of the TextView
public void run() {
barcodeInfo.setText( // Update the TextView
barcodes.valueAt(0).displayValue
);
}
});
}
}
});
}
private void requestCameraPermission() {
Log.i("CAM","CAMERA permission has NOT been granted. Requesting permission.");
// BEGIN_INCLUDE(camera_permission_request)
if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CAMERA))
{
Snackbar.make(findViewById(android.R.id.content), R.string.permission_camera_rationale,
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(QrScanActivity.this,
new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA);
}
})
.show();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA);
}
// END_INCLUDE(camera_permission_request)
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA)
{
Log.i(TAG, "Received response for Camera permission request.");
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "CAMERA permission has now been granted. Showing preview.");
Snackbar.make(findViewById(android.R.id.content), R.string.permision_available_camera,
Snackbar.LENGTH_SHORT).show();
} else {
Log.i(TAG, "CAMERA permission was NOT granted.");
Snackbar.make(findViewById(android.R.id.content), R.string.permissions_not_granted,
Snackbar.LENGTH_SHORT).show();
}
}
else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
activity_qr_scan.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_qr_scan"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:layout_width="400dp"
android:layout_height="280dp"
android:layout_marginTop="30dp"
android:id="@+id/camera_view"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/code_info"
android:layout_below="@+id/camera_view"
android:textSize="20sp"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:text="Nothing to read."
/>
</RelativeLayout>
运行应用时获得的logcat:
logcat的
E / AndroidRuntime:致命异常:主要 过程:com.shreybank.shrey,PID:12044 java.lang.RuntimeException:无法连接到摄像头服务 在android.hardware.Camera。(Camera.java:518) 在android.hardware.Camera.open(Camera.java:360) 在com.google.android.gms.vision.CameraSource.zzBZ(未知来源) 在com.google.android.gms.vision.CameraSource.start(未知来源) 在com.shreybank.shrey.activities.QrScanActivity $ 1.surfaceCreated(QrScanActivity.java:64) 在android.view.SurfaceView.updateWindow(SurfaceView.java:583) 在android.view.SurfaceView $ 3.onPreDraw(SurfaceView.java:177) 在android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2055) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6013) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:858) 在android.view.Choreographer.doCallbacks(Choreographer.java:670) 在android.view.Choreographer.doFrame(Choreographer.java:606) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5466) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
您必须设置运行时权限才能解决此问题。在开始QrScanActivity
之前,您已经打电话给我。
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, REQUEST_READ_CAMERA);
}
else {
// Already grant Camera permission. Now call your QR scan Activity
}
}else {
// call your QR scan Activity
}
当用户授予权限时,您可以打开QrScanActivity
onRequestPermissionsResult方法。
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_READ_CAMERA: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission has granted . Call the QR scan Activity
} else {
// permission denied
}
return;
}
}
}
答案 1 :(得分:0)
更新您的QrScanActivity,将您的相机源创建代码移动到initCameraSource方法,检查CAMERA的权限,如果被授予调用initCameraSource方法,否则请求权限。授予权限后,从onRequestPermissionsResult调用initCameraSource。
public class QrScanActivity extends AppCompatActivity
{
private SurfaceView cameraView;
private TextView barcodeInfo;
private BarcodeDetector barcodeDetector;
private CameraSource cameraSource;
private static final String TAG = "QR_ACTIVITY";
private static final int REQUEST_CAMERA = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr_scan);
int permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA);
if(permissionCheck == PackageManager.PERMISSION_GRANTED)
initCameraSource
else
requestCameraPermission();
}
private void requestCameraPermission() {
Log.i("CAM","CAMERA permission has NOT been granted. Requesting permission.");
// BEGIN_INCLUDE(camera_permission_request)
if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CAMERA))
{
Snackbar.make(findViewById(android.R.id.content), R.string.permission_camera_rationale,
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(QrScanActivity.this,
new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA);
}
})
.show();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA);
}
// END_INCLUDE(camera_permission_request)
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA)
{
Log.i(TAG, "Received response for Camera permission request.");
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "CAMERA permission has now been granted. Showing preview.");
initCameraSource();
} else {
Log.i(TAG, "CAMERA permission was NOT granted.");
Snackbar.make(findViewById(android.R.id.content), R.string.permissions_not_granted,
Snackbar.LENGTH_SHORT).show();
}
}
else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void initCameraSource(){
cameraView = (SurfaceView) findViewById(R.id.camera_view);
barcodeInfo = (TextView) findViewById(R.id.code_info);
barcodeDetector =
new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();
cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setRequestedPreviewSize(640, 480)
.build();
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (ActivityCompat.checkSelfPermission(QrScanActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestCameraPermission();
}
try {
cameraSource.start(cameraView.getHolder());
} catch (IOException ie) {
Log.e("CAMERA SOURCE", ie.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
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() { // Use the post method of the TextView
public void run() {
barcodeInfo.setText( // Update the TextView
barcodes.valueAt(0).displayValue
);
}
});
}
}
});
}
}