我是靠谱客的博主 高大哈密瓜,最近开发中收集的这篇文章主要介绍Android实现扫描二维码功能,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

本文实例为大家分享了Android实现扫描二维码的具体代码,供大家参考,具体内容如下

1.效果图:

2.引用公共库:builder.radle,在app目录下

implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-vision:19.0.0'
implementation 'com.google.android.gms:auth-api-impl:11.6.0'

3.清单文件,拍照权限申请 

<uses-permission android:name="android.permission.CAMERA" />

4.主界面:

package com.example.administrator.testz;
 
 
import android.Manifest;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.os.Vibrator;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;
 
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
 
import java.io.IOException;
import java.util.ArrayList;
 
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
 
 
public class MainActivity extends AppCompatActivity {
 
 SurfaceView surfaceView;
 CameraSource cameraSource;
 TextView qrCodeText;
 BarcodeDetector barcodeDetector;
 public static ArrayList<String> scannList;
 
 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 surfaceView = (SurfaceView) findViewById(R.id.qrView);
 qrCodeText = (TextView) findViewById(R.id.tv);
 scannList = new ArrayList<>();
 barcodeDetector = new BarcodeDetector.Builder(this)
  .setBarcodeFormats(Barcode.QR_CODE)
  .build();
 cameraSource = new CameraSource.Builder(this, barcodeDetector)
  .setRequestedPreviewSize(640, 480)
  .setAutoFocusEnabled(true)
  .build();
 surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
  if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
   return;
  }
  try {
   cameraSource.start(holder);
  } catch (IOException e) {
   e.printStackTrace();
  }
  }
 
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
 
  }
 
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
  cameraSource.stop();
  }
 });
 
 
 barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
  @Override
  public void release() {
  }
 
  @Override
  public void receiveDetections(Detector.Detections<Barcode> detections) {
  final SparseArray<Barcode> qrCode = detections.getDetectedItems();
 
  if (qrCode.size() != 0) {
   qrCodeText.post(new Runnable() {
   @Override
   public void run() {
    qrCodeText.setText(qrCode.valueAt(0).displayValue);
    addInList(qrCode.valueAt(0).displayValue);
   }
   });
  }
  }
 });
 
 }
 
 private void addInList(String scannResult) {
 if (scannList.isEmpty()) {
  scannList.add(scannResult);
 } else {
  String a = scannList.get(scannList.size() - 1);
  if (!a.equals(scannResult)) {
  scannList.add(scannResult);
  }
 }
 for (String s : scannList) {
  Log.e("s", "onCreate: " + s);
 }
 Log.e("size", "addInList: " + scannList.size());
 
 }
 
 @Override
 protected void onDestroy() {
 super.onDestroy();
 
 }
}

5.布局页面:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
 tools:context="com.example.administrator.testz.MainActivity">
 
 
 <SurfaceView
 android:id="@+id/qrView"
 android:layout_width="300dp"
 android:layout_height="300dp"
 android:layout_marginStart="32dp"
 android:layout_marginLeft="32dp"
 android:layout_marginEnd="32dp"
 android:layout_marginRight="32dp"
 android:orientation="vertical" />
 
 <TextView
 android:id="@+id/tv"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:text="TextView"
 android:textSize="30sp" />
 
 
</RelativeLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是高大哈密瓜为你收集整理的Android实现扫描二维码功能的全部内容,希望文章能够帮你解决Android实现扫描二维码功能所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(107)

评论列表共有 0 条评论

立即
投稿
返回
顶部