gpt4 book ai didi

java - 旧库 zxing 未在我的应用程序中运行

转载 作者:行者123 更新时间:2023-12-01 18:47:51 27 4
gpt4 key购买 nike

这段代码有什么问题?我在 MainActivity 中运行以下代码,但我只得到一个空应用程序:

package com.example.testbar;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import com.google.zxing.Result;
import android.util.Log;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
// Programmatically initialize the scanner view
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
}
@Override
public void handleResult(Result rawResult) {
// Do something with the result here
Log.v("TAG", rawResult.getText()); // Prints scan results
// Prints the scan format (qrcode, pdf417 etc.)
Log.v("TAG", rawResult.getBarcodeFormat().toString());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan Result");
builder.setMessage(rawResult.getText());
AlertDialog alert1 = builder.create();
alert1.show();

// If you would like to resume scanning, call this method below:
mScannerView.resumeCameraPreview(this);
}

@Override
public void onResume() {
super.onResume();
// Register ourselves as a handler for scan results.
mScannerView.setResultHandler(this);
// Start camera on resume
mScannerView.startCamera();
}

@Override
public void onPause() {
super.onPause();
// Stop camera on pause
mScannerView.stopCamera();
}

}

我已在 AndroidManifest.xml 文件中添加了权限:

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

代码在 Android Studio 中构建时没有错误,但我只得到一个空应用程序......并且应用程序运行时没有相机。

最佳答案

您应该遵循 Android 文档来检查和请求权限。 https://developer.android.com/training/permissions/requesting

我已经修改了您的代码,这应该是一个工作示例。

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
private static final int MY_PERMISSIONS_REQUEST_CAMERA = 101;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
// Programmatically initialize the scanner view
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
}

@Override
public void handleResult(Result rawResult) {
// Do something with the result here
Log.v("TAG", rawResult.getText()); // Prints scan results
// Prints the scan format (qrcode, pdf417 etc.)
Log.v("TAG", rawResult.getBarcodeFormat().toString());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan Result");
builder.setMessage(rawResult.getText());
AlertDialog alert1 = builder.create();
alert1.show();

// If you would like to resume scanning, call this method below:
mScannerView.resumeCameraPreview(this);
}

@Override
public void onResume() {
super.onResume();
// Register ourselves as a handler for scan results.
mScannerView.setResultHandler(this);
// Start camera on resume

if (hasPermission(Manifest.permission.CAMERA)) {
startCamera();
}
}

@Override
public void onPause() {
super.onPause();
// Stop camera on pause
mScannerView.stopCamera();
}

private void startCamera() {
mScannerView.startCamera();
}

private Boolean hasPermission(String permission) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
// you need to request the permission
Log.d("TAG", "User hasn't granted permission.");

// No explanation needed for camera. request the permission
ActivityCompat.requestPermissions(this,
new String[]{permission},
MY_PERMISSIONS_REQUEST_CAMERA);
}
else {
Log.d("TAG", "User already granted permission.");
return true;
}
return false;
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CAMERA:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startCamera();
} else {
Log.d("TAG", "Permission denied by user...");
}
break;
}
}
}

关于java - 旧库 zxing 未在我的应用程序中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59794143/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com