本来是使用默认扫描的界面,虽然也能识别但界面过于简陋,自己就自定义优化一下,代码如下。
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.CamerScan">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="18sp"
android:background="@color/colorPrimary"
app:title="扫描链接" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/viewfinder_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:corner_color="@color/corner_color"
app:frame_color="@color/viewfinder_frame"
app:label_text="请扫描网站上的二维码"
app:label_text_color="@color/colorAccent"
app:laser_color="@color/laser_color"
app:mask_color="@color/viewfinder_mask"
app:result_color="@color/result_view"
app:result_point_color="@color/result_point_color" />
<ImageButton
android:id="@+id/btn_flash"
android:layout_width="40dip"
android:layout_height="40dip"
android:padding="6dip"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="30dip"
android:background="?attr/selectableItemBackground"
android:scaleType="centerInside"
android:visibility="gone"
android:src="@drawable/flash_off" />
</FrameLayout>
</LinearLayout>
java
package com.ccf.feige.vmqapk.activity;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
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.ccf.feige.vmqapk.R;
import com.google.zxing.Result;
import com.google.zxing.ResultPoint;
import com.journeyapps.barcodescanner.BarcodeCallback;
import com.journeyapps.barcodescanner.BarcodeResult;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import java.util.List;
public class CamerScan extends AppCompatActivity {
private static final int CAMERA_PERMISSION_REQUEST_CODE = 101;
private DecoratedBarcodeView barcodeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camer_scan);
barcodeView = findViewById(R.id.viewfinder_content);
if (checkCameraPermission()) {
setupBarcodeScanner();
} else {
requestCameraPermission();
}
}
private boolean checkCameraPermission() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
}
private void requestCameraPermission() {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
}
private void setupBarcodeScanner() {
barcodeView.decodeContinuous(new BarcodeCallback() {
@Override
public void barcodeResult(BarcodeResult result) {
handleResult(result.getResult());
}
@Override
public void possibleResultPoints(List<ResultPoint> resultPoints) {
// Handle potential result points if needed
}
});
}
private void handleResult(Result result) {
Log.d("CamerScan", "QR Code Content: " + result.getText());
Toast.makeText(this, "QR Code: " + result.getText(), Toast.LENGTH_LONG).show();
// Additional handling of QR code content
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
setupBarcodeScanner();
} else {
Toast.makeText(this, "Camera permission is required to scan QR codes", Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onResume() {
super.onResume();
if (checkCameraPermission()) {
barcodeView.resume();
}
}
@Override
protected void onPause() {
super.onPause();
barcodeView.pause();
}
}
如何调用
java
Intent intent=new Intent(MainActivity.this, CamerScan.class);
startActivity(intent);