我是Android编程的新手,我想做的就是在表面视图中打开相机应用程序,以便我可以打开相机并设置一些参数如下?
Camera camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(p);
camera.startPreview();
camera.release();
我发现评论说我必须通过表面。所以我创建了以下表面:
package com.example.fcloader;
import java.io.IOException;
import android.content.Context;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.hardware.Camera;
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback
{
private SurfaceHolder holder;
private Camera camera;
public CameraSurfaceView(Context context)
{
super(context);
//Initiate the Surface Holder properly
this.holder = this.getHolder();
this.holder.addCallback(this);
this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
try
{
//Open the Camera in preview mode
this.camera = Camera.open();
this.camera.setPreviewDisplay(this.holder);
}
catch(IOException ioe)
{
ioe.printStackTrace(System.out);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(width, height);
camera.setParameters(parameters);
camera.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
// Surface will be destroyed when replaced with a new screen
//Always make sure to release the Camera instance
camera.stopPreview();
camera.release();
camera = null;
}
public Camera getCamera()
{
return this.camera;
}
}
这是我上网的基本表面代码。我的问题是,我应该改变什么来使这些事情相互协调?
答案 0 :(得分:1)
在camera.startPreview()
之后,你必须写preview.setCamera(camera)
来设置相机。 preview
是您的类的对象,它扩展SurfaceView
并实现SurfaceHolder.Callback
。
camera.startPreview();
preview.setCamera(camera);
答案 1 :(得分:0)
清单应包括以下内容:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
您是否在广告布局中添加了CameraSurfaceView
?您可以在layout.xml
中部署它,也可以通过onCreate()
以编程方式将其添加到layout.addView
。