Getting Camera error in Zxing Barcode Application

匿名 (未验证) 提交于 2019-12-03 01:47:02

问题:

I am trying to use Zxing Library for developing a barcode scanner.

My activity is as follows:

public class Scanner extends Activity {        private static final String PACKAGE = "com.test.scan";       private static final String SCANNER = "com.google.zxing.client.android.SCAN";       private static final String SCAN_FORMATS = "UPC_A,UPC_E,EAN_8,EAN_13,CODE_39,CODE_93,CODE_128";       private static final String SCAN_MODE = "QR_CODE_MODE";       public static final int REQUEST_CODE = 1;        @Override       public void onCreate(Bundle icicle) {           super.onCreate(icicle);            setContentView(R.layout.main);             Button ok;            ok = (Button) findViewById(R.id.b1);            ok.setOnClickListener(new View.OnClickListener() {                 public void onClick(View v) {                    Intent scanIntent = new Intent(SCANNER);                   scanIntent.setPackage(PACKAGE);                   scanIntent.addCategory(Intent.CATEGORY_DEFAULT);                   scanIntent.putExtra("SCAN_FORMATS", SCAN_FORMATS);                   scanIntent.putExtra("SCAN_MODE", SCAN_MODE);                   try {                       startActivityForResult(scanIntent, REQUEST_CODE);                   } catch (ActivityNotFoundException e) {                        // TODO: Exception handling                   }               }            });        } 

Also manifest file:

    

But I am getting the following error:

"Sorry, the Android camera encountered a problem. You may need to restart the device".

I have followed few blogs.

Log:

   Unexpected error initializating camera       01-27 10:40:48.281: WARN/CaptureActivity(1007): java.lang.RuntimeException: Fail to connect to camera service       01-27 10:40:48.281: WARN/CaptureActivity(1007):     at android.hardware.Camera.native_setup(Native Method)       01-27 10:40:48.281: WARN/CaptureActivity(1007):     at android.hardware.Camera.(Camera.java:185)       01-27 10:40:48.281: WARN/CaptureActivity(1007):     at android.hardware.Camera.open(Camera.java:165)    01-27 10:40:48.281: WARN/CaptureActivity(1007):     at com.google.zxing.client.android.camera.CameraManager.openDriver(CameraManager.java:126)     01-27 10:40:48.281: WARN/CaptureActivity(1007):     at com.google.zxing.client.android.CaptureActivity.initCamera(CaptureActivity.java:606)       01-27 10:40:48.281: WARN/CaptureActivity(1007):     at com.google.zxing.client.android.CaptureActivity.surfaceCreated(CaptureActivity.java:346)        01-27 10:40:48.281: WARN/CaptureActivity(1007):     at android.view.SurfaceView.updateWindow(SurfaceView.java:532)       01-27 10:40:48.281: WARN/CaptureActivity(1007):     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:339)  

回答1:

This means the device returned null from Camera.open() and it shouldn't ever do that. It's treated as a device bug. I am not sure how you would debug why it is doing this, but that's the cause.

The only time I have seen this happen consistently is on Android 2.2 devices that have a front camera, only. The API for accessing a front camera only appeared in Android 2.3, and the previous Camera.open() API method may only return a rear-facing camera. So these return null. And it is a device bug, really, since they really need to be running Android 2.3 to let apps use a front camera.



回答2:

For Android 6+, because of the "permission" issue, If you got the message "Sorry, the camera encountered a problem. You may need to restart the device.", go to Settings - Apps - find "your app name" - select Permissions and switch on "Camera".



回答3:

permission of camera

is not at proper place. it should be after application tag.



回答4:

I faced a similar problem. While testing on Android M,the camera requires runtime permission. Adding this answer just for reference so as to help anyone who is stuck in a similar scenario.



回答5:

It's possible that you have a Device Administrator disabling access to the camera. Should check if the android camera app can start up or if it complains of a device admin.



回答6:

I got the same problem Fixed it with

SurfaceHolder surfaceHolder = surfaceView.getHolder(); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

Note: this seems to be a recent issue with the OS ( pre V3.0 Android) as the code was previously working.



回答7:

I was able to sort the issue out by simply placing the camera permissions in the main application.



回答8:

If you are invoking camera in android emulator for Android 2.2 it will give exception . While it runs fine on a device.



回答9:

Check out your code, I think your code trying to open camera multiple times. Download google zxing barcode scanner source code and then try it.



回答10:

it is useful https://github.com/zxing/zxing/wiki/Scanning-Via-Intent (formely hosted on Google Code, now on GitHub)



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!