Created
October 27, 2022 03:14
-
-
Save yatiksihag01/d3554c8c6a66ae86c645876d382a26cd to your computer and use it in GitHub Desktop.
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 class MainActivity extends AppCompatActivity { | |
private ActivityMainBinding binding; | |
ActivityResultLauncher<String> mChoosePhoto; | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
binding = ActivityMainBinding.inflate(getLayoutInflater()); | |
setContentView(binding.getRoot()); | |
binding.selectFromGallery.setOnClickListener(view -> mChoosePhoto.launch("image/*")); | |
mChoosePhoto = registerForActivityResult(new ActivityResultContracts.GetContent(), result -> { | |
InputImage image; | |
BarcodeScannerOptions options = | |
new BarcodeScannerOptions.Builder() | |
.setBarcodeFormats(Barcode.FORMAT_QR_CODE) | |
.build(); | |
BarcodeScanner scanner = BarcodeScanning.getClient(options); | |
Log.v("myUri", String.valueOf(result)); | |
try { | |
image = InputImage.fromFilePath(this, result); | |
scanner.process(image) | |
.addOnSuccessListener(new OnSuccessListener<List<Barcode>>() { | |
@Override | |
public void onSuccess(List<Barcode> barcodes) { | |
Log.v("size", String.valueOf(barcodes. Size())); | |
MainActivity.this.processResult(barcodes); | |
} | |
}) | |
.addOnFailureListener(e -> { | |
// Task failed with an exception | |
Toast.makeText(MainActivity.this, "Failed to scan.", Toast.LENGTH_SHORT).show(); | |
e.printStackTrace(); | |
}); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
}); | |
} | |
private void processResult(List<Barcode> barcodes){ | |
for (Barcode barcode: barcodes) { | |
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); | |
vibrator.vibrate(VibrationEffect.createOneShot(100, 125)); | |
// Rect bounds = barcode.getBoundingBox(); | |
// Point[] corners = barcode.getCornerPoints(); | |
// | |
String rawValue = barcode.getRawValue(); | |
int valueType = barcode.getValueType(); | |
switch (valueType) { | |
case Barcode.TYPE_WIFI: | |
String ssid = barcode.getWifi().getSsid(); | |
String password = barcode.getWifi().getPassword(); | |
int type = barcode.getWifi().getEncryptionType(); | |
Toast.makeText(this, ssid + " " + password, Toast.LENGTH_SHORT).show(); | |
break; | |
case Barcode.TYPE_URL: | |
String title = barcode.getUrl().getTitle(); | |
String url = barcode.getUrl().getUrl(); | |
Toast.makeText(this, title + " " + url, Toast.LENGTH_SHORT).show(); | |
break; | |
case Barcode.TYPE_TEXT: | |
String text = barcode.getDisplayValue(); | |
Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); | |
break; | |
default: | |
Toast.makeText(this, rawValue, Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment