短视频系统,获取camera的的图像并保存
短视频系统,获取camera的的图像并保存实现的相关代码
要想保存camera中的图像就要用到ImageReader
ImageReader中有surface,将这个surface设置到
CaptureRequest和CameraCaptureSession中。
ImageReader的创建
public static @NonNull ImageReader newInstance(
@IntRange(from = 1) int width,
@IntRange(from = 1) int height,
@Format int format,
@IntRange(from = 1) int maxImages)
创建imagerader我们需要的参数
width:我们要读取图片的宽
height 我们要读取图片的高
format 我们要读取图片的格式 ,特别要注意不支持ImageFormat.NV21。我们可以从源码中看到
protected ImageReader(int width, int height, int format, int maxImages, long usage) {
.......
if (format == ImageFormat.NV21) {
throw new IllegalArgumentException(
"NV21 format is not supported");
}
.......
}
maxImages 表示我们最多可以同时从ImageRader中获取多少张图片。获取的越多,需要的buffer也就越多。所以这里要注意。
我们知道camera可以支持很多分辨率,那么我们就从camera当中,找到我们支持的宽高
所以就有了下面的代码
从camera中获取宽高的方法
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(backCameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
Size largest = Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.YUV_420_888)), new CompareSizesByArea());
创建ImageRader
mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
ImageFormat.YUV_420_888, 2);
width = largest.getWidth();
height = largest.getHeight();
获取到宽高后我们创建ImageRader设置到CameraCaptureSession
OutputConfiguration outputConfiguration = new OutputConfiguration(binding.surfaceView.getHolder().getSurface());
OutputConfiguration imageReaderOutputConfiguration = new OutputConfiguration(mImageReader.getSurface());
List<OutputConfiguration> outputs = new ArrayList<>();
outputs.add(outputConfiguration);
outputs.add(imageReaderOutputConfiguration);
SessionConfiguration sessionConfiguration
= new SessionConfiguration(SessionConfiguration.SESSION_REGULAR,
outputs,
mExecutorService,
new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
}
}
);
mCamera.createCaptureSession(sessionConfiguration);
我们将ImageReader设置到CaptureRequest
private void record() {
try {
Log.e("CameraActivity", "record");
i = 0;
File file = new File(savePicPath);
DirUtil.deleteDir(file);
final CaptureRequest.Builder builder
= mCamera.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
builder.addTarget(binding.surfaceView.getHolder().getSurface());
builder.addTarget(mImageReader.getSurface());
OutputConfiguration outputConfiguration = new OutputConfiguration(binding.surfaceView.getHolder().getSurface());
OutputConfiguration imageReaderOutputConfiguration = new OutputConfiguration(mImageReader.getSurface());
List<OutputConfiguration> outputs = new ArrayList<>();
outputs.add(outputConfiguration);
outputs.add(imageReaderOutputConfiguration);
SessionConfiguration sessionConfiguration
= new SessionConfiguration(SessionConfiguration.SESSION_REGULAR,
outputs,
mExecutorService,
new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
}
}
);
mCamera.createCaptureSession(sessionConfiguration);
} catch (Exception e) {
}
}
以上就是短视频系统,获取camera的的图像并保存实现的相关代码, 更多内容欢迎关注之后的文章