-3

I followed this guide to get the base on my Cameraapplication, I have changed some code to get it work because the tutorial has some faults in it and stuff like that but the backfacing-camera is with preview and everything is working, and I can take a photo which get saved on the sd-card.

Now to my problem, I want to be able to switch to the frontfacing-camera but I don't get it to work. I have searched the web for hours and hours but I can't get it to work. I get this error:

java.lang.RuntimeException: Fail to connect to camera service

This is the code when I try to switch to the camera:

OnClickListener switchCameraListener = new OnClickListener() {
    @Override
    public void onClick(View v) {

        findFrontFacingCamera();
        chooseCamera();
    }
};

All other code is on that link i sent. I can of course post it here if that makes it easier so please tell me if I should write any other code here. I'm very grateful for any help. Thanks! /Alex

EDIT: posting the code now on both of my classes:

package com.alexborghesi.androidcameraexample;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.PictureCallback;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class cameraClass extends Activity implements        SensorEventListener {

Shake shake = new Shake();
private Camera mCamera;
private previewClass mPreview;
private PictureCallback mPicture;
private Button capture, switchCamera;
private Context myContext;
private RelativeLayout cameraPreview;
private boolean cameraFront = false;


Sensor acc;
SensorManager accManager;
TextView textView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    myContext = this;

    accManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    acc = accManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    accManager.registerListener(this, acc, SensorManager.SENSOR_DELAY_UI);

    textView = (TextView) findViewById(R.id.textView);

    initialize();
}

public void initialize() {
    cameraPreview = (RelativeLayout) findViewById(R.id.camera_preview);
    mPreview = new previewClass(myContext, mCamera);
    cameraPreview.addView(mPreview);

    capture = (Button) findViewById(R.id.button_capture);
    capture.setOnClickListener(captureListener);

    switchCamera = (Button) findViewById(R.id.button_ChangeCamera);
    switchCamera.setOnClickListener(switchCameraListener);

}

private int findFrontFacingCamera() {
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
            cameraId = i;
            cameraFront = true;
            break;
        }
    }
    return cameraId;
}

private int findBackFacingCamera() {
    int cameraId = -1;
    //Search for the back facing camera
    //get the number of cameras
    int numberOfCameras = Camera.getNumberOfCameras();
    //for every camera check
    for (int i = 0; i < numberOfCameras; i++) {
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
            cameraId = i;
            cameraFront = false;
            break;
        }
    }
    return cameraId;
}

public void onResume() {

    super.onResume();

    if (!hasCamera(myContext)) {
        Toast toast = Toast.makeText(myContext, "Sorry, your phone does not have a camera!", Toast.LENGTH_LONG);
        toast.show();
        finish();
    }
    if (mCamera == null) {
        if (findFrontFacingCamera() < 0) {
            Toast.makeText(this, "No front facing camera found.", Toast.LENGTH_LONG).show();
            switchCamera.setVisibility(View.GONE);
        }
        mCamera = Camera.open(findBackFacingCamera());
        mPicture = getPictureCallback();


        mPreview.refreshCamera(mCamera);
    }

}

;

public void chooseCamera() {
    //if the camera preview is the front
    if (cameraFront == false) {
        int cameraId = findBackFacingCamera();
        if (cameraId <= 0) {
            //open the backFacingCamera
            //set a picture callback
            //refresh the preview

            mCamera = Camera.open(cameraId);
            mPicture = getPictureCallback();
            mPreview.refreshCamera(mCamera);
        }
    } else {
        int cameraId = findFrontFacingCamera();
        if (cameraId >= 1) {
            //open the frontFacingCamera
            //set a picture callback
            //refresh the preview

            mCamera = Camera.open(cameraId);
            mPicture = getPictureCallback();
            mPreview.refreshCamera(mCamera);
        }
    }
}

@Override
protected void onPause() {
    super.onPause();
    //mSensorManager.unregisterListener(mSensorListener);
    //when on Pause, release camera in order to be used from other applications
    releaseCamera();
}

private boolean hasCamera(Context context) {
    //check if the device has camera
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        return true;
    } else {
        return false;
    }
}

private PictureCallback getPictureCallback() {
    PictureCallback picture = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            //make a new picture file
            File pictureFile = getOutputMediaFile();

            if (pictureFile == null) {
                return;
            }
            try {
                //write the file
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
                Toast toast = Toast.makeText(myContext, "Picture saved: " + pictureFile.getName(), Toast.LENGTH_LONG);
                toast.show();

            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            }

            //refresh camera to continue preview
            mPreview.refreshCamera(mCamera);
        }
    };
    return picture;
}




OnClickListener captureListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        mCamera.takePicture(null, null, mPicture);
    }
};

OnClickListener switchCameraListener = new OnClickListener() {
    @Override
    public void onClick(View v) {

        findFrontFacingCamera();
        chooseCamera();
    }
};

//make picture and save to a folder
private static File getOutputMediaFile() {
    //make a new file directory inside the "sdcard" folder
    File mediaStorageDir = new File("/sdcard/", "JCG Camera");

    //if this "JCGCamera folder does not exist
    if (!mediaStorageDir.exists()) {
        //if you cannot make this folder return
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }
    }

    //take the current timeStamp
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    //and make a media file:
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");

    return mediaFile;
}







private void releaseCamera() {
    // stop and release camera
    if (mCamera != null) {
        mCamera.release();
        mCamera = null;
    }
}

@Override
public void onSensorChanged(SensorEvent event) {

    if(shake.thresholdSpan(event,  -6.0, 6.0)){
        textView.setText("I'm Shaking!");

    }
    else{
        textView.setText("SKAKA DÅ!!");
    }


}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
}

SECOND CLASS:

package com.alexborghesi.androidcameraexample;

import java.io.IOException;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

public class previewClass extends SurfaceView implements SurfaceHolder.Callback {


private SurfaceHolder mHolder;
private Camera mCamera;
private Camera.Parameters mParameters;

public previewClass(Context context, Camera camera) {
    super(context);
    mCamera = camera;
    mHolder = getHolder();
    mHolder.addCallback(this);
    // deprecated setting, but required on Android versions prior to 3.0
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
    try {
        // create the surface and start camera preview
        if (mCamera == null) {

            mCamera.setPreviewDisplay(holder);

            mCamera.startPreview();

        }
    } catch (IOException e) {
        Log.d(VIEW_LOG_TAG, "Error setting camera preview: " + e.getMessage());
    }
}

public void refreshCamera(Camera camera) {
    if (mHolder.getSurface() == null) {
        // preview surface does not exist
        return;
    }
    // stop preview before making changes
    try {
        mCamera.stopPreview();
    } catch (Exception e) {
        // ignore: tried to stop a non-existent preview
    }
    // set preview size and make any resize, rotate or
    // reformatting changes here
    // start preview with new settings

    setCamera(camera);
    try {
        mCamera.setPreviewDisplay(mHolder);

        mCamera.setDisplayOrientation(90);
        mCamera.startPreview();
    } catch (Exception e) {
        Log.d(VIEW_LOG_TAG, "Error starting camera preview: " + e.getMessage());
    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    {

        mCamera.stopPreview();
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.
        refreshCamera(mCamera);
    }
}

public void setCamera(Camera camera) {
    //method to set a camera instance
    mCamera = camera;
}


@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    // mCamera.release();

}

}

My device is running android 4.2.2.

bogdaan
  • 3
  • 3

2 Answers2

0

you said the camera permission in Manifest.xml?

<manifest

   <application

        <Uses-permission android: name = "android.permission.CAMERA" />

    </ Application>

 </ Manifest>

I have helped

Marco Giovanni
  • 297
  • 7
  • 18
0

Solved the problem myself, the problem was that i had not released the camera before i tried to use the second camera.

i just added this line of code:

releaseCamera();

this method contains this :

private void releaseCamera() {
// stop and release camera
if (mCamera != null) {
    mCamera.release();
    mCamera = null;
 }
}

and then in my click-listener it looks like this:

 OnClickListener switchCameraListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        releaseCamera();
        chooseCamera();
    }
};
bogdaan
  • 3
  • 3