当前位置:   article > 正文

android studio生成二维码、扫描二维码_android studio扫二维码功能

android studio扫二维码功能

首先介绍一下,我做的这个比较简单,是参照其他UP主的。大概就是,先输入文字,获取文字内容,点击生成二维码,扫描生成的二维码获取输入的文字。

一、添加第三方插件:app下的

implementation'com.google.zxing:core:3.3.3'
implementation'com.journeyapps:zxing-android-embedded:3.6.0'
implementation'com.google.zxing:javase:3.0.0'

 

二、在添加 

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera2" />
<uses-feature android:name="android.hardware.camera2.autofocus" />

 

三、布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. android:gravity="center"
  9. tools:context=".MainActivity">
  10. <ImageView
  11. android:layout_width="200dp"
  12. android:layout_height="200dp"
  13. android:id="@+id/qr"/>
  14. <EditText
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:id="@+id/creat"
  18. android:hint="要生成二维码的文字"
  19. android:layout_marginTop="20dp"
  20. android:textSize="20dp"/>
  21. <Button
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="生成"
  25. android:id="@+id/sc"
  26. android:layout_marginTop="20dp"
  27. />
  28. <Button
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:text="扫描"
  32. android:id="@+id/smiao"
  33. android:layout_marginTop="20dp"
  34. />
  35. </LinearLayout>

四、逻辑代码

  1. package com.example.shaomiao;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.graphics.Bitmap;
  5. import android.os.Bundle;
  6. import android.renderscript.Matrix2f;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.ImageView;
  11. import com.google.zxing.BarcodeFormat;
  12. import com.google.zxing.MultiFormatWriter;
  13. import com.google.zxing.WriterException;
  14. import com.google.zxing.common.BitMatrix;
  15. import com.journeyapps.barcodescanner.BarcodeEncoder;
  16. public class MainActivity extends AppCompatActivity {
  17. private ImageView qr;
  18. private EditText creat;
  19. private Button sc;
  20. private Button smiao;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. qr = (ImageView) findViewById(R.id.qr);
  26. creat = (EditText) findViewById(R.id.creat);
  27. sc = (Button) findViewById(R.id.sc);
  28. smiao = (Button) findViewById(R.id.smiao);
  29. sc.setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View v) {
  32. gener(creat.getText().toString());//获取输入内容
  33. }
  34. });
  35. smiao.setOnClickListener(new View.OnClickListener() {
  36. @Override
  37. public void onClick(View v) {
  38. Intent intent=new Intent(MainActivity.this, MainActivity2.class);
  39. startActivity(intent);
  40. }
  41. });
  42. }
  43. private void gener(String text) {
  44. MultiFormatWriter multiFormatWriter=new MultiFormatWriter();
  45. try{
  46. BitMatrix bitMatrix=multiFormatWriter.encode(text, BarcodeFormat.QR_CODE,200,200);
  47. BarcodeEncoder barcodeEncoder=new BarcodeEncoder();
  48. Bitmap bitmap=barcodeEncoder.createBitmap(bitMatrix);
  49. qr.setImageBitmap(bitmap);
  50. } catch (WriterException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. }

 五、创建一个空活动,在逻辑代码中

  1. package com.example.shaomiao;
  2. import androidx.annotation.Nullable;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.widget.Toast;
  7. import com.google.zxing.integration.android.IntentIntegrator;
  8. import com.google.zxing.integration.android.IntentResult;
  9. public class MainActivity2 extends AppCompatActivity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main2);
  14. IntentIntegrator intentIntegrator=new IntentIntegrator(this);
  15. intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE);
  16. intentIntegrator.setPrompt("Scan a QR Code");
  17. intentIntegrator.setCameraId(0);
  18. intentIntegrator.setBeepEnabled(false);
  19. intentIntegrator.setOrientationLocked(true);
  20. intentIntegrator.initiateScan();
  21. }
  22. @Override
  23. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  24. super.onActivityResult(requestCode, resultCode, data);
  25. IntentResult result=IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
  26. if(result !=null && result.getContents() !=null){
  27. String sca=result.getContents();
  28. Toast.makeText(this, "扫描结果"+sca, Toast.LENGTH_SHORT).show();
  29. }
  30. }
  31. }

我们生活中的二维码,就微信来说,它是点击二维码名片,生成二维码的,里面就是自己布局一下。扫码是点击扫一扫,出现扫码框,然后布局一下。但扫一扫功能强大,可以识别的多,列如:图片、支付码,返回的是一个动态活动,而我们这个返回的是静态。这个大家可以一起探讨一下。 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/949566
推荐阅读
相关标签
  

闽ICP备14008679号