ConnectOut-sdp/sdp2023

View on GitHub
app/src/main/java/com/sdpteam/connectout/QrCode/QRcodeGenerator.java

Summary

Maintainability
A
25 mins
Test Coverage
B
80%
package com.sdpteam.connectout.QrCode;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import android.graphics.Bitmap;
import android.graphics.Color;

public class QRcodeGenerator {

    private static final int QR_CODE_SIZE = 512;

    /**
     * Generates QR codes from given data.
     *
     * @param data (String): data to parse into the QR code
     * @return (Bitmap): map coloration of QR code
     * @throws (IllegalArgumentException): if failed to encode the data.
     */
    public static Bitmap generateQRCode(String data) throws IllegalArgumentException {
        if (data == null || data.isEmpty()) {
            throw new IllegalArgumentException("Data cannot be null or empty");
        }

        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix bitMatrix = null;
        try {
            bitMatrix = writer.encode(data, BarcodeFormat.QR_CODE, QR_CODE_SIZE, QR_CODE_SIZE);
        } catch (WriterException e) {
            e.printStackTrace();
            return null;
        }

        Bitmap bitmap = Bitmap.createBitmap(bitMatrix.getWidth(), bitMatrix.getHeight(), Bitmap.Config.RGB_565);
        for (int x = 0; x < bitMatrix.getWidth(); x++) {
            for (int y = 0; y < bitMatrix.getHeight(); y++) {
                bitmap.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
            }
        }

        return bitmap;
    }
}