Created
August 6, 2018 07:23
-
-
Save Sadwyn/9eb76b4440d6477554614f769151ba71 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 { | |
public static final int CAMERA_PERMISSION_REQUEST = 666; | |
private boolean isZXing; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
isZXing = true; | |
Button buttonZxing = findViewById(R.id.btn_start_qr_zxing); | |
buttonZxing.setOnClickListener(v -> { | |
isZXing = true; | |
checkCameraPermission(); | |
}); | |
Button buttonZbar = findViewById(R.id.btn_start_qr_zbar); | |
buttonZbar.setOnClickListener(v -> { | |
isZXing = false; | |
checkCameraPermission(); | |
}); | |
} | |
private void checkCameraPermission() { | |
if (ContextCompat.checkSelfPermission(this, | |
Manifest.permission.READ_CONTACTS) | |
!= PackageManager.PERMISSION_GRANTED) { | |
if (ActivityCompat.shouldShowRequestPermissionRationale(this, | |
Manifest.permission.CAMERA)) { | |
Toast.makeText(this, "Suck the cock! We need your fuck'n camera!", Toast.LENGTH_SHORT).show(); | |
} else { | |
ActivityCompat.requestPermissions(this, | |
new String[]{Manifest.permission.CAMERA}, | |
CAMERA_PERMISSION_REQUEST); | |
} | |
} else { | |
startQr(isZXing ? QRActivityZxing.class : QRActivityZbar.class); | |
} | |
} | |
private void startQr(Class activityClass) { | |
startActivity(new Intent(this, activityClass)); | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { | |
switch (requestCode) { | |
case CAMERA_PERMISSION_REQUEST: { | |
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
startQr(isZXing ? QRActivityZxing.class : QRActivityZbar.class); | |
} else { | |
checkCameraPermission(); | |
} | |
} | |
} | |
} | |
} |
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 QRActivityZbar extends AppCompatActivity implements ZBarScannerView.ResultHandler{ | |
private ZBarScannerView mScannerView; | |
@Override | |
public void onCreate(Bundle state) { | |
super.onCreate(state); | |
mScannerView = new ZBarScannerView(this); // Programmatically initialize the scanner view | |
setContentView(mScannerView); // Set the scanner view as the content view | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results. | |
mScannerView.startCamera(); // Start camera on resume | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
mScannerView.stopCamera(); // Stop camera on pause | |
} | |
@Override | |
public void handleResult(Result rawResult) { | |
// Do something with the result here | |
Log.v("TAG", rawResult.getContents()); // Prints scan results | |
Log.v("TAG", rawResult.getBarcodeFormat().getName()); // Prints the scan format (qrcode, pdf417 etc.) | |
// If you would like to resume scanning, call this method below: | |
mScannerView.resumeCameraPreview(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment