android studio 拍照
时间: 2023-09-15 09:18:02 浏览: 86
在Android Studio中拍照有两种方式:使用Camera API和使用CameraX API。
使用Camera API:
1. 添加权限到AndroidManifest.xml文件:
```
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
```
2. 在布局文件中添加一个SurfaceView或TextureView:
```
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
3. 在Activity或Fragment中添加以下代码:
```
private Camera camera;
private SurfaceView surfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = findViewById(R.id.surfaceView);
surfaceView.getHolder().addCallback(surfaceCallback);
}
private SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(width, height);
camera.setParameters(parameters);
camera.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}
};
public void takePicture(View view) {
camera.takePicture(null, null, pictureCallback);
}
private Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
//处理拍照数据
}
};
```
4. 在布局文件中添加一个按钮,并在Activity或Fragment中实现按钮的点击事件:
```
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Picture"
android:onClick="takePicture" />
```
使用CameraX API:
1. 添加依赖到build.gradle文件:
```
dependencies {
def camerax_version = "1.0.0"
// CameraX core library
implementation "androidx.camera:camera-core:$camerax_version"
// CameraX Camera2 extensions
implementation "androidx.camera:camera-camera2:$camerax_version"
// CameraX Lifecycle library
implementation "androidx.camera:camera-lifecycle:$camerax_version"
// CameraX View class
implementation "androidx.camera:camera-view:1.0.0-alpha23"
}
```
2. 在布局文件中添加一个PreviewView:
```
<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
3. 在Activity或Fragment中添加以下代码:
```
private ListenableFuture<ProcessCameraProvider> cameraProviderFuture;
private Preview preview;
private ImageCapture imageCapture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> {
try {
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
preview = new Preview.Builder().build();
preview.setSurfaceProvider(previewView.createSurfaceProvider());
imageCapture = new ImageCapture.Builder().build();
cameraProvider.unbindAll();
cameraProvider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA, preview, imageCapture);
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}, ContextCompat.getMainExecutor(this));
}
public void takePicture(View view) {
File file = new File(getExternalMediaDirs()[0], System.currentTimeMillis() + ".jpg");
ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions.Builder(file).build();
imageCapture.takePicture(outputFileOptions, ContextCompat.getMainExecutor(this), new ImageCapture.OnImageSavedCallback() {
@Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
//处理拍照数据
}
@Override
public void onError(@NonNull ImageCaptureException exception) {
exception.printStackTrace();
}
});
}
```
4. 在布局文件中添加一个按钮,并在Activity或Fragment中实现按钮的点击事件:
```
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Picture"
android:onClick="takePicture" />
```