我已经查看了许多有关此问题和答案,但是在我的情况下它们不起作用。
我正在使用Android Studio开发QR码扫描应用程序。
但这给我一个错误:
“ java.lang.RuntimeException:无法连接到摄像头服务”
谁知道我错了?
这是清单:
<!-- Camera Requirement -->
<uses-feature android:name="android.hardware.camera" />
<!-- Camera Permissions -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- Features -->
<uses-feature android:name="android.hardware.camera.flash" />
<uses-feature android:name="android.hardware.camera.autofocus" />
下面是我的代码:
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseArray;
import android.view.View;
import android.webkit.URLUtil;
import android.widget.Toast;
import com.google.android.gms.vision.barcode.Barcode;
import java.util.List;
import info.androidhive.barcode.BarcodeReader;
public class ScanQRActivity extends AppCompatActivity implements BarcodeReader.BarcodeReaderListener {
private boolean isOnFlash = false;
static Camera camera;
static Camera.Parameters parameters;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_qr);
findViewById(R.id.btnLight).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isOnFlash) {
turnOnFlash();
}
else {
getCamera();
turnOffFlash();
}
}
});
findViewById(R.id.btnBack).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
parameters = camera.getParameters();
}
catch (Exception ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_SHORT).show();
}
}
}
private void turnOnFlash() {
if( ! getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
Toast.makeText(this, "Sorry, your device doesn't support flash light!", Toast.LENGTH_LONG).show();
}
try {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
isOnFlash = true;
}
catch (Exception ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_SHORT).show();
}
}
private void turnOffFlash() {
try {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();
isOnFlash = false;
}
catch (Exception ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
camera = null;
parameters = null;
}
}
@Override
public void onScanned(Barcode barcode) {
gotoResultActivity(barcode.displayValue);
}
@Override
public void onScannedMultiple(List<Barcode> barcodes) {
}
@Override
public void onBitmapScanned(SparseArray<Barcode> sparseArray) {
}
@Override
public void onScanError(String errorMessage) {
}
@Override
public void onCameraPermissionDenied() {
}
private void gotoResultActivity(String displayValue) {
if ( URLUtil.isValidUrl(displayValue) ) {
Intent intent = new Intent(this, ResultActivity.class);
Bundle bundle = new Bundle();
bundle.putString("url", displayValue);
intent.putExtras(bundle);
startActivity ( intent );
finish();
}
else {
Toast.makeText(this, "Invalid Url", Toast.LENGTH_SHORT).show();
}
}
}