编写Android 调用摄像头
时间: 2023-11-10 21:55:06 浏览: 140
好的,您可以参考以下步骤:
1. 添加权限
首先,在AndroidManifest.xml中添加以下权限:
```
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
这将允许您的应用程序访问设备上的摄像头和存储。
2. 创建布局
然后,在您的布局文件中添加一个SurfaceView和一个按钮,以便用户可以启动摄像头。
```
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Picture" />
```
3. 实现启动摄像头的方法
在您的活动中,您需要实现启动摄像头并将预览显示在SurfaceView上的方法。您可以使用Camera类来实现这一点。
```
private Camera camera;
private SurfaceView surfaceView;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = findViewById(R.id.surfaceView);
button = findViewById(R.id.button);
// Add button click listener
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Start the camera
startCamera();
}
});
}
private void startCamera() {
try {
// Get the camera object
camera = Camera.open();
// Create a new surface holder
SurfaceHolder holder = surfaceView.getHolder();
// Set the surface holder callback
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
// Set the camera preview display
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
// Do nothing
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
// Stop the camera preview
camera.stopPreview();
// Release the camera
camera.release();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
```
这个方法使用Camera.open()方法来获取摄像头对象。然后,它创建一个SurfaceHolder对象,并将它添加到SurfaceView上。使用SurfaceHolder.Callback,我们在SurfaceHolder创建和销毁时启动和停止摄像头预览。
4. 实现拍照的方法
最后,您需要实现一个方法,以便用户可以拍照并将图像保存到设备存储中。您可以使用Camera.takePicture()方法来实现这一点。
```
private void takePicture() {
// Create a new picture callback
Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes, Camera camera) {
// Save the image to external storage
File file = new File(Environment.getExternalStorageDirectory(), "picture.jpg");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
// Restart the camera preview
camera.startPreview();
}
};
// Take the picture
camera.takePicture(null, null, pictureCallback);
}
```
这个方法将创建一个Camera.PictureCallback对象,并将其传递给Camera.takePicture()方法。在PictureCallback的回调方法中,我们将保存图像到设备存储,并通过camera.startPreview()方法重新启动摄像头预览。
5. 完成
现在,您的Android应用程序可以使用摄像头拍照了。您可以使用以下代码将它们组合在一起。
```
private Camera camera;
private SurfaceView surfaceView;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = findViewById(R.id.surfaceView);
button = findViewById(R.id.button);
// Add button click listener
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Take the picture
takePicture();
}
});
// Start the camera
startCamera();
}
private void startCamera() {
try {
// Get the camera object
camera = Camera.open();
// Create a new surface holder
SurfaceHolder holder = surfaceView.getHolder();
// Set the surface holder callback
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
// Set the camera preview display
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
// Do nothing
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
// Stop the camera preview
camera.stopPreview();
// Release the camera
camera.release();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private void takePicture() {
// Create a new picture callback
Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes, Camera camera) {
// Save the image to external storage
File file = new File(Environment.getExternalStorageDirectory(), "picture.jpg");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
// Restart the camera preview
camera.startPreview();
}
};
// Take the picture
camera.takePicture(null, null, pictureCallback);
}
```
希望对您有所帮助!
阅读全文