use utf-8 in barcodes

This commit is contained in:
Daniel Gultsch 2018-03-19 21:05:20 +01:00
parent 0f97b44e9f
commit b682aeb252
1 changed files with 155 additions and 154 deletions

View File

@ -42,6 +42,36 @@ public class BarcodeProvider extends ContentProvider implements ServiceConnectio
private XmppConnectionService mXmppConnectionService; private XmppConnectionService mXmppConnectionService;
private boolean mBindingInProcess = false; private boolean mBindingInProcess = false;
public static Uri getUriForAccount(Context context, Account account) {
final String packageId = context.getPackageName();
return Uri.parse("content://" + packageId + AUTHORITY + "/" + account.getJid().asBareJid() + ".png");
}
public static Bitmap create2dBarcodeBitmap(String input, int size) {
try {
final QRCodeWriter barcodeWriter = new QRCodeWriter();
final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
final BitMatrix result = barcodeWriter.encode(input, BarcodeFormat.QR_CODE, size, size, hints);
final int width = result.getWidth();
final int height = result.getHeight();
final int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
final int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;
}
}
final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (final Exception e) {
e.printStackTrace();
return null;
}
}
@Override @Override
public boolean onCreate() { public boolean onCreate() {
File barcodeDirectory = new File(getContext().getCacheDir().getAbsolutePath() + "/barcodes/"); File barcodeDirectory = new File(getContext().getCacheDir().getAbsolutePath() + "/barcodes/");
@ -113,7 +143,7 @@ public class BarcodeProvider extends ContentProvider implements ServiceConnectio
outputStream.close(); outputStream.close();
outputStream.flush(); outputStream.flush();
} }
return ParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY); return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
} }
} catch (Exception e) { } catch (Exception e) {
throw new FileNotFoundException(); throw new FileNotFoundException();
@ -130,7 +160,7 @@ public class BarcodeProvider extends ContentProvider implements ServiceConnectio
if (context != null) { if (context != null) {
synchronized (this) { synchronized (this) {
if (mXmppConnectionService == null && !mBindingInProcess) { if (mXmppConnectionService == null && !mBindingInProcess) {
Log.d(Config.LOGTAG,"calling to bind service"); Log.d(Config.LOGTAG, "calling to bind service");
context.startService(intent); context.startService(intent);
context.bindService(intent, this, Context.BIND_AUTO_CREATE); context.bindService(intent, this, Context.BIND_AUTO_CREATE);
this.mBindingInProcess = true; this.mBindingInProcess = true;
@ -173,36 +203,7 @@ public class BarcodeProvider extends ContentProvider implements ServiceConnectio
lock.wait(); lock.wait();
} }
} else { } else {
Log.d(Config.LOGTAG,"not waiting for service because already initialized"); Log.d(Config.LOGTAG, "not waiting for service because already initialized");
}
}
public static Uri getUriForAccount(Context context, Account account) {
final String packageId = context.getPackageName();
return Uri.parse("content://" + packageId + AUTHORITY + "/" + account.getJid().asBareJid() + ".png");
}
public static Bitmap create2dBarcodeBitmap(String input, int size) {
try {
final QRCodeWriter barcodeWriter = new QRCodeWriter();
final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
final BitMatrix result = barcodeWriter.encode(input, BarcodeFormat.QR_CODE, size, size, hints);
final int width = result.getWidth();
final int height = result.getHeight();
final int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
final int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;
}
}
final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (final Exception e) {
e.printStackTrace();
return null;
} }
} }
} }