讲讲鸿蒙拍到图片存储的代码
时间: 2023-11-09 18:07:53 浏览: 134
鸿蒙拍照存储的代码可以分为三个部分:预览、拍照和保存。
预览部分:
```
SurfaceTexture surfaceTexture = new SurfaceTexture(10);
mCamera.setPreviewTexture(surfaceTexture);
mCamera.startPreview();
```
拍照部分:
```
mCamera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// 拍照完成后的回调函数
// data 即为拍照得到的 byte 数组
// 在这里可以将 byte 数组转换成 Bitmap 或者保存到文件
}
});
```
保存部分:
```
FileOutputStream fos = null;
try {
File file = new File(Environment.getExternalStorageDirectory(), "test.jpg");
fos = new FileOutputStream(file);
fos.write(data); // 写入文件
fos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close(); // 关闭流
}
} catch (IOException e) {
e.printStackTrace();
}
}
```
以上代码实现了拍照后保存到本地文件的功能,可以根据实际需求进行修改。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)