我正在开发适用于Android设备的应用程序,我想扫描QR码,但是我有一个问题,它只是不要求相机许可,因此 surfaceview
黑色。
我的代码:
public class ScanQR extends AppCompatActivity {
private SurfaceView surfaceView;
private CameraSource cameraSource;
private TextView textView;
private BarcodeDetector barcodeDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_qr);
surfaceView = (SurfaceView) findViewById(R.id.scanQR);
textView = (TextView) findViewById(R.id.tvScanQr);
barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
cameraSource = new CameraSource.Builder(this, barcodeDetector).setRequestedPreviewSize(640, 480).build();
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
return;
} try {
cameraSource.start(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
@Override
public void release() {
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
SparseArray<Barcode> qrCodes = detections.getDetectedItems();
if(qrCodes.size() > 0){
textView.post(new Runnable() {
@Override
public void run() {
Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
textView.setText(qrCodes.valueAt(0).displayValue);
}
});
}
}
});
}
Android清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.estcers.partygooo">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" />
<activity android:name=".Login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CreateParty" />
<activity android:name=".SearchUser" />
<activity android:name=".UserInfo" />
<activity android:name=".Friends" />
<activity android:name=".PendingFriends" />
<activity android:name=".Parties" />
<activity android:name=".Invitations" />
<activity android:name=".Entries" />
<activity android:name=".PendingAssistances" />
<activity android:name=".PartyInfo" />
<activity android:name=".GetQR" />
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="barcode" />
<activity android:name=".ScanQR"></activity>
</application>
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ScanQR"
android:orientation="vertical">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="300sp"
android:id="@+id/scanQR"
android:layout_gravity="center"
android:layout_marginTop="100sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tvScanQr"
android:text="AAAAAAAAAAAAAAAA"
android:textSize="20sp"
android:layout_gravity="center"
android:layout_marginTop="20sp"
android:gravity="center"/>
如果有人可以帮助我解决这个问题,我将不胜感激,我只是不知道发生了什么。
我也有实现:
"implementation 'com.google.android.gms:play-services-vision:15.0.2'"
和google()
。
感谢您的帮助!
答案 0 :(得分:0)
当您要打开相机时,请进行以下检查:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(YourCameraActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CAMERA_PERMISSION);
return;
}
并使用以下方法处理结果:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
// close the app
Toast.makeText(CameraActivity.this, "The app requires that permission in order to work properly!", Toast.LENGTH_LONG).show();
finish();
}
}
}
和您的清单中
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />