Skip to content

Instantly share code, notes, and snippets.

@yunusemredilber
Created April 21, 2020 11:43
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 yunusemredilber/b143ef9ffbbcd727e7019dab3a071236 to your computer and use it in GitHub Desktop.
Save yunusemredilber/b143ef9ffbbcd727e7019dab3a071236 to your computer and use it in GitHub Desktop.
Android QR Reading
public class QrActivity extends AppCompatActivity implements QRCodeReaderView.OnQRCodeReadListener {
private QRCodeReaderView qrCodeReaderView;
private static final int MY_CAMERA_REQUEST_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr);
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_REQUEST_CODE);
}
qrCodeReaderView = findViewById(R.id.view_qrdecoder);
qrCodeReaderView.setOnQRCodeReadListener(this);
// Use this function to enable/disable decoding
qrCodeReaderView.setQRDecodingEnabled(true);
// Use this function to change the autofocus interval (default is 5 secs)
qrCodeReaderView.setAutofocusInterval(2000L);
// Use this function to enable/disable Torch
qrCodeReaderView.setTorchEnabled(true);
// Use this function to set back camera preview
qrCodeReaderView.setBackCamera();
}
// Called when a QR is decoded
// "text" : the text encoded in QR
// "points" : points where QR control points are placed in View
@Override
public void onQRCodeRead(String text, PointF[] points) {
System.out.println("*** -> " + text);
}
@Override
protected void onResume() {
super.onResume();
qrCodeReaderView.startCamera();
}
@Override
protected void onPause() {
super.onPause();
qrCodeReaderView.stopCamera();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
}
}
// https://github.com/dlazaro66/QRCodeReaderView
/*
<com.dlazaro66.qrcodereaderview.QRCodeReaderView
android:id="@+id/view_qrdecoder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment