android使用zxing实现扫码功能并附上demo
时间: 2023-11-13 09:05:35 浏览: 162
好的,下面是使用ZXing实现Android扫码功能的示例代码。
首先,添加ZXing依赖库到你的项目中。可以使用Gradle,在build.gradle文件中添加以下代码:
```
implementation 'com.google.zxing:core:3.4.0'
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
```
然后,在你的layout文件中添加一个用于预览扫描结果的SurfaceView:
```xml
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
接下来,在你的Activity中实现扫描逻辑。首先,初始化扫描器并启动扫描:
```java
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.journeyapps.barcodescanner.BarcodeCallback;
import com.journeyapps.barcodescanner.BarcodeEncoder;
import com.journeyapps.barcodescanner.BarcodeResult;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private static final String TAG = MainActivity.class.getSimpleName();
private static final int REQUEST_CAMERA_PERMISSION = 101;
private CaptureManager capture;
private DecoratedBarcodeView barcodeScannerView;
private SurfaceView surfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = findViewById(R.id.surfaceView);
barcodeScannerView = findViewById(R.id.barcodeScannerView);
// Check camera permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
} else {
startCamera();
}
}
private void startCamera() {
barcodeScannerView.getBarcodeView().setDecoderFactory(new DefaultDecoderFactory(getBarcodeFormats()));
capture = new CaptureManager(this, barcodeScannerView);
capture.initializeFromIntent(getIntent(), savedInstanceState);
capture.decode();
}
private List<BarcodeFormat> getBarcodeFormats() {
List<BarcodeFormat> formats = new ArrayList<>();
formats.add(BarcodeFormat.QR_CODE);
return formats;
}
@Override
protected void onResume() {
super.onResume();
if (capture != null) {
capture.onResume();
}
}
@Override
protected void onPause() {
super.onPause();
if (capture != null) {
capture.onPause();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (capture != null) {
capture.onDestroy();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (capture != null) {
capture.onSaveInstanceState(outState);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startCamera();
} else {
Toast.makeText(this, "Camera permission required", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (capture != null) {
capture.onSurfaceCreated(holder);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (capture != null) {
capture.onSurfaceChanged(holder, format, width, height);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (capture != null) {
capture.onSurfaceDestroyed(holder);
}
}
}
```
这里使用了ZXing官方提供的CaptureManager和DecoratedBarcodeView来处理扫描逻辑和预览界面。在onCreate方法中,首先检查相机权限,然后初始化扫描器并启动扫描。在onResume、onPause、onDestroy、onSaveInstanceState等方法中,分别处理扫描器的生命周期。
最后,在AndroidManifest.xml文件中添加相机权限:
```xml
<uses-permission android:name="android.permission.CAMERA" />
```
这样,就可以实现基本的扫码功能了。当用户扫描了一张二维码后,可以在onActivityResult方法中获取扫描结果:
```java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_SCAN) {
String result = data.getStringExtra(Intents.Scan.RESULT);
Log.d(TAG, "Scan result: " + result);
}
}
```
完整的示例代码可以参考这个GitHub项目:https://github.com/journeyapps/zxing-android-embedded
阅读全文