Use Mode.ALPHANUMERIC for QR codes to save space

This commit is contained in:
Dominik Schürmann 2015-05-19 18:27:04 +02:00
parent b62ad4d79c
commit 79b7cec94a
4 changed files with 24 additions and 9 deletions

View File

@ -85,11 +85,12 @@ public class QrCodeViewActivity extends BaseActivity {
ActivityCompat.finishAfterTransition(QrCodeViewActivity.this); ActivityCompat.finishAfterTransition(QrCodeViewActivity.this);
} }
String fingerprint = KeyFormattingUtils.convertFingerprintToHex(blob); Uri uri = new Uri.Builder()
String qrCodeContent = Constants.FINGERPRINT_SCHEME + ":" + fingerprint; .scheme(Constants.FINGERPRINT_SCHEME)
.opaquePart(KeyFormattingUtils.convertFingerprintToHex(blob))
.build();
// create a minimal size qr code, we can keep this in ram no problem // create a minimal size qr code, we can keep this in ram no problem
final Bitmap qrCode = QrCodeUtils.getQRCodeBitmap(qrCodeContent, 0); final Bitmap qrCode = QrCodeUtils.getQRCodeBitmap(uri, 0);
mQrCode.getViewTreeObserver().addOnGlobalLayoutListener( mQrCode.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() { new OnGlobalLayoutListener() {

View File

@ -731,9 +731,12 @@ public class ViewKeyActivity extends BaseNfcActivity implements
AsyncTask<Void, Void, Bitmap> loadTask = AsyncTask<Void, Void, Bitmap> loadTask =
new AsyncTask<Void, Void, Bitmap>() { new AsyncTask<Void, Void, Bitmap>() {
protected Bitmap doInBackground(Void... unused) { protected Bitmap doInBackground(Void... unused) {
String qrCodeContent = Constants.FINGERPRINT_SCHEME + ":" + fingerprint; Uri uri = new Uri.Builder()
.scheme(Constants.FINGERPRINT_SCHEME)
.opaquePart(fingerprint)
.build();
// render with minimal size // render with minimal size
return QrCodeUtils.getQRCodeBitmap(qrCodeContent, 0); return QrCodeUtils.getQRCodeBitmap(uri, 0);
} }
protected void onPostExecute(Bitmap qrCode) { protected void onPostExecute(Bitmap qrCode) {

View File

@ -386,9 +386,12 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
AsyncTask<Void, Void, Bitmap> loadTask = AsyncTask<Void, Void, Bitmap> loadTask =
new AsyncTask<Void, Void, Bitmap>() { new AsyncTask<Void, Void, Bitmap>() {
protected Bitmap doInBackground(Void... unused) { protected Bitmap doInBackground(Void... unused) {
String qrCodeContent = Constants.FINGERPRINT_SCHEME + ":" + fingerprint; Uri uri = new Uri.Builder()
.scheme(Constants.FINGERPRINT_SCHEME)
.opaquePart(fingerprint)
.build();
// render with minimal size // render with minimal size
return QrCodeUtils.getQRCodeBitmap(qrCodeContent, 0); return QrCodeUtils.getQRCodeBitmap(uri, 0);
} }
protected void onPostExecute(Bitmap qrCode) { protected void onPostExecute(Bitmap qrCode) {

View File

@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.ui.util;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Color; import android.graphics.Color;
import android.net.Uri;
import com.google.zxing.BarcodeFormat; import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType; import com.google.zxing.EncodeHintType;
@ -33,17 +34,24 @@ import org.sufficientlysecure.keychain.KeychainApplication;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Locale;
/** /**
* Copied from Bitcoin Wallet * Copied from Bitcoin Wallet
*/ */
public class QrCodeUtils { public class QrCodeUtils {
public static Bitmap getQRCodeBitmap(final Uri uri, final int size) {
// for URIs we want alphanumeric encoding to save space, thus make everything upper case!
// zxing will then select Mode.ALPHANUMERIC internally
return getQRCodeBitmap(uri.toString().toUpperCase(Locale.ENGLISH), size);
}
/** /**
* Generate Bitmap with QR Code based on input. * Generate Bitmap with QR Code based on input.
* @return QR Code as Bitmap * @return QR Code as Bitmap
*/ */
public static Bitmap getQRCodeBitmap(final String input, final int size) { private static Bitmap getQRCodeBitmap(final String input, final int size) {
try { try {