在Android应用程序中整合zxing库期间,每件事都运行正常但是当我尝试用相机反复调用相机时遇到这个问题 这是我的logcat
02-24 20:49:34.775: W/CameraBase(15622): An error occurred while connecting to camera: 0
02-24 20:49:34.785: W/CaptureActivity(15622): Unexpected error initializing camera
02-24 20:49:34.785: W/CaptureActivity(15622): java.lang.RuntimeException: Fail to connect to camera service
02-24 20:49:34.785: W/CaptureActivity(15622): at android.hardware.Camera.<init>(Camera.java:545)
02-24 20:49:34.785: W/CaptureActivity(15622): at android.hardware.Camera.open(Camera.java:385)
02-24 20:49:34.785: W/CaptureActivity(15622): at com.google.zxing.client.android.camera.open.OpenCameraInterface.open(OpenCameraInterface.java:53)
02-24 20:49:34.785: W/CaptureActivity(15622): at com.google.zxing.client.android.camera.CameraManager.openDriver(CameraManager.java:80)
02-24 20:49:34.785: W/CaptureActivity(15622): at com.google.zxing.client.android.CaptureActivity.initCamera(CaptureActivity.java:652)
02-24 20:49:34.785: W/CaptureActivity(15622): at com.google.zxing.client.android.CaptureActivity.surfaceCreated(CaptureActivity.java:370)
02-24 20:49:34.785: W/CaptureActivity(15622): at android.view.SurfaceView.updateWindow(SurfaceView.java:607)
02-24 20:49:34.785: W/CaptureActivity(15622): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:184)
02-24 20:49:34.785: W/CaptureActivity(15622): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:921)
02-24 20:49:34.785: W/CaptureActivity(15622): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2169)
02-24 20:49:34.785: W/CaptureActivity(15622): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1196)
02-24 20:49:34.785: W/CaptureActivity(15622): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6675)
02-24 20:49:34.785: W/CaptureActivity(15622): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
02-24 20:49:34.785: W/CaptureActivity(15622): at android.view.Choreographer.doCallbacks(Choreographer.java:590)
02-24 20:49:34.785: W/CaptureActivity(15622): at android.view.Choreographer.doFrame(Choreographer.java:560)
02-24 20:49:34.785: W/CaptureActivity(15622): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
02-24 20:49:34.785: W/CaptureActivity(15622): at android.os.Handler.handleCallback(Handler.java:739)
02-24 20:49:34.785: W/CaptureActivity(15622): at android.os.Handler.dispatchMessage(Handler.java:95)
02-24 20:49:34.785: W/CaptureActivity(15622): at android.os.Looper.loop(Looper.java:145)
02-24 20:49:34.785: W/CaptureActivity(15622): at android.app.ActivityThread.main(ActivityThread.java:5944)
02-24 20:49:34.785: W/CaptureActivity(15622): at java.lang.reflect.Method.invoke(Native Method)
02-24 20:49:34.785: W/CaptureActivity(15622): at java.lang.reflect.Method.invoke(Method.java:372)
02-24 20:49:34.785: W/CaptureActivity(15622): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
02-24 20:49:34.785: W/CaptureActivity(15622): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
以下是集成zxing库的代码,请查看
@Override
public void onScanClicked(int position) {
barCodePosition = position;
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "SCAN_MODE");
startActivityForResult(intent, 123);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// We came from the scanning activity; the return intent contains a
// RESULT_EXTRA key
// whose value is an ArrayList of BarcodeResult objects that we found
// while scanning.
// Get the list of objects and add them to our list view.
System.out
.println(requestCode + "DDDDDDDDDDDDDDDDDDDDDDD" + resultCode);
switch (requestCode) {
case SCAN_CODE:
if (resultCode == RESULT_OK) {
ArrayList<BarcodeResult> barcodes = data
.getParcelableArrayListExtra(BarcodeScanActivity.RESULT_EXTRA);
if (barcodes != null && barcodes.size() > 0) {
BarcodeResult barCodeResult = barcodes.get(0);
String barCodeNumber = barCodeResult.barcodeString;
Toast.makeText(getApplicationContext(), barCodeNumber,
Toast.LENGTH_LONG).show();
scanAdapter.addBarcodes(barCodeNumber, barCodePosition);
}
}
break;
case SCANNER_REQ_CODE:
if (resultCode == RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
System.out.println(contents
+ "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT");
scanAdapter.addBarcodes(contents, barCodePosition);
}
break;
case gps_code:
switch (resultCode) {
case Activity.RESULT_OK:
Bundle bund = data.getExtras();
outletLatitude = bund.getString("LATITUDE");
outletLongitude = bund.getString("LONGITUDE");
diag = ProgressDialog.show(this, "Please wait",
"saving data...");
thd = new Thread(StockCountActivity.this);
thd.start();
break;
default:
}
case product_code:
switch (resultCode) {
case Activity.RESULT_OK:
Bundle bund = data.getExtras();
// purchaseVolume.setText(bund.getString("SALES_VOLUME"));
// purchaseValue.setText(bund.getString("SALES_VALUE"));
// salesDetails = bund.getString("SALES_DETAILS");
break;
case Activity.RESULT_CANCELED:
break;
default:
}
}
}
答案 0 :(得分:0)
在AndroidManifest.xml中添加权限:
<uses-permission android:name="android.permission.CAMERA"/>
答案 1 :(得分:0)
请看这里:W/CameraBase﹕ An error occurred while connecting to camera: 0
从Android 6.0(API级别23)开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时。此方法简化了应用安装过程,因为用户在安装或更新应用时无需授予权限。它还使用户可以更好地控制应用程序的功能;例如,用户可以选择让相机应用程序访问相机,但不能访问设备位置。用户可以通过转到应用程序的“设置”屏幕随时撤消权限。 http://developer.android.com/training/permissions/requesting.html