在Zxing条形码应用程序中出现相机错误

23

我想使用Zxing库开发一个条形码扫描器。

我的活动如下:

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  
                }  
            }  

        });  
     }

同时还有清单文件:

    <activity android:name=".Scanner"
            android:screenOrientation="landscape"   android:configChanges="orientation|keyboardHidden"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:windowSoftInputMode="stateAlwaysHidden">  
                 <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>

</activity>

<activity android:name="com.google.zxing.client.android.CaptureActivity"
                android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden"  
                android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                android:windowSoftInputMode="stateAlwaysHidden">
                <intent-filter>
                    <action     android:name="com.google.zxing.client.android.SCAN" />
                    <category    android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
            <activity android:name="com.google.zxing.client.android.PreferencesActivity"
                android:label="@string/preferences_name">
            </activity>
            <activity android:name="com.google.zxing.client.android.HelpActivity"
                android:screenOrientation="user">
                <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
            <activity android:name="com.google.zxing.client.android.share.ShareActivity"
                android:label="@string/share_name" android:screenOrientation="user"
                android:theme="@android:style/Theme.Light">
                <intent-filter>
                    <action android:name="com.google.zxing.client.android.SHARE" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
    <uses-permission android:name="android.permission.CAMERA"

但是我遇到了以下错误:

"很抱歉,Android相机遇到问题。您可能需要重启设备。"

我已经按照一些博客的步骤进行了操作。

日志:

   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.<init>(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) 
11个回答

37

针对 Android 6+ 版本,由于“权限”问题,如果您看到了“抱歉,相机遇到了问题。您可能需要重新启动设备。”的提示信息,请前往设置 - 应用程序 - 找到“您的应用程序名称” - 选择权限并开启“相机”权限。


这个帮了我大忙,谢谢!但是什么是“权限问题”? - Deleplace
2
为 Android 6.0 设计的应用现在将在需要时请求权限。例如,安装应用程序时不会授予应用程序访问相机的权限,而是在应用程序首次要求访问相机时提示您。 - 1nteger
只有当应用程序的目标为Android 6+时,才是真实的。 - Sean Owen

16

相机权限

<uses-permission android:name="android.permission.CAMERA"

位置不正确,它应该在 application 标签后面。


你的手机里有SD卡吗?你在其他设备上尝试过相同的代码吗? - Connecting life with Android
是的。我的设备中有SD卡。我也在Galaxy Tab上尝试过。 - Smitha
请查看以下链接:http://www.java2s.com/Open-Source/Android/App/imhotep/com/google/zxing/client/android/CaptureActivity.java.htm 您在initCamera()方法中遇到了错误,请尝试找出原因。 - Connecting life with Android
是的,但我不知道问题出在哪里。让我检查一下。 - Smitha
权限顺序正确吗?在</application>之后,您应该提供权限。 - Connecting life with Android
没错,问题解决了。很抱歉没有注意到,谢谢你的解决方案 :-) - Smitha

7
这意味着设备从Camera.open()返回了null,这是不应该发生的。这被视为设备错误。我不确定您如何调试它为什么会出现这种情况,但这就是原因。
我唯一看到这种情况经常发生的是在只有前置摄像头的Android 2.2设备上。访问前置摄像头的API仅出现在Android 2.3中,并且以前的Camera.open() API方法可能只返回后置摄像头,因此这些返回null。这实际上是一个设备错误,因为他们真的需要运行Android 2.3才能让应用程序使用前置摄像头。

@SeanOwen 在我的情况下,我有一部三星Galaxy S(Android 2.3.6)手机,它有前置和后置摄像头,当我尝试使用条形码扫描器时,我会收到这个警告。有没有什么解决方法?我正在使用最新版本的BarcodeScanner。两天前下载的。谢谢。 - Edison Santos
它不能使固件行为发生改变。如果它返回 null,那么就返回 null。这不是应用程序的功能。 - Sean Owen
@ConnectinglifewithAndroid,你解决了这个问题吗?我在使用前置摄像头时也遇到了这个问题。 - Qadir Hussain
这里的问题是相机权限的位置。可能有很多原因。你可以自己找到它。还要确保它不是设备故障。 - Connecting life with Android
不,那不是正确的。没有授权,你会得到另一种失败。 - Sean Owen
显示剩余4条评论

2

我通过代码请求应用程序的权限成功解决了Android 8.1.0上的问题。

原因是(我认为)由于在较新的Android版本中处理设备访问的政策发生了变化,您必须在代码中请求权限。仅在XML中放置所需权限是不够的。用户在使用应用程序时必须有拒绝访问的选择。

要激活“授权弹出窗口”,您可以在MainActivity的onCreate内使用以下代码:

    if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA)
            == PackageManager.PERMISSION_DENIED) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.CAMERA}, MY_CAMERA_REQUEST_CODE);
    }

它应该具有与通过设置激活权限相同的效果。但在实际应用中,从应用程序内部请求权限可能更加用户友好。


2

我遇到了类似的问题。 在测试Android M时,相机需要运行时权限。添加这个答案只是为了参考,以帮助任何陷入类似情况的人。


2

只需使用Toast添加相机权限:

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
              == PackageManager.PERMISSION_GRANTED) {
          // Permission is already available, start camera preview
          Toast.makeText(this,"camera permission granted",Toast.LENGTH_LONG).show();


      } else {
          // Permission is missing and must be requested.
          requestCameraPermission();
      }

并且:

private void requestCameraPermission() {
        // Permission has not been granted and must be requested.
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.CAMERA)) {
            // Provide an additional rationale to the user if the permission was not granted
            // and the user would benefit from additional context for the use of the permission.
            // Display a SnackBar with cda button to request the missing permission.
            Toast.makeText(this, "Camera access is required to Scan The Barcode.",
                    Toast.LENGTH_LONG).show();


                    // Request the permission
                    ActivityCompat.requestPermissions(CaptureActivity.this,
                            new String[]{Manifest.permission.CAMERA},
                            PERMISSION_REQUEST_CAMERA);



        } else {
            Toast.makeText(this,
                    "<b>Camera could not be opened.</b>\\nThis occurs when the camera is not available (for example it is already in use) or if the system has denied access (for example when camera access has been disabled).", Toast.LENGTH_SHORT).show();
            // Request the permission. The result will be received in onRequestPermissionResult().
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CAMERA);
        }
    }

0

我遇到了同样的问题 用以下方法解决了

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

注意:这似乎是操作系统(V3.0之前的Android)的一个最近问题,因为代码以前是可行的。

0

可能是您的设备管理员禁用了相机访问权限。应该检查Android相机应用程序是否可以启动,或者是否出现设备管理员的错误提示。


-1

我通过将相机权限放置在主应用程序中,成功解决了这个问题。


-2

如果您在 Android 2.2 模拟器中调用相机,它会抛出异常。但在设备上运行正常。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接