上一篇可以生成QRcode那这篇来说说扫描QRcode
如果要扫描QRcode一定要先取得相机权限
private void getPermissionsCamera(){
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA},1);
}
}
接着是code
public class CameraActivity extends AppCompatActivity {
SurfaceView surfaceView;
TextView textView;
CameraSource cameraSource;
BarcodeDetector barcodeDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
surfaceView=findViewById(R.id.surfaceView);
textView=findViewById(R.id.textView);
barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
cameraSource=new CameraSource.Builder(this,barcodeDetector).setRequestedPreviewSize(300,300).build();
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback(){
@Override
public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
if(ActivityCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.CAMERA)
!=PackageManager.PERMISSION_GRANTED)
return;
try{
cameraSource.start(surfaceHolder);
}catch (IOException e){
e.printStackTrace();
}
}
@Override
public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
cameraSource.stop();
}
});
barcodeDetector.setProcessor(new Detector.Processor<Barcode>(){
@Override
public void release() {
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> qrCodes=detections.getDetectedItems();
if(qrCodes.size()!=0){
textView.post(new Runnable() {
@Override
public void run() {
textView.setText(qrCodes.valueAt(0).displayValue);
}
});
}
}
});
}
}
然後是xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".CameraActivity">
<TextView
android:id="@+id/textView"
android:layout_width="350dp"
android:layout_height="70dp"
android:layout_marginBottom="172dp"
android:text="网址在这"
android:textSize="30sp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent" />
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="350dp"
android:layout_height="350dp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.573" />
</androidx.constraintlayout.widget.ConstraintLayout>
成果如下
<<: Day22 高阶网路应用 GPS 和 Ender Modem
倒数几篇文章,我想回头聊聊, 如果未来的人们想要一起踏上这个 「在AI时代追求人性」之路, 应该要有...
1.前端的建立过程(上): 我是在本机电脑运行起来本地版本 Container 的静态前端 Serv...
昨天提到骇客暴力破解密码的方式,我们接着这个话题继续,最後来聊聊怎麽取一个好一些、实用一些的密码吧!...
如果家里、公司或学校电脑上的重要档案损坏或丢失,您一定会非常着急。不要担心,本文将爲您介绍一款适合任...
本系列文章同步发布於笔者网站 今天我们要来介绍最後一项 OpenStack 元件,Cinder。Ci...