gpt4 book ai didi

安卓相机黑屏

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:23:09 33 4
gpt4 key购买 nike

我正在尝试创建一个将读取二维码的应用程序,该代码间歇性地工作,但它会给出权限错误并且不会加载。我相信我已经修复了这些问题,但现在我得到了一个黑屏。没有错误所以我不知道在哪里看,但我觉得它与权限有关。

package com.sample.qrcodereadervision.Activity;

import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;

import com.sample.qrcodereadervision.R;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.PermissionUtils;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public class QRcodeReader extends AppCompatActivity {
public final String TAG = getClass().getSimpleName();

private CameraSource cameraSource;
/** UI Parts : Preview screen */
private SurfaceView cameraView;
/** UI Parts : decoding results */
private TextView barcodeInfo;

private static final int REQUEST_CAMERA = 0x00000011;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("QRcodeReader", "onCreate");
setContentView(R.layout.activity_main);

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestCameraPermission();
}

}

@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart");
barcodeInfo = (TextView) findViewById(R.id.code_info);
cameraView = (SurfaceView) findViewById(R.id.camera_view);
startQRcodeReader();
// startCameraSource();
}

@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}

//@Override
//public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
// Log.d(TAG, "onRequestPermissionsResult");
// QRcodeReaderPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
//}

private void startQRcodeReader() {
Log.d(TAG, "startQRcodeReader");
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();

cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setAutoFocusEnabled(true)
.build();

QRcodeReaderPermissionsDispatcher.startCameraSourceWithCheck(QRcodeReader.this);
//requestCameraPermission();

//QR Code reading
barcodeDetector.setProcessor(
new Detector.Processor<Barcode>() {
@Override
public void release() {
}

@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size() != 0) {
barcodeInfo.post(
new Runnable() {
public void run() {
barcodeInfo.setText(
barcodes.valueAt(0).displayValue
);
}
});
}
}
});
}

@NeedsPermission(Manifest.permission.CAMERA)
void startCameraSource() {
Log.d(TAG, "startCameraSource");
Toast.makeText(this, "\n" +
"Start the camera source", Toast.LENGTH_SHORT)
.show();
try {
releaseCameraAndPreview();
if (cameraSource != null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
cameraSource.start(cameraView.getHolder());
return;
}

}
} catch (IOException | SecurityException e) {
Log.w(TAG, e);
}
}


@SuppressWarnings("unused")
@OnPermissionDenied(Manifest.permission.CAMERA)
void deniedPermission() {
Log.d(TAG, "deniedPermission");
if (PermissionUtils.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
Toast.makeText(this, "\n" +
"It failed to camera startup .", Toast.LENGTH_SHORT)
.show();
}
}

@SuppressWarnings("unused")
@OnShowRationale(Manifest.permission.CALL_PHONE)
void showRationalForStorage(final PermissionRequest request) {
Log.d(TAG, "showRationalForStorage");
Toast.makeText(this, "\n" +
"The camera of the use permit is required", Toast.LENGTH_SHORT)
.show();
request.proceed();
}

private void releaseCameraAndPreview() {
if (cameraSource != null) {
cameraSource.release();
cameraSource = null;
}
}

private void requestCameraPermission() {
Log.i(TAG, "CAMERA permission has NOT been granted. Requesting permission.");

if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
Log.i(TAG,
"Displaying camera permission rationale to provide additional context.");
Snackbar.make(findViewById(R.id.code_info), R.string.permission_camera_rationale,
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(QRcodeReader.this,
new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA);
}
})
.show();
} else {

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
@NonNull int[] grantResults) {
Log.d(TAG, "onRequestPermissionsResult");
switch (requestCode) {
case REQUEST_CAMERA: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

QRcodeReaderPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
startCameraSource();

} else {

finish();
}
}
break;
}
}

}

list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.qrcodereadervision">

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

<application
android:theme="@style/AppTheme"
android:label="@string/app_name"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:supportsRtl="true">

<activity android:name=".Activity.QRcodeReader">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="barcode"/>
</application>

</manifest>

最佳答案

来晚了,但它会对你有所帮助。

之后,您已经设置了相机并添加了预览和其他所有内容。打电话

recreate();

像这样在您的 Camera Activity 中使用示例。

    camera = prepareCamera(camera); // a method to get camera instance
preview = createCameraPreview(camera) // a method which returns object of surfaceview
showCameraPreview(preview); // adding preview to the frame
recreate(); // <---This line should be called every time we setup a camera preview

关于安卓相机黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38527357/

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