Android中怎么实现扫描二维码功能
这篇文章给大家介绍Android中怎么实现扫描二维码功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
成都创新互联公司服务项目包括南开网站建设、南开网站制作、南开网页制作以及南开网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,南开网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到南开省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
引用公共库: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.清单文件,拍照权限申请
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 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() { @Override public void release() { } @Override public void receiveDetections(Detector.Detections detections) { final SparseArray 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.布局页面:
关于Android中怎么实现扫描二维码功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
网页标题:Android中怎么实现扫描二维码功能
网站链接:
http://dzwzjz.com/article/jisdds.html