[Android] Realize the function of generating QR code, barcode and scanning QR code

Table of contents

1. Add dependencies

2. Layout file

3. Realize the function of generating QR code

Four, renderings


1. Add dependencies

First add a third-party library to realize the generation of QR codes and the function of scanning QR codes. The open source library is as follows:

Add dependencies in build.grade(Moudle):

implementation 'com.journeyapps:zxing-android-embedded:3.4.0'

2. Layout file

The effect is as follows:

activity_main.xml:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="40dp"
        android:gravity="center">
        <EditText
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="输入文字"/>
        <Button
            android:id="@+id/generate"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="点击生成"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/text"/>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/image"
            android:layout_below="@+id/generate"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"/>

    </RelativeLayout>

3. Realize the function of generating QR code

MainActivity.java:

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.journeyapps.barcodescanner.BarcodeEncoder;

public class MainActivity extends AppCompatActivity {
    private Button generate;
    private TextView text;
    private ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = this.findViewById(R.id.text);
        image = this.findViewById(R.id.image);
        generate = this.findViewById(R.id.generate);

        //生成
        generate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = text.getText().toString().trim();
                MultiFormatWriter writer = new MultiFormatWriter();
                try {
                    BitMatrix matrix = writer.encode(s, BarcodeFormat.QR_CODE,350,350);
                    BarcodeEncoder encoder = new BarcodeEncoder();
                    Bitmap bitmap = encoder.createBitmap(matrix);
                    image.setImageBitmap(bitmap);

                    InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    // manager.hideSoftInputFromWindow(editText.getApplicationWindowToken(),0);
                } catch (WriterException e) {
                    e.printStackTrace();
                }

            }
        });
    }
}

Four, renderings

Simple and rude!

Thanks ლ(°◕‵ƹ′◕ლ)! ! !

Guess you like

Origin blog.csdn.net/yao_yaoya/article/details/128286338