前摄像头打开时显示空白。后部工作正常

时间:2019-06-02 15:51:26

标签: android android-camera

当我尝试打开前置摄像头时,它只会显示一个空白屏幕。但是后置摄像头工作正常。我在整个论坛上尝试了许多不同的代码。是的,我正在使用带有前后摄像头的物理设备。 这是我的代码

public class CamFragment extends Fragment {

android.hardware.Camera camera;
ShowCamera showCamera;
FrameLayout frameLayout;
public CamFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_cam, container, false);
    if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(getContext(), "Permission Denied", Toast.LENGTH_LONG).show();
        ActivityCompat.requestPermissions(getActivity(), new String[] {
            Manifest.permission.CAMERA
        }, 1);
    }

    frameLayout = view.findViewById(R.id.frame);
    int cameraCount = 0;

    android.hardware.Camera.CameraInfo cameraInfo = new android.hardware.Camera.CameraInfo();
    cameraCount = android.hardware.Camera.getNumberOfCameras();
    for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
        android.hardware.Camera.getCameraInfo(camIdx, cameraInfo);
        if (cameraInfo.facing == android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT) {
            try {
                camera = android.hardware.Camera.open(camIdx);
            } catch (RuntimeException e) {
                Log.e("Your_TAG", "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }
    // camera= android.hardware.Camera.open();

    showCamera = new ShowCamera(getContext(), camera);
    frameLayout.addView(showCamera);
    return view;
}

}

0 个答案:

没有答案