使用zxing显示白色空 Activity 的Android QR阅读器

标签 android

我正在使用以下 Activity 代码扫描二维码:

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());*/

}
}

list 文件权限:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera" />
<uses-permission android:name="android.hardware.camera.autofocus" />

build.gradle 在依赖项中添加了编译行:

compile 'me.dm7.barcodescanner:zxing:1.9'

当我在我的三星手机 Android: 4.4.4 上运行时,我得到的只是一个白色的空白 Activity

以及以下错误:

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

最佳答案

确保您没有在使用 ZXingScannerView 之前的任何 Activity 上设置 WindowManager.LayoutParams.FLAG_FULLSCREEN。

//WindowManager.LayoutParams.FLAG_FULLSCREEN suregetWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

如果您在任何 Activity 中有注释掉的行,请将其删除。

关于使用zxing显示白色空 Activity 的Android QR阅读器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50616030/

相关文章:

android - 颤振 : Google Play services out of date

Android 安全 HTTP 通信

android - BroadcastReceiver 从未调用过

android - 单击按钮时如何在android中获取复选框,带有ListView值的EditText?

android - 从 apk 获取证书详细信息

安卓 : Logout of Facebook from another Activity

java - 我无法从 AsyncTask 获得结果?

android - 我可以避免在 colors.xml 中多次指定相同的颜色值吗?

java - setDisplayHomeAsUpEnabled 关闭 ActionBarDrawerToggle

android - Google 地点 API(地点搜索)