gpt4 book ai didi

java - 无法从 Samsung Galaxy S (2.2.1) 上的前置摄像头拍照

转载 作者:行者123 更新时间:2023-11-30 04:06:07 26 4
gpt4 key购买 nike

我正在像这里描述的那样使用相机: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;
}
}
}

最佳答案

你应该得到另一个相机实例。参见 Camera.open(int cameraId) .您必须找出前置摄像头的 ID 并通过上述方法使用它。

更新:

采用 Android API 级别 7、8 解决方案的 Samsung Galaxy S

在 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

看来您必须连同 id 一起更改 PreviewSize。

关于java - 无法从 Samsung Galaxy S (2.2.1) 上的前置摄像头拍照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11634261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com