AndroidCamera无法打开

时间:2016-04-06 11:10:22

标签: android android-camera

我跟着this guide获取我的Cameraapplication的基础,我已经更改了一些代码以使其工作,因为教程中有一些错误和类似的东西,但背面摄像头是预览,一切正常,我可以拍一张保存在SD卡上的照片。

现在我的问题是,我希望能够切换到前置摄像头,但我不能让它工作。我在网上搜索了几个小时,但是我无法让它上​​班。我收到这个错误:

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

这是我尝试切换到相机时的代码:

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

        findFrontFacingCamera();
        chooseCamera();
    }
};

所有其他代码都在我发送的链接上。我当然可以在这里发布,如果它更容易,所以请告诉我是否应该在这里写任何其他代码。我非常感谢任何帮助。谢谢! /亚历

编辑:现在在我的两个班级上发布代码:

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) {

}
}

第二类:

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();

}

}

我的设备正在运行android 4.2.2。

2 个答案:

答案 0 :(得分:0)

你在Manifest.xml中说过相机权限吗?

<manifest

   <application

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

    </ Application>

 </ Manifest>

我帮助了

答案 1 :(得分:0)

我自己解决了这个问题,问题是我在尝试使用第二台相机之前没有释放相机。

我刚添加了这行代码:

releaseCamera();

此方法包含:

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

然后在我的点击监听器中它看起来像这样:

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