"Android: Importing Levels From QR Codes (Camera / File)" 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
| compile 'com.github.aitorvs:allowme:0.3.2' | |
| compile 'com.google.android.gms:play-services-vision:10.0.1' |
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 void importFromCamera(View v) { | |
| try { | |
| Intent intent = new Intent("com.google.zxing.client.android.SCAN"); | |
| intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); | |
| startActivityForResult(intent, INTENT_CAMERA); | |
| } catch (Exception e) { | |
| Toast.makeText(this, "Scanning QR codes requires a barcode reader to be installed!", Toast.LENGTH_SHORT).show(); | |
| Uri marketUri = Uri.parse("market://details?id=com.google.zxing.client.android"); | |
| Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri); | |
| startActivity(marketIntent); | |
| } | |
| } | |
| public void importFromFile(View v) { | |
| PermissionHelper.runIfPossible(Manifest.permission.READ_EXTERNAL_STORAGE, new Runnable() { | |
| @Override | |
| public void run() { | |
| importFromFile(); | |
| } | |
| }); | |
| } | |
| private void importFromFile() { | |
| Intent intent = new Intent(); | |
| intent.setType("image/*"); | |
| intent.setAction(Intent.ACTION_GET_CONTENT); | |
| startActivityForResult(Intent.createChooser(intent, "Select Picture"), INTENT_FILE); | |
| } | |
| @Override | |
| public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
| super.onActivityResult(requestCode, resultCode, data); | |
| if (resultCode == RESULT_OK) { | |
| String puzzleString = ""; | |
| if (requestCode == INTENT_CAMERA) { | |
| puzzleString = data.getStringExtra("SCAN_RESULT"); | |
| } else if (requestCode == INTENT_FILE) { | |
| try { | |
| Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData()); | |
| puzzleString = StorageHelper.readQRImage(this, bitmap); | |
| } catch (Exception e) { | |
| AlertHelper.error(this, AlertHelper.getError(AlertHelper.Error.FILE_IMPORT_FAIL)); | |
| } | |
| } activity | |
| if (!puzzleString.equals("") && PuzzleShareHelper.importPuzzleString(puzzleString, false)) { | |
| GooglePlayHelper.UpdateEvent(Constants.EVENT_IMPORT_PUZZLE, 1); | |
| AlertHelper.success(this, Text.get("ALERT_PUZZLE_IMPORTED")); | |
| populatePuzzles(); | |
| } else if (requestCode == INTENT_CAMERA) { | |
| AlertHelper.error(this, AlertHelper.getError(AlertHelper.Error.CAMERA_IMPORT_FAIL)); | |
| } else if (requestCode == INTENT_FILE) { | |
| AlertHelper.error(this, AlertHelper.getError(AlertHelper.Error.FILE_IMPORT_FAIL)); | |
| } | |
| } | |
| } |
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
| import com.aitorvs.android.allowme.AllowMe; | |
| import com.aitorvs.android.allowme.AllowMeCallback; | |
| import com.aitorvs.android.allowme.PermissionResultSet; | |
| public class PermissionHelper { | |
| public static void runIfPossible(final String permission, final Runnable callback) { | |
| if (!AllowMe.isPermissionGranted(permission)) { | |
| new AllowMe.Builder() | |
| .setPermissions(permission) | |
| .setCallback(new AllowMeCallback() { | |
| @Override | |
| public void onPermissionResult(int requestCode, PermissionResultSet result) { | |
| if (result.isGranted(permission)) { | |
| callback.run(); | |
| } | |
| } | |
| }) | |
| .request(123); | |
| } else { | |
| callback.run(); | |
| } | |
| } | |
| } |
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 static String readQRImage(Activity activity, Bitmap bitmap) { | |
| String contents = ""; | |
| BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(activity) | |
| .setBarcodeFormats(Barcode.QR_CODE) | |
| .build(); | |
| SparseArray<Barcode> detectedBarcodes = barcodeDetector.detect(new Frame.Builder() | |
| .setBitmap(bitmap) | |
| .build()); | |
| if (detectedBarcodes.size() > 0 && detectedBarcodes.valueAt(0) != null) { | |
| contents = detectedBarcodes.valueAt(0).rawValue; | |
| } | |
| return contents; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment