Created
April 6, 2017 18:46
-
-
Save JakeSteam/3af6abb136f56763b4528746e3fcf4f2 to your computer and use it in GitHub Desktop.
"Exporting Levels Into QR Codes Using ZXing" for GameDevAlgorithms.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void populateCard() { | |
... | |
StorageHelper.fillWithQrDrawable((ImageView) findViewById(R.id.puzzleQrCode), exportedText); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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