Skip to content

Instantly share code, notes, and snippets.

@JakeSteam
Created April 6, 2017 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JakeSteam/3af6abb136f56763b4528746e3fcf4f2 to your computer and use it in GitHub Desktop.
Save JakeSteam/3af6abb136f56763b4528746e3fcf4f2 to your computer and use it in GitHub Desktop.
"Exporting Levels Into QR Codes Using ZXing" for GameDevAlgorithms.com
private void populateCard() {
...
StorageHelper.fillWithQrDrawable((ImageView) findViewById(R.id.puzzleQrCode), exportedText);
}
public static String getPuzzleString(Puzzle puzzle) {
PuzzleCustom puzzleCustom = puzzle.getCustomData();
StringBuilder sb = new StringBuilder();
sb.append(puzzleCustom.getName()).append(sectionDelimiter)
.append(puzzleCustom.getDescription()).append(sectionDelimiter)
.append(puzzleCustom.getAuthor()).append(sectionDelimiter)
.append(puzzle.getParMoves()).append(sectionDelimiter)
.append(puzzle.getParTime()).append(sectionDelimiter);
List<Tile> tiles = puzzle.getTiles();
for (Tile tile : tiles) {
sb.append(tile.getTileTypeId()).append(tileElementDelimiter)
.append(tile.getX()).append(tileElementDelimiter)
.append(tile.getY()).append(tileElementDelimiter)
.append(tile.getRotation()).append(tileDelimiter);
}
return sb.toString();
}
public final static int WHITE = 0xFFFFFFFF;
public final static int BLACK = 0xFF000000;
public final static int WIDTH = 400;
public final static int HEIGHT = 400;
private static final int screenshotSize = 500;
public static void fillWithQrDrawable(ImageView imageView, String text) {
try {
Bitmap bitmap = generateQrCode(text);
imageView.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}
private static Bitmap generateQrCode(String str) throws WriterException {
BitMatrix result;
try {
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.MARGIN, 0);
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
result = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
} catch (IllegalArgumentException iae) {
// Unsupported format
return null;
}
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment