我正在尝试将cemera预览集成到我的应用程序中。但是,每当我启动应用程序时,它就会显示黑屏。
这是我的带有摄像头预览集成的片段类:
public class VideoPlayerFragment extends Fragment {
private Context mContext;
private TextureView textureView;
private CameraDevice cameraDevice;
private CaptureRequest.Builder captureRequestBuilder;
private CameraCaptureSession cameraCaptureSessions;
private static final int REQUEST_CAMERA_PERMISSION = 200;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mContext = getActivity();
gc = GlobalClass.getInstance(mContext);
db = DataBaseHelper.getInstance(mContext);
manager = SharedPreferenceManager.getInstance(mContext);
if(covered.equalsIgnoreCase("covered")) {
return inflater.inflate(R.layout.fragment_video, container, false);
}else{
return inflater.inflate(R.layout.fragment_video_side_by_side, container, false);
}
//return inflater.inflate(R.layout.fragment_video, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
SurfaceView video = (SurfaceView) view.findViewById(R.id.video);
RelativeLayout overlay = (RelativeLayout) view.findViewById(R.id.overlay);
textureView = (TextureView) view.findViewById(R.id.camera_texture);
parentLayout = view.findViewById(R.id.parent_layout) ;
overLay = view.findViewById(R.id.overlay);
/* parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mVisible){
// means true
overLay.setVisibility(View.INVISIBLE);
mVisible = false;
}
else{
overLay.setVisibility(View.VISIBLE);
mVisible = true;
}
}
});*/
// _videoController = new VideoController(video, overlay, this, isHTUL_Mode, isHTUP_Mode, isFromHTI, mContext);
}
@Override
public void onStart() {
super.onStart();
// _videoController.initMediaPlayer();
}
@Override
public void onStop() {
super.onStop();
// _videoController.releaseMedia();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onResume() {
super.onResume();
startBackgroundThread();
if (textureView.isAvailable()) {
transformImage(textureView.getWidth(), textureView.getHeight());
openCamera();
} else {
textureView.setSurfaceTextureListener(textureListener);
}
}
TextureView.SurfaceTextureListener textureListener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
transformImage(textureView.getWidth(), textureView.getHeight());
openCamera();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
cameraDevice.close();
cameraDevice = null;
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
};
private final CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
cameraDevice = camera;
createCameraPreview();
}
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
}
@Override
public void onError(@NonNull CameraDevice camera, int error) {
cameraDevice.close();
cameraDevice = null;
}
};
private void openCamera() {
CameraManager manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = manager.getCameraIdList()[1];
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
assert map != null;
imageDimension = map.getOutputSizes(SurfaceTexture.class)[0];
// Add permission for camera and let user grant the permission
if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
return;
}
manager.openCamera(cameraId, stateCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
protected void createCameraPreview() {
SurfaceTexture texture = textureView.getSurfaceTexture();
assert texture != null;
texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight());
transformImage(textureView.getWidth(), textureView.getHeight());
Surface surface = new Surface(texture);
try {
captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureRequestBuilder.addTarget(surface);
cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
//The camera is already closed
if (null == cameraDevice) {
return;
}
// When the session is ready, we start displaying the preview.
cameraCaptureSessions = cameraCaptureSession;
updatePreview();
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
}
}, null);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void updatePreview() {
if (cameraDevice == null) {
}
captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
try {
cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void startBackgroundThread() {
HandlerThread mBackgroundThread = new HandlerThread("Camera Background");
mBackgroundThread.start();
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
}
private void transformImage(int width, int height) {
if (textureView == null) {
return;
} else {
Matrix matrix = new Matrix();
int rotation = getActivity().getWindowManager().getDefaultDisplay().getRotation();
RectF textureRectF = new RectF(0, 0, width, height);
RectF previewRectF = new RectF(0, 0, textureView.getHeight(), textureView.getWidth());
float centerX = textureRectF.centerX();
float centerY = textureRectF.centerY();
if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
previewRectF.offset(centerX - previewRectF.centerX(), centerY - previewRectF.centerY());
matrix.setRectToRect(textureRectF, previewRectF, Matrix.ScaleToFit.FILL);
float scale = Math.max((float) width / width, (float) height / width);
matrix.postScale(scale, scale, centerX, centerY);
matrix.postRotate(90 * (rotation - 2), centerX, centerY);
}
textureView.setTransform(matrix);
}
}
@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(getActivity(), gc.getStringsForElement(mContext, "univCameraPermission", db), Toast.LENGTH_LONG).show();
//finish();
}
}
}
}
它也随机崩溃,并出现以下错误:
java.lang.SecurityException: Lacking privileges to access camera service
at android.hardware.camera2.utils.CameraBinderDecorator.throwOnError(CameraBinderDecorator.java:108)
at android.hardware.camera2.utils.CameraBinderDecorator$CameraBinderDecoratorListener.onAfterInvocation(CameraBinderDecorator.java:73)
at android.hardware.camera2.utils.Decorator.invoke(Decorator.java:81)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy2.cancelRequest(Unknown Source)
at android.hardware.camera2.impl.CameraDeviceImpl.stopRepeating(CameraDeviceImpl.java:926)
at android.hardware.camera2.impl.CameraCaptureSessionImpl.close(CameraCaptureSessionImpl.java:378)
at android.hardware.camera2.impl.CameraCaptureSessionImpl$2.onDisconnected(CameraCaptureSessionImpl.java:514)
at android.hardware.camera2.impl.CameraDeviceImpl$7.run(CameraDeviceImpl.java:228)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
09-21 16:49:09.293 1373-17920/? E/android.os.Debug: ro.product_ship = true
ro.debug_level = 0x4f4c
未获得预览的可能原因是什么?我已经添加了所有必要的摄像头权限,并检查了运行时权限。