我正在使用这里描述的相机:Android Camera will not work. startPreview fails。 如果我使用后置摄像头,它工作正常!预览有效,我可以拍照。
但是,当我通过设置参数parameter.set("camera-id", 2)
使用前置摄像头时,它会在camera.takePicture(...)
方法上抛出错误。 预览工作正常,我可以在SurfaceView上看到自己。但我无法拍照。
这是错误:
> 07-24 21:59:52.926: E/AndroidRuntime(2462): Caused by: java.lang.RuntimeException:
> takePicture failed
> at android.hardware.Camera.native_takePicture(Native Method)
> at android.hardware.Camera.takePicture(Camera.java:672)
> at android.hardware.Camera.takePicture(Camera.java:636)
> at org.omich.alarm.PhotoController.takePhoto(PhotoController.java:94)
> ... 15 more
这是我的代码:
public class Photo
{
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;
private CameraCallback mCameraCallback;
public Photo (Activity context)
{
mSurfaceView = (SurfaceView)context.findViewById(R.id.surfaceView);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(new SurfaceCallback());
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void takePhoto ()
{
if(mCameraCallback == null)
{
mCameraCallback = new CameraCallback();
}
try
{
mCamera.takePicture(null, null, mCameraCallback);
}
catch (Throwable e)
{
Log.d("ERROR!!! takePicture error");
}
}
//========================================================================
// private int getFrontCameraId()
// {
// CameraInfo ci = new CameraInfo();
// for (int i = 0 ; i < Camera.getNumberOfCameras(); i++)
// {
// Camera.getCameraInfo(i, ci);
// if (ci.facing == CameraInfo.CAMERA_FACING_FRONT)
// {
// return i;
// }
// }
// return -1; // No front-facing camera found
// }
private static class CameraCallback implements Camera.PictureCallback
{
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
Log.d("Yahoo! Photo taken");
}
}
private class SurfaceCallback implements SurfaceHolder.Callback
{
//==== SurfaceHolder.Callback ============================================
@Override
public void surfaceChanged(SurfaceHolder arg0, int format, int width, int height)
{
Camera camera = mCamera;
Parameters p = camera.getParameters();
if(p != null)
{
p.setPreviewSize(width, height);
p.set("camera-id", 2);
camera.setParameters(p);
}
try
{
camera.setPreviewDisplay(mSurfaceHolder);
camera.startPreview();
}
catch (IOException e)
{
Log.d("setPreviewDisplay error");
}
catch (Throwable e)
{
Log.d("startPreview error");
}
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
// mCamera = Camera.open(getFrontCameraId());
mCamera = Camera.open();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
}
}
答案 0 :(得分:1)
你应该得到另一个Camera-Instance。见Camera.open(int cameraId)。你必须找出前置摄像头的ID,并使用上述方法。
更新:
三星Galaxy S,Android API级别为7,8解决方案
After Camera mCamera = Camera.open()使用setFrontCamera(mCamera):
void setFrontCamera(Camera camera) {
Camera.Parameters parameters = camera.getParameters();
parameters.set("camera-id", 2);
// (800, 480) is also supported front camera preview size at Samsung Galaxy S.
parameters.setPreviewSize(640, 480);
camera.setParameters(parameters);
}
三星也链接到SO: How to use Front Facing Camera on Samsung Galaxy S
您似乎必须更改PreviewSize以及ID。