我正在尝试用zxing制作一个简单的条码扫描器,我使用了以下代码
public class MainActivity extends Activity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v){
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
@Override
protected void onPause() {
super.onPause();
mScannerView.stopCamera();
}
@Override
public void handleResult(Result result) {
//Do anything with result here :D
Log.w("handleResult", result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan result");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();
//Resume scanning
//mScannerView.resumeCameraPreview(this);
}
}
但我继续收到以下错误
连接相机时出错:0
这是日志
08-23 13:07:09.412 24318-24318/com.gnirt69.qrcodesannerexample V/ViewRootImpl: Contents drawing finished : com.gnirt69.qrcodesannerexample/com.gnirt69.qrcodesannerexample.MainActivity
08-23 13:07:09.414 24318-24318/com.gnirt69.qrcodesannerexample I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@d9ac315 time:103259036
08-23 13:07:25.832 24318-24318/com.gnirt69.qrcodesannerexample I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
08-23 13:07:25.869 24318-24318/com.gnirt69.qrcodesannerexample I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
08-23 13:07:25.876 24318-24318/com.gnirt69.qrcodesannerexample I/AudioManagerEx: AudioManagerEx created
08-23 13:07:25.941 24318-24890/com.gnirt69.qrcodesannerexample W/CameraBase: An error occurred while connecting to camera: 0
这是清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gnirt69.qrcodesannerexample">
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
如何修改上述代码以消除此错误? 感谢
答案 0 :(得分:0)
在清单中加入
<uses-feature android:name="android.hardware.camera2" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />