android Camera使用示例
时间: 2023-07-11 10:20:42 浏览: 103
Android Camera使用实例
以下是一个简单的 Android Camera 使用示例:
1. 首先,在 AndroidManifest.xml 文件中添加相机的权限:
```
<uses-permission android:name="android.permission.CAMERA" />
```
2. 在布局文件中添加一个 SurfaceView 作为相机的预览视图:
```
<SurfaceView
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
3. 在 Activity 中获取相机实例并设置预览视图:
```
private Camera mCamera;
private SurfaceView mPreview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPreview = (SurfaceView) findViewById(R.id.camera_preview);
// 获取相机实例
mCamera = getCameraInstance();
// 设置预览视图
CameraPreview cameraPreview = new CameraPreview(this, mCamera);
mPreview.addView(cameraPreview);
}
// 获取相机实例的方法
private Camera getCameraInstance() {
Camera camera = null;
try {
camera = Camera.open();
} catch (Exception e) {
// 相机不可用或无法访问
}
return camera;
}
```
4. 创建一个 CameraPreview 类,继承 SurfaceView 并实现 SurfaceHolder.Callback 接口:
```
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// 初始化 SurfaceHolder,并添加回调
mHolder = getHolder();
mHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// 当 Surface 创建时,启动相机预览
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
// 相机预览失败
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// 当 Surface 改变时,调整相机预览大小
if (mHolder.getSurface() == null || mCamera == null) {
return;
}
try {
mCamera.stopPreview();
} catch (Exception e) {
// 忽略异常
}
try {
Camera.Parameters parameters = mCamera.getParameters();
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
Camera.Size optimalSize = getOptimalPreviewSize(sizes, width, height);
parameters.setPreviewSize(optimalSize.width, optimalSize.height);
mCamera.setParameters(parameters);
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
// 相机预览失败
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// 当 Surface 销毁时,释放相机资源
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
// 获取最佳的相机预览尺寸
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int width, int height) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) width / height;
if (sizes == null) {
return null;
}
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = height;
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) {
continue;
}
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
}
```
5. 最后,在 Activity 的 onDestroy() 方法中释放相机资源:
```
@Override
protected void onDestroy() {
super.onDestroy();
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
```
以上是一个简单的 Android Camera 使用示例,可以实现基本的相机预览功能。
阅读全文