如何在 Android 中使用 CAMView 库读取二维码?
CAMView Library 是一种用于访问用户设备摄像头的简单解决方案。通过使用这个库,我们可以访问用户的相机,并使用内置的 ZXing 解码引擎来执行相机的许多功能,例如扫描条形码。该库包含一组准备好放入您的布局的组件,以便即时访问如此多的功能。该库处理低级例程,例如相机、配置、流媒体、方向等等。我们只需要按照一些基本步骤来使用这个库。
重要方法
Methods | Description |
---|---|
onScannerStarted() | This method can be used when the scanner is started. |
onScannerStopped() | This method is called when the scanner is stopped. |
onScannerError() | This method is used when the scanner gives some error. |
onCodeScanned() | This method is called when the scanner scans the data from the camera. |
CAMView 库的实现
使用这个库,我们将创建一个简单的二维码扫描应用程序,它将扫描二维码中的数据并将其显示在 TextView 中。下面给出了一个示例 GIF,以了解我们将在本文中做什么。请注意,我们将使用Java语言来实现这个项目。
分步实施
第 1 步:创建一个新项目
要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Java作为编程语言。
第 2 步:在 build.gradle(Module:app) 中添加依赖项
导航到Gradle Scripts > build.gradle(Module:app)并在依赖项部分添加以下依赖项。
implementation (‘eu.livotov.labs.android:CAMView:2.0.1@aar’) {transitive=true}
现在同步选项将出现在右上角单击立即同步选项。
第三步:修改字符串.xml文件
下面是字符串.xml文件的代码。
XML
GFG APP
Toggle Flash
Settings
Scanned Data
XML
XML
Java
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import eu.livotov.labs.android.camview.ScannerLiveView;
import eu.livotov.labs.android.camview.scanner.decoder.zxing.ZXDecoder;
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.VIBRATE;
public class MainActivity extends AppCompatActivity {
private ScannerLiveView camera;
private TextView scannedTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// check permission method is to check that the
// camera permission is granted by user or not.
// request permission method is to request the
// camera permission if not given.
if (checkPermission()) {
// if permission is already granted display a toast message
Toast.makeText(this, "Permission Granted..", Toast.LENGTH_SHORT).show();
} else {
requestPermission();
}
// initialize scannerLiveview and textview.
scannedTV = findViewById(R.id.idTVscanned);
camera = (ScannerLiveView) findViewById(R.id.camview);
camera.setScannerViewEventListener(new ScannerLiveView.ScannerViewEventListener() {
@Override
public void onScannerStarted(ScannerLiveView scanner) {
// method is called when scanner is started
Toast.makeText(MainActivity.this, "Scanner Started", Toast.LENGTH_SHORT).show();
}
@Override
public void onScannerStopped(ScannerLiveView scanner) {
// method is called when scanner is stoped.
Toast.makeText(MainActivity.this, "Scanner Stopped", Toast.LENGTH_SHORT).show();
}
@Override
public void onScannerError(Throwable err) {
// method is called when scanner gives some error.
Toast.makeText(MainActivity.this, "Scanner Error: " + err.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onCodeScanned(String data) {
// method is called when camera scans the
// qr code and the data from qr code is
// stored in data in string format.
scannedTV.setText(data);
}
});
}
@Override
protected void onResume() {
super.onResume();
ZXDecoder decoder = new ZXDecoder();
// 0.5 is the area where we have
// to place red marker for scanning.
decoder.setScanAreaPercent(0.8);
// below method will set secoder to camera.
camera.setDecoder(decoder);
camera.startScanner();
}
@Override
protected void onPause() {
// on app pause the
// camera will stop scanning.
camera.stopScanner();
super.onPause();
}
private boolean checkPermission() {
// here we are checking two permission that is vibrate
// and camera which is granted by user and not.
// if permission is granted then we are returning
// true otherwise false.
int camera_permission = ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA);
int vibrate_permission = ContextCompat.checkSelfPermission(getApplicationContext(), VIBRATE);
return camera_permission == PackageManager.PERMISSION_GRANTED && vibrate_permission == PackageManager.PERMISSION_GRANTED;
}
private void requestPermission() {
// this method is to request
// the runtime permission.
int PERMISSION_REQUEST_CODE = 200;
ActivityCompat.requestPermissions(this, new String[]{CAMERA, VIBRATE}, PERMISSION_REQUEST_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
// this method is called when user
// allows the permission to use camera.
if (grantResults.length > 0) {
boolean cameraaccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean vibrateaccepted = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (cameraaccepted && vibrateaccepted) {
Toast.makeText(this, "Permission granted..", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission Denined \n You cannot use app without providing permission", Toast.LENGTH_SHORT).show();
}
}
}
}
第 4 步:在清单文件中为相机添加权限
我们正在清单文件中添加摄像头和振动权限。导航到应用程序 > 清单以找到AndroidManifest.xml 。下面是 AndroidManifest.xml 的代码片段
XML
步骤 5:使用 activity_main.xml 文件
导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码。
XML
第 6 步:使用 MainActivity。 Java文件
导航到应用程序 > Java > 您的应用程序包名称 > MainActivity。 Java文件。下面是MainActivity 的代码。 Java文件。代码中添加了注释以更详细地理解代码。
Java
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import eu.livotov.labs.android.camview.ScannerLiveView;
import eu.livotov.labs.android.camview.scanner.decoder.zxing.ZXDecoder;
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.VIBRATE;
public class MainActivity extends AppCompatActivity {
private ScannerLiveView camera;
private TextView scannedTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// check permission method is to check that the
// camera permission is granted by user or not.
// request permission method is to request the
// camera permission if not given.
if (checkPermission()) {
// if permission is already granted display a toast message
Toast.makeText(this, "Permission Granted..", Toast.LENGTH_SHORT).show();
} else {
requestPermission();
}
// initialize scannerLiveview and textview.
scannedTV = findViewById(R.id.idTVscanned);
camera = (ScannerLiveView) findViewById(R.id.camview);
camera.setScannerViewEventListener(new ScannerLiveView.ScannerViewEventListener() {
@Override
public void onScannerStarted(ScannerLiveView scanner) {
// method is called when scanner is started
Toast.makeText(MainActivity.this, "Scanner Started", Toast.LENGTH_SHORT).show();
}
@Override
public void onScannerStopped(ScannerLiveView scanner) {
// method is called when scanner is stoped.
Toast.makeText(MainActivity.this, "Scanner Stopped", Toast.LENGTH_SHORT).show();
}
@Override
public void onScannerError(Throwable err) {
// method is called when scanner gives some error.
Toast.makeText(MainActivity.this, "Scanner Error: " + err.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onCodeScanned(String data) {
// method is called when camera scans the
// qr code and the data from qr code is
// stored in data in string format.
scannedTV.setText(data);
}
});
}
@Override
protected void onResume() {
super.onResume();
ZXDecoder decoder = new ZXDecoder();
// 0.5 is the area where we have
// to place red marker for scanning.
decoder.setScanAreaPercent(0.8);
// below method will set secoder to camera.
camera.setDecoder(decoder);
camera.startScanner();
}
@Override
protected void onPause() {
// on app pause the
// camera will stop scanning.
camera.stopScanner();
super.onPause();
}
private boolean checkPermission() {
// here we are checking two permission that is vibrate
// and camera which is granted by user and not.
// if permission is granted then we are returning
// true otherwise false.
int camera_permission = ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA);
int vibrate_permission = ContextCompat.checkSelfPermission(getApplicationContext(), VIBRATE);
return camera_permission == PackageManager.PERMISSION_GRANTED && vibrate_permission == PackageManager.PERMISSION_GRANTED;
}
private void requestPermission() {
// this method is to request
// the runtime permission.
int PERMISSION_REQUEST_CODE = 200;
ActivityCompat.requestPermissions(this, new String[]{CAMERA, VIBRATE}, PERMISSION_REQUEST_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
// this method is called when user
// allows the permission to use camera.
if (grantResults.length > 0) {
boolean cameraaccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean vibrateaccepted = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (cameraaccepted && vibrateaccepted) {
Toast.makeText(this, "Permission granted..", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission Denined \n You cannot use app without providing permission", Toast.LENGTH_SHORT).show();
}
}
}
}
输出:在真实设备上运行
现在在真实设备上运行您的应用程序并扫描一些示例二维码,您将开始测试该应用程序。确保在应用程序启动时允许相机权限。
此项目的 GitHub 链接: https://github.com/ChaitanyaMunje/QR_Code_Scanner