Reputation: 23
I am using the below activity code to scan QR code:
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
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.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
import static android.Manifest.permission_group.CAMERA;
public class QRScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private static final int REQUEST_CAMERA = 1;
private ZXingScannerView scannerView;
private static int camId = Camera.CameraInfo.CAMERA_FACING_BACK;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
int currentApiVersion = Build.VERSION.SDK_INT;
if(currentApiVersion >= Build.VERSION_CODES.M)
{
if(checkPermission())
{
Toast.makeText(getApplicationContext(), "Permission already granted!", Toast.LENGTH_LONG).show();
}
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){
Toast.makeText(getApplicationContext(), "Permission Granted, Now you can access camera", Toast.LENGTH_LONG).show();
}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) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{CAMERA},
REQUEST_CAMERA);
}
}
});
return;
}
}
}
}
break;
}
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new android.support.v7.app.AlertDialog.Builder(QRScannerActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
@Override
public void handleResult(Result result) {
final String myResult = result.getText();
Log.d("QRCodeScanner", result.getText());
Log.d("QRCodeScanner", result.getBarcodeFormat().toString());
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(QRScannerActivity.this);
}
});
builder.setNeutralButton("Visit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myResult));
startActivity(browserIntent);
}
});
builder.setMessage(result.getText());
AlertDialog alert1 = builder.create();
alert1.show();
/*MessageSender messageSender = new MessageSender();
messageSender.execute(result.getText().toString());*/
}
}
Manifest file permissions:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera" />
<uses-permission android:name="android.hardware.camera.autofocus" />
build.gradle added compile line in dependencies:
compile 'me.dm7.barcodescanner:zxing:1.9'
All i get is a white blank activity when i run on my samsung phone Android: 4.4.4
and the following errors:
05-28 08:56:59.751 1381-1381/? E/SamsungIME: AbstractCandidateLayout-setCandidates suggestions count : 9
05-28 08:56:59.761 1381-1381/? E/SamsungIME: SwiftkeyWrapper-inputKey
05-28 08:56:59.941 1381-1381/? E/SamsungIME: AbstractCandidateLayout-setCandidates suggestions count : 9
05-28 08:57:00.831 235-235/? E/SMD: DCD ON
05-28 08:57:03.591 750-1132/? E/Watchdog: !@Sync 199
05-28 08:57:03.841 235-235/? E/SMD: DCD ON
05-28 08:57:06.841 235-235/? E/SMD: DCD ON
05-28 08:57:09.841 235-235/? E/SMD: DCD ON
05-28 08:57:12.841 235-235/? E/SMD: DCD ON
05-28 08:57:15.841 235-235/? E/SMD: DCD ON
05-28 08:57:18.851 235-235/? E/SMD: DCD ON
05-28 08:57:21.851 235-235/? E/SMD: DCD ON
05-28 08:57:24.852 235-235/? E/SMD: DCD ON
05-28 08:57:27.851 235-235/? E/SMD: DCD ON
05-28 08:57:30.851 235-235/? E/SMD: DCD ON
05-28 08:57:33.601 750-1132/? E/Watchdog: !@Sync 200
05-28 08:57:33.861 235-235/? E/SMD: DCD ON
05-28 08:57:36.861 235-235/? E/SMD: DCD ON
05-28 08:57:39.861 235-235/? E/SMD: DCD ON
05-28 08:57:42.861 235-235/? E/SMD: DCD ON
05-28 08:57:45.861 235-235/? E/SMD: DCD ON
05-28 08:57:48.871 235-235/? E/SMD: DCD ON
05-28 08:57:51.871 235-235/? E/SMD: DCD ON
05-28 08:57:54.871 235-235/? E/SMD: DCD ON
05-28 08:57:57.871 235-235/? E/SMD: DCD ON
05-28 08:58:00.881 235-235/? E/SMD: DCD ON
05-28 08:58:03.601 750-1132/? E/Watchdog: !@Sync 201
05-28 08:58:24.891 235-235/? E/SMD: DCD ON
05-28 08:58:27.891 235-235/? E/SMD: DCD ON
05-28 08:58:30.901 235-235/? E/SMD: DCD ON
05-28 08:58:33.601 750-1132/? E/Watchdog: !@Sync 202
05-28 08:58:33.901 235-235/? E/SMD: DCD ON
05-28 08:58:36.891 235-235/? E/SMD: DCD ON
Upvotes: 1
Views: 831
Reputation: 21
Make sure you are not setting WindowManager.LayoutParams.FLAG_FULLSCREEN on any activity before the one that uses ZXingScannerView.
// WindowManager.LayoutParams.FLAG_FULLSCREEN suregetWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Remove the commented out lines if you have them on any activity.
Upvotes: 1
Reputation: 662
I think the problem here is :RequestPermission Try this:
private static final int ZXING_CAMERA_PERMISSION = 1123;
public void requestPermisstion() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA}, ZXING_CAMERA_PERMISSION);
} else {
//start camera or go to QR code activity
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case ZXING_CAMERA_PERMISSION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(this, QRCodeActivity.class);
startActivity(intent);
} else {
Toast.makeText(this, "Please grant camera permission to use the QR Scanner", Toast.LENGTH_SHORT).show();
}
return;
}
}
Upvotes: 0
Reputation: 1268
Update your onResume()
method
@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();
}
}else{ // below api 23
scannerView.setResultHandler(this);
scannerView.startCamera();
}
}
Since Android 4.4.4 (API 19) doesn't need runtime permission, so you can just run startCamera()
code
Upvotes: 0