session-android/src/org/thoughtcrime/securesms/qr/QrCode.java
2019-09-10 16:12:16 +10:00

43 lines
1.2 KiB
Java

package org.thoughtcrime.securesms.qr;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.annotation.NonNull;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.thoughtcrime.securesms.logging.Log;
public class QrCode {
public static final String TAG = QrCode.class.getSimpleName();
public static @NonNull Bitmap create(String data) {
return create(data, 1024);
}
public static @NonNull Bitmap create(String data, int size) {
try {
BitMatrix result = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, size, size);
Bitmap bitmap = Bitmap.createBitmap(result.getWidth(), result.getHeight(), Bitmap.Config.ARGB_8888);
for (int y = 0; y < result.getHeight(); y++) {
for (int x = 0; x < result.getWidth(); x++) {
if (result.get(x, y)) {
bitmap.setPixel(x, y, Color.BLACK);
}
}
}
return bitmap;
} catch (WriterException e) {
Log.w(TAG, e);
return Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
}
}
}