Last active
March 21, 2020 01:35
-
-
Save kamaravichow/4a4294cc5e1c4dce735f688bed5353ea to your computer and use it in GitHub Desktop.
QR-scanning.java
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
package com.aravindchowdary.qrplus; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.support.v4.app.ActivityCompat; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v7.app.AlertDialog; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.Toast; | |
import com.google.android.gms.ads.AdListener; | |
import com.google.android.gms.ads.AdRequest; | |
import com.google.android.gms.ads.InterstitialAd; | |
import com.google.zxing.Result; | |
import me.dm7.barcodescanner.zxing.ZXingScannerView; | |
import static android.Manifest.permission.CAMERA; | |
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler { | |
private static final int REQUEST_CAMERA = 1; | |
private ZXingScannerView scannerView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
View decorView = getWindow().getDecorView(); | |
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; | |
decorView.setSystemUiVisibility(uiOptions); | |
scannerView = new ZXingScannerView(this); | |
setContentView(scannerView); | |
int currentApiVersion = Build.VERSION.SDK_INT; | |
if(currentApiVersion >= Build.VERSION_CODES.M) | |
{ | |
if(checkPermission()) | |
{ | |
} | |
else | |
{ | |
requestPermission(); | |
} | |
} | |
} | |
private boolean checkPermission() | |
{ | |
return (ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA) == PackageManager.PERMISSION_GRANTED); | |
} | |
private void requestPermission() | |
{ | |
ActivityCompat.requestPermissions(this, new String[]{CAMERA}, REQUEST_CAMERA); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
int currentapiVersion = android.os.Build.VERSION.SDK_INT; | |
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) { | |
if (checkPermission()) { | |
if(scannerView == null) { | |
scannerView = new ZXingScannerView(this); | |
setContentView(scannerView); | |
} | |
scannerView.setResultHandler(this); | |
scannerView.startCamera(); | |
} else { | |
requestPermission(); | |
} | |
} | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
scannerView.stopCamera(); | |
} | |
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { | |
switch (requestCode) { | |
case REQUEST_CAMERA: | |
if (grantResults.length > 0) { | |
boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED; | |
if (cameraAccepted){ | |
}else { | |
Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access and camera", Toast.LENGTH_LONG).show(); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
if (shouldShowRequestPermissionRationale(CAMERA)) { | |
showMessageOKCancel("You need to allow access to both the permissions", | |
new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
requestPermissions(new String[]{CAMERA}, | |
REQUEST_CAMERA); | |
} | |
}); | |
return; | |
} | |
} | |
} | |
} | |
break; | |
} | |
} | |
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) { | |
new android.support.v7.app.AlertDialog.Builder(MainActivity.this) | |
.setMessage(message) | |
.setPositiveButton("OK", okListener) | |
.setNegativeButton("Cancel", null) | |
.create() | |
.show(); | |
} | |
@Override | |
public void handleResult(Result result) { | |
final String myResult = result.getText(); | |
AlertDialog.Builder builder = new AlertDialog.Builder(this); | |
builder.setTitle("Scan Result"); | |
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
scannerView.resumeCameraPreview(MainActivity.this); | |
} | |
}); | |
builder.setNeutralButton("Visit", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
scannerView.resumeCameraPreview(MainActivity.this); | |
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myResult)); | |
startActivity(browserIntent); | |
} | |
}); | |
builder.setMessage(result.getText()); | |
AlertDialog alert1 = builder.create(); | |
alert1.show(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment