When resuming, camera turns black. Need help to make camera initialize

zzinzzin CAMember ✭✭

` public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Open an instance of the first camera and retrieve its info.
camera = GetCameraInstance (CAMERA_ID);
Camera.CameraInfo cameraInfo = null;

        if (camera != null) {
            // Get camera info only if the camera is available
            cameraInfo = new Camera.CameraInfo ();
            Camera.GetCameraInfo (CAMERA_ID, cameraInfo);
        }

        View root = inflater.Inflate (Resource.Layout.framelayout, null);

        // Get the rotation of the screen to adjust the preview image accordingly.
        SurfaceOrientation displayRotation = Activity.WindowManager.DefaultDisplay.Rotation;

        // Create the Preview view and set it as the content of this Activity.
        cameraPreview = new CameraPreview (Activity, camera, cameraInfo, displayRotation);
        var preview =  root.FindViewById <FrameLayout> (Resource.Id.camera_preview);
        preview.AddView (cameraPreview);
        return root;
    }

    public override void OnResume ()
    {
        base.OnResume ();
        if (camera == null) {
            zcamint ();
        }
    }

    public override void OnPause () 
    {
        base.OnPause ();
        ReleaseCamera ();
    }

    public static Camera GetCameraInstance (int cameraId)
    {
        Camera c = null;

        try {
            c = Camera.Open (cameraId); 

            Log.Debug(TAG,"camera Opening");// attempt to get a Camera instance
        } catch (Exception e) {
            // Camera is not available (in use or does not exist)
            Log.Debug (TAG, string.Format ("Camera {0} is not available: {1}", cameraId,  e.Message));
        }

        return c;   // returns null if camera is unavailable
    }

    void ReleaseCamera ()
    {
        if (camera != null) {
            cameraPreview.Holder.RemoveCallback (cameraPreview);
            camera.Release ();
            // release the camera for other applications
            camera = null;
            Log.Debug (TAG, "camera paused");
        }
    }

    void zcamint(){
        camera = GetCameraInstance (CAMERA_ID);
        Camera.CameraInfo cameraInfo = null;

        if (camera != null) {
            // Get camera info only if the camera is available
            cameraInfo = new Camera.CameraInfo ();
            Camera.GetCameraInfo (CAMERA_ID, cameraInfo);
        }

        View root = Activity.LayoutInflater.Inflate (Resource.Layout.framelayout, null);

        // Get the rotation of the screen to adjust the preview image accordingly.
        SurfaceOrientation displayRotation = Activity.WindowManager.DefaultDisplay.Rotation;

        // Create the Preview view and set it as the content of this Activity.
        cameraPreview = new CameraPreview (Activity, camera, cameraInfo, displayRotation);
        var preview =  root.FindViewById <FrameLayout> (Resource.Id.camera_preview);
        preview.AddView (cameraPreview);
        cameraPreview = new CameraPreview (Activity, camera, cameraInfo, displayRotation);
    }`

Answers

  • Quan.HoangQuan.Hoang USMember ✭✭

    Do you have the log output of the issue as well as a little more details of what you are seeing? That would help people here on the forum help you better.

  • zzinzzin CAMember ✭✭

    the issue is that there is no log output because the app doesn't crash. the screen just goes black.

  • Quan.HoangQuan.Hoang USMember ✭✭
    edited October 2015

    There should be some log output regardless of whether the app crashes or not since you have some in your codes. I would add a few more Debug.WriteLine to get a better handle on how your codes are working. Otherwise you would only be guessing as to the problem.

    Also, are you having a problem with the camera turning on altogether? or just when it is resuming?...

  • zzinzzin CAMember ✭✭

    I fixed the problem, thanks a lot though.

  • BerayBentesenBerayBentesen TRUniversity ✭✭✭✭

    @zzin can u share solution for other developers :)

Sign In or Register to comment.