Creare QR Code in Android con QRGenerator

Mattepuffo's logo
Creare QR Code in Android con QRGenerator

Creare QR Code in Android con QRGenerator

QRGenerator è una libreria per Android per la creazione di QR Code nelle nostre app.

Non so se funziona anche in Kotlin, noi comunque vedremo un esempio in Java.

Prima di tutto aggiungiamo questo al nostro gradle:

dependencies {
    ................
    implementation 'androidmads.library.qrgenearator:QRGenearator:1.0.3'
}

Poi aggiungiamo i permessi al manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Questo il nostro layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <Button
            android:id="@+id/start"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Generate" />

        <Button
            android:id="@+id/save"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Save" />
    </LinearLayout>

    <ImageView
        android:id="@+id/QR_Image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name" />

</LinearLayout>

Infine il nostro codice:

import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.zxing.WriterException;
import androidmads.library.qrgenearator.QRGContents;
import androidmads.library.qrgenearator.QRGEncoder;
import androidmads.library.qrgenearator.QRGSaver;

public class MainActivity extends AppCompatActivity {

    ImageView qrImage;
    Button start, save;
    String savePath = "";
    Bitmap bitmap;
    QRGEncoder qrgEncoder;
    String inputValue = "TESTO DEL QR CODE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        savePath = getApplicationContext().getFilesDir().getPath() + "/QRCODE/";
        qrImage = (ImageView) findViewById(R.id.QR_Image);
        start = (Button) findViewById(R.id.start);
        save = (Button) findViewById(R.id.save);

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
                Display display = manager.getDefaultDisplay();
                Point point = new Point();
                display.getSize(point);
                int width = point.x;
                int height = point.y;
                int smallerDimension = width < height ? width : height;
                smallerDimension = smallerDimension * 3 / 4;

                qrgEncoder = new QRGEncoder(inputValue, null, 
                        QRGContents.Type.TEXT, smallerDimension);
                try {
                    bitmap = qrgEncoder.encodeAsBitmap();
                    qrImage.setImageBitmap(bitmap);
                } catch (WriterException e) {
                    Log.v("ERRORE QR CODE", e.toString());
                }
            }
        });

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean save;
                String result;
                try {
                    save = QRGSaver.save(savePath, inputValue, bitmap, 
                            QRGContents.ImageType.IMAGE_JPEG);
                    result = save ? "Immagine salvata" : "Immagine non salvata";
                    Toast.makeText(getApplicationContext(), result, 
                            Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }
}

Abbiamo due tasi:

  • uno visualizza il QR Code nella schermata
  • l'altro lo salva

Ovviamente, in questo esempio, il testo salvato nel QR Code è sempre lo stesso.

Come esercizio potete agiungere una casella di testo per impostare cosa mettere nel QR Code.

Enjoy!


Condividi

Commentami!