我正在开发我的一个Android项目,其中我已经实现了Famous Zbar条形码扫描器,如下所示
surfaceViewFlash = (SurfaceView) mView.findViewById(R.id.PREVIEW);
surfaceViewFlash.setVisibility(View.INVISIBLE);
surfaceViewFlash.setVisibility(View.VISIBLE);
mCameraScanner = getCameraInstance();
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
previewingScanner = true;
autoFocusHandlerScanner = new Handler();
// Instance barcode zBarScanner
zBarScanner = new ImageScanner();
zBarScanner.setConfig(0, Config.X_DENSITY, 3);
zBarScanner.setConfig(0, Config.Y_DENSITY, 3);
mPreviewScanner = new CameraPreview(getActivity(), mCameraScanner, previewCb, autoFocusCB);
FrameLayout preview = (FrameLayout) mView.findViewById(R.id.cameraPreview);
preview.addView(mPreviewScanner);
if (barcodeScanned) {
barcodeScanned = false;
mCameraScanner.setPreviewCallback(previewCb);
mCameraScanner.startPreview();
previewingScanner = true;
mCameraScanner.autoFocus(autoFocusCB);
Log.e("initControls", "initControls");
}
/ ** *获取Camera对象实例的安全方法。 * /
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open();
} catch (Exception e) {
}
return c;
}
private void releaseCamera() {
if (mCameraScanner != null) {
mCameraScanner.cancelAutoFocus();
previewingScanner = false;
mCameraScanner.setPreviewCallback(null);
mPreviewScanner.getHolder().removeCallback(mPreviewScanner);
mCameraScanner.stopPreview();
mCameraScanner.release();
mCameraScanner = null;
mPreviewScanner = null;
}
}
public Runnable doAutoFocus = new Runnable() {
public void run() {
try {
if (previewingScanner) {
handlerForAutoFocus.postDelayed(doAutoFocus,2000);
Camera.Parameters parameters = null;
mCameraScanner=mCameraScanner.open();
if (null != mCameraScanner.getParameters()) {
parameters = mCameraScanner.getParameters();
}
List<String> focusModes = parameters.getSupportedFocusModes();
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
mCameraScanner.setParameters(parameters);
mCameraScanner.autoFocus(autoFocusCB);
}
}
} catch (Exception e) {
e.printStackTrace();
Log.e("doAutoFocus", e.getMessage());
}
}
};
public Camera.PreviewCallback previewCb = new Camera.PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
try {
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = parameters.getPreviewSize();
Image barcode = new Image(size.width, size.height, "Y800");
barcode.setData(data);
int result = zBarScanner.scanImage(barcode);
if (result != 0) {
previewingScanner = false;
mCameraScanner.setPreviewCallback(null);
mCameraScanner.stopPreview();
SymbolSet syms = zBarScanner.getResults();
for (Symbol sym : syms) {
/*Log.i("<<<<<<Asset Code>>>>> ","<<<<Bar Code>>> " + sym.getData());*/
String scanResult = sym.getData().trim();
int type = sym.getType();
BarcodeFormat name = BarcodeFormat.getFormatById(type);
String nameOfBarcode = name.getName();
DataToPref.setSharedPreferanceData(getActivity(), BarcodeConstant.SCANNEDMESSAGE, scanResult + "");
// showMessage(getActivity(), getString(R.string.app_name), scanResult + " " + nameOfBarcode);
showMessage(getActivity(), getString(R.string.app_name), scanResult + "");
barcodeScanned = true;
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
// Mimic continuous auto-focusing
Camera.AutoFocusCallback autoFocusCB = new Camera.AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
autoFocusHandlerScanner.postDelayed(doAutoFocus, 2000);
}
};
以上代码在许多设备上运行良好,例如nexus 5,samsung core等等。最近我在三星Tab中测试了应用程序,但我在这里得到了错误
mCameraScanner=mCameraScanner.open();
if (null != mCameraScanner.getParameters()) {
parameters = mCameraScanner.getParameters();
}
错误是
连接到相机时发生错误:0 09-02 13:16:39.357 14292-14292 / com.hyper.barcodeapp W / System.err:java.lang.RuntimeException:无法连接到摄像头服务 09-02 13:16:39.367 14292-14292 / com.hyper.barcodeapp W / System.err:at android.hardware.Camera.native_setup(Native Method) 09-02 13:16:39.367 14292-14292 / com.hyper.barcodeapp W / System.err:在android.hardware.Camera。(Camera.java:419) 09-02 13:16:39.367 14292-14292 / com.hyper.barcodeapp W / System.err:在android.hardware.Camera.open(Camera.java:389) 09-02 13:16:39.367 14292-14292 / com.hyper.barcodeapp W / System.err:at com.hyper.barcodeapp.fragment.ScannerFragment $ 4.run(ScannerFragment.java:425) 09-02 13:16:39.367 14292-14292 / com.hyper.barcodeapp W / System.err:at android.os.Handler.handleCallback(Handler.java:733) 09-02 13:16:39.377 14292-14292 / com.hyper.barcodeapp W / System.err:at android.os.Handler.dispatchMessage(Handler.java:95) 09-02 13:16:39.377 14292-14292 / com.hyper.barcodeapp W / System.err:at android.os.Looper.loop(Looper.java:146) 09-02 13:16:39.377 14292-14292 / com.hyper.barcodeapp W / System.err:在android.app.ActivityThread.main(ActivityThread.java:5598) 09-02 13:16:39.377 14292-14292 / com.hyper.barcodeapp W / System.err:at java.lang.reflect.Method.invokeNative(Native Method) 09-02 13:16:39.377 14292-14292 / com.hyper.barcodeapp W / System.err:at java.lang.reflect.Method.invoke(Method.java:515) 09-02 13:16:39.377 14292-14292 / com.hyper.barcodeapp W / System.err:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1283) 09-02 13:16:39.377 14292-14292 / com.hyper.barcodeapp W / System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 09-02 13:16:39.377 14292-14292 / com.hyper.barcodeapp W / System.err:at dalvik.system.NativeStart.main(Native Method)
任何人都可以帮我解决这个问题吗?我在扫描仪区域出现黑屏。
答案 0 :(得分:0)
您是否添加了正确的权限...
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />