首先将LibZxing库下载导入android studio直接使用即可
清单文件application中配置zxing相关
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidzxing">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
<activity
android:name="com.google.zxing.client.android.CaptureActivity2"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.Transparent"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!--
Allow web apps to launch Barcode Scanner by linking to
http://zxing.appspot.com/scan.
-->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="zxing.appspot.com"
android:path="/scan"
android:scheme="http" />
</intent-filter>
<!-- We also support a Google Product Search URL. -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="www.google.com"
android:path="/m/products/scan"
android:scheme="http" />
</intent-filter>
<!-- And the UK version. -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="www.google.co.uk"
android:path="/m/products/scan"
android:scheme="http" />
</intent-filter>
</activity>
</application>
</manifest>
扫描
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final int SCAN_REQUEST_CODE = 100;
private static final int CAMERA_PERMISSION = 110;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT > 22) {
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.CAMERA}, CAMERA_PERMISSION);
} else {
startScanActivity();
}
} else {
startScanActivity();
}
}
private void startScanActivity() {
Intent intent = new Intent(MainActivity.this, CaptureActivity2.class);
intent.putExtra(CaptureActivity2.USE_DEFUALT_ISBN_ACTIVITY, true);
startActivityForResult(intent, SCAN_REQUEST_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case CAMERA_PERMISSION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startScanActivity();
} else {
Toast.makeText(MainActivity.this, "请手动打开摄像头权限", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
@Override
public int checkSelfPermission(String permission) {
return super.checkSelfPermission(permission);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SCAN_REQUEST_CODE) {
//Todo Handle the isbn number entered manually
String isbn = data.getStringExtra("CaptureIsbn");
if (!TextUtils.isEmpty(isbn)) {
//todo something
Toast.makeText(this, "解析到的内容为" + isbn, Toast.LENGTH_LONG).show();
}
}
}
}
}
LibZxing请在demo中下载demo链接