赞
踩
首先介绍一下,我做的这个比较简单,是参照其他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" />
三、布局
- <?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"
- android:gravity="center"
- tools:context=".MainActivity">
-
-
- <ImageView
- android:layout_width="200dp"
- android:layout_height="200dp"
- android:id="@+id/qr"/>
- <EditText
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/creat"
- android:hint="要生成二维码的文字"
- android:layout_marginTop="20dp"
- android:textSize="20dp"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="生成"
- android:id="@+id/sc"
- android:layout_marginTop="20dp"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="扫描"
- android:id="@+id/smiao"
- android:layout_marginTop="20dp"
- />
-
- </LinearLayout>

四、逻辑代码
- package com.example.shaomiao;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.os.Bundle;
- import android.renderscript.Matrix2f;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
-
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.MultiFormatWriter;
- import com.google.zxing.WriterException;
- import com.google.zxing.common.BitMatrix;
- import com.journeyapps.barcodescanner.BarcodeEncoder;
-
- public class MainActivity extends AppCompatActivity {
- private ImageView qr;
- private EditText creat;
- private Button sc;
- private Button smiao;
-
-
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- qr = (ImageView) findViewById(R.id.qr);
- creat = (EditText) findViewById(R.id.creat);
- sc = (Button) findViewById(R.id.sc);
- smiao = (Button) findViewById(R.id.smiao);
- sc.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- gener(creat.getText().toString());//获取输入内容
- }
- });
- smiao.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent=new Intent(MainActivity.this, MainActivity2.class);
- startActivity(intent);
- }
- });
- }
-
- private void gener(String text) {
- MultiFormatWriter multiFormatWriter=new MultiFormatWriter();
- try{
- BitMatrix bitMatrix=multiFormatWriter.encode(text, BarcodeFormat.QR_CODE,200,200);
- BarcodeEncoder barcodeEncoder=new BarcodeEncoder();
- Bitmap bitmap=barcodeEncoder.createBitmap(bitMatrix);
- qr.setImageBitmap(bitmap);
- } catch (WriterException e) {
- e.printStackTrace();
- }
- }
- }

五、创建一个空活动,在逻辑代码中
- package com.example.shaomiao;
-
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.Toast;
-
- import com.google.zxing.integration.android.IntentIntegrator;
- import com.google.zxing.integration.android.IntentResult;
-
- public class MainActivity2 extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main2);
-
- IntentIntegrator intentIntegrator=new IntentIntegrator(this);
- intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE);
- intentIntegrator.setPrompt("Scan a QR Code");
- intentIntegrator.setCameraId(0);
- intentIntegrator.setBeepEnabled(false);
- intentIntegrator.setOrientationLocked(true);
- intentIntegrator.initiateScan();
- }
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- IntentResult result=IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
- if(result !=null && result.getContents() !=null){
- String sca=result.getContents();
- Toast.makeText(this, "扫描结果"+sca, Toast.LENGTH_SHORT).show();
- }
- }
- }

我们生活中的二维码,就微信来说,它是点击二维码名片,生成二维码的,里面就是自己布局一下。扫码是点击扫一扫,出现扫码框,然后布局一下。但扫一扫功能强大,可以识别的多,列如:图片、支付码,返回的是一个动态活动,而我们这个返回的是静态。这个大家可以一起探讨一下。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。