Merge branch '346-remove-base32-todos' into 'master'

Remove base32 TODOs, fix a potential NPE

I thought we'd be able to get higher data density in QR codes by using base32 instead of base64, allowing the QR code to use alphanumeric mode instead of byte mode. But I tried it, and although the QR code does use alphanumeric mode, it comes out at exactly the same size (see the 346-use-base32-for-qr-codes branch). So this MR removes the TODOs and fixes a potential NPE I spotted while working on the other branch.

See merge request !327
This commit is contained in:
akwizgran
2016-09-27 14:19:45 +00:00
2 changed files with 27 additions and 24 deletions

View File

@@ -2,16 +2,20 @@ package org.briarproject.android.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.DisplayMetrics;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.jetbrains.annotations.Nullable;
import java.util.logging.Logger;
import static android.graphics.Bitmap.Config.ARGB_8888;
import static android.graphics.Color.BLACK;
import static android.graphics.Color.WHITE;
import static com.google.zxing.BarcodeFormat.QR_CODE;
import static java.util.logging.Level.WARNING;
public class QrCodeUtils {
@@ -19,6 +23,7 @@ public class QrCodeUtils {
private static final Logger LOG =
Logger.getLogger(QrCodeUtils.class.getName());
@Nullable
public static Bitmap createQrCode(Context context, String input) {
// Get narrowest screen dimension
DisplayMetrics dm = context.getResources().getDisplayMetrics();
@@ -26,24 +31,21 @@ public class QrCodeUtils {
try {
// Generate QR code
final BitMatrix encoded = new QRCodeWriter().encode(
input, BarcodeFormat.QR_CODE, smallestDimen, smallestDimen);
input, QR_CODE, smallestDimen, smallestDimen);
// Convert QR code to Bitmap
int width = encoded.getWidth();
int height = encoded.getHeight();
int[] pixels = new int[width * height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
pixels[y * width + x] =
encoded.get(x, y) ? Color.BLACK : Color.WHITE;
pixels[y * width + x] = encoded.get(x, y) ? BLACK : WHITE;
}
}
Bitmap qr = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
Bitmap qr = Bitmap.createBitmap(width, height, ARGB_8888);
qr.setPixels(pixels, 0, width, 0, 0, width, height);
return qr;
} catch (WriterException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
return null;
}
}