【问题标题】:Getting Camera error in Zxing Barcode Application在 Zxing 条码应用程序中出现相机错误
【发布时间】:2012-02-20 04:09:16
【问题描述】:

我正在尝试使用 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) 

【问题讨论】:

    标签: android android-camera barcode-scanner zxing


    【解决方案1】:

    对于Android 6+,由于“权限”问题,如果您收到消息“抱歉,相机遇到问题。您可能需要重启设备。”,进入设置-应用程序-找到“您的应用程序”名称” - 选择权限并打开“相机”。

    【讨论】:

    • 这让我感到困惑,谢谢!但什么是“权限问题”?
    • 专为 Android 6.0 设计的应用程序现在会在需要时请求权限。例如,当您安装某个应用程序时,它不会访问您的相机,而是在该应用程序第一次想要访问您的相机时提示您。
    • 只有当应用程序以 Android 6+ 为目标时才适用。
    【解决方案2】:

    摄像头权限

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

    位置不正确。它应该在应用程序标签之后。

    【讨论】:

    • 你的手机有sd卡吗?您是否在其他设备上尝试过相同的代码?
    • 是的。我的设备中有 SD 卡。我也试过银河标签
    • 查看链接:java2s.com/Open-Source/Android/App/imhotep/com/google/zxing/… 您在 initCamera() 方法中遇到错误。尝试找出原因。
    • 是的.. 但我不明白是什么问题。让我看看
    • 权限顺序是否正确?在 之后你应该提供权限。
    【解决方案3】:

    这意味着设备从Camera.open() 返回null,它不应该这样做。它被视为设备错误。我不确定您将如何调试它为什么会这样做,但这就是原因。

    我唯一一次看到这种情况一直发生在配备前置摄像头的 Android 2.2 设备上。访问前置摄像头的API只出现在Android 2.3中,之前的Camera.open() API方法可能只返回后置摄像头。所以这些返回null。这确实是一个设备错误,因为它们确实需要运行 Android 2.3 才能让应用使用前置摄像头。

    【讨论】:

    • 是的,它甚至支持 1.5。 4.0 将需要 2.1。
    • @SeanOwen 在我的情况下,我有一个三星 Galaxy S (Android 2.3.6),它有一个前后摄像头,当我尝试使用条形码扫描仪时,我收到了这个警告。有办法解决吗?我正在使用最新版本的 BarcodeScanner。 2天前下载的。谢谢。
    • 它无法使固件正常运行,不。如果它返回 null,则返回 null。这不是应用程序的功能。
    • @ConnectinglifewithAndroid 你解决了这个问题吗?我在使用前置摄像头时也得到了这个
    • 这里的问题是相机权限的地方。可能有很多原因。您可以自己找到它。还要确保它不是设备错误。
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

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

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

      要激活“give-permission-popup”,您可以使用以下命令,例如在 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);
          }
      

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

      【讨论】:

        【解决方案6】:

        只需使用 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);
                }
            }
        

        【讨论】:

          【解决方案7】:

          您的设备管理员可能禁止访问摄像头。应该检查android相机应用程序是否可以启动或者它是否抱怨设备管理员。

          【讨论】:

            【解决方案8】:

            我遇到了同样的问题 用

            修复它
            SurfaceHolder surfaceHolder = surfaceView.getHolder();
            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            

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

            【讨论】:

              【解决方案9】:

              我只需将相机权限放在主应用程序中即可解决问题。

              【讨论】:

                【解决方案10】:

                如果您在 Android 2.2 的 android 模拟器中调用摄像头,则会出现异常。虽然它在设备上运行良好。

                【讨论】:

                  【解决方案11】:

                  查看您的代码,我认为您的代码试图多次打开相机。 下载google zxing条码扫描器源码试试看。

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2011-12-15
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2018-01-11
                    • 1970-01-01
                    相关资源
                    最近更新 更多