portfoliobuilder
portfoliobuilder

Reputation: 7856

Unable to take image using front camera: Android

I am unable to take pictures using the front camera. My test device definately has both front and back camera/functionality. For some reason pictures are only being taken with back camera. Here is what I have. Note: camera and parameter are globally set.

@Override
   public void surfaceCreated(SurfaceHolder holder)
   {

            camera = Camera.open();
            parameter = camera.getParameters();

            Camera.Size size = getBestPreviewSize(surfaceView.getWidth(), surfaceView.getHeight(), parameter);
            parameter.setPreviewSize(size.width, size.height);

            try {
                camera.setPreviewDisplay(surfaceHolder);
            } catch (Throwable ignored) {
                Log.e(TAG, "set preview error: ", ignored);
            }
            camera.setParameters(parameter);

        }  
    }

In surfaceChanged() I start camera preview.

@Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height)
    {
        // TODO Auto-generated method stub
        if(isPreview == true)
        {
            camera.stopPreview();
            isPreview = false;
        }

        if (camera != null)
        {
            try
            {

                camera.setPreviewDisplay(surfaceHolder);
                camera.setDisplayOrientation(90);
camera.startPreview();
                isPreview = true;
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

In surfaceDestroyed() I release camera resources

@Override
   public void surfaceDestroyed(SurfaceHolder holder)
   {
       if (camera != null) {
           camera.stopPreview();

           camera.release();
           camera = null;
           isPreview = false; 
       }

   }

Here is the referenced method that returns preview size

private Camera.Size getBestPreviewSize(int width, int height,
                                           Camera.Parameters parameters) {
        Camera.Size result = null;

        for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
            if (size.width <= width && size.height <= height) {
                if (result == null) {
                    result = size;
                } else {
                    int resultArea = result.width * result.height;
                    int newArea = size.width * size.height;

                    if (newArea > resultArea) {
                        result = size;
                    }
                }
            }
        }

        this.size = result;
        return (result);
    }

Whenever I want to use the front camera--the front camera is an additional feature, generally the back camera is used--I call frontCamera() method. All this method does is set camera-id to 2, which I believe is the flag for frontCamera(). I then set parameters with updated parameters and take picture.

    private void frontCamera() {

        parameter = camera.getParameters();

            cameraId = findFrontFacingCamera();

            if (cameraId < 0) {
//no front face camera

            } else {

                parameter.set("camera-id", 2);
                camera.setParameters(parameter);
            }

            camera.takePicture(null,
                    null, myPictureCallback_JPG);

    }

My check for front face camera is done in a findFrontFacingCamera() method

private int findFrontFacingCamera() {
        cameraId = -1;
        //search for the front facing camera
        int numberOfCameras = Camera.getNumberOfCameras();
        for (int i = 0; i<numberOfCameras; i++) {
            CameraInfo cInfo = new CameraInfo();
            Camera.getCameraInfo(i, cInfo);
            if (cInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
                Log.d(TAG, "Front Camera found");
                cameraId = i;
                break;
            }
        }
        return cameraId;
    }

Final Notes: numberOfCameras returns 2, indicating that my test device has two cameras. Also, I am reaching that "Front Camera found" tag which further convinces me that this is not a hardware issue. For whatever reason, this code will only take images with back camera. Any ideas? Thanks in advance.

Upvotes: 0

Views: 851

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93559

Don't use .open(). Use .open(cameraID) and pass it the camera id of the front facing camera.

Upvotes: 2

Related Questions