Android CameraX returns 成功但未保存图片
Android CameraX returns success but not saving picture
我已经实现了 android camerax 但它无法正确捕获图像,回调 returns 成功但无法找到或查看图像。
我的实现:
private void startCamera(){
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(new Runnable() {
@Override
public void run() {
try {
ProcessCameraProvider provider = cameraProviderFuture.get();
bindPreview(provider);
} catch (Exception e){
e.printStackTrace();
}
}
}, ContextCompat.getMainExecutor(this));
}
private void bindPreview(@NonNull ProcessCameraProvider cameraProvider){
Preview preview = new Preview.Builder().build();
CameraSelector selector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_FRONT).build();
ImageCapture.Builder builder = new ImageCapture.Builder();
final ImageCapture imageCapture = builder.setTargetRotation(this.getWindowManager().getDefaultDisplay().getRotation()).build();
preview.setSurfaceProvider(cameraView.createSurfaceProvider());
cameraProvider.bindToLifecycle(this,selector,preview,imageCapture);
btnCapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selfieFile = new File(getFilesDir(),"attendance.jpg");
OutputFileOptions outputFileOptions = new OutputFileOptions.Builder(selfieFile).build();
imageCapture.takePicture(outputFileOptions, executor, new ImageCapture.OnImageSavedCallback() {
@Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Glide.with(CameraActivity.this).load(outputFileResults.getSavedUri()).into(capturedImage);
//updateUI(false);
}
});
}
@Override
public void onError(@NonNull ImageCaptureException exception) {
}
});
}
});
}
我已经尝试加载 Uri.fromFile() 并尝试获取 imageproxy 并转换为位图,但仍然有这些问题。任何帮助将不胜感激...
根据 ImageCapture.OutputFileResults.getSavedUri()
的文档,当 ImageCapture.OutputFileOptions
由 MediaStore 支持时,此方法仅 returns 一个值,即当您使用 ImageCapture.OutputFileOptions.Builder(ContentResolver, Uri, ContentValues)
.
您正在使用 ImageCapture.OutputFileOptions.Builder(File)
,这意味着您在调用 ImageCapture.OutputFileResults.getSavedUri()
时应该会收到 null
。
我对 Glide 不够熟悉,所以我不确定为什么使用 Uri.fromFile()
加载图像不起作用。您可以尝试直接在 ImageView 上设置图像,看看您是否误用了 Glide:
capturedImage.setImageURI(Uri.fromFile(selfieFile))
我已经实现了 android camerax 但它无法正确捕获图像,回调 returns 成功但无法找到或查看图像。
我的实现:
private void startCamera(){
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(new Runnable() {
@Override
public void run() {
try {
ProcessCameraProvider provider = cameraProviderFuture.get();
bindPreview(provider);
} catch (Exception e){
e.printStackTrace();
}
}
}, ContextCompat.getMainExecutor(this));
}
private void bindPreview(@NonNull ProcessCameraProvider cameraProvider){
Preview preview = new Preview.Builder().build();
CameraSelector selector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_FRONT).build();
ImageCapture.Builder builder = new ImageCapture.Builder();
final ImageCapture imageCapture = builder.setTargetRotation(this.getWindowManager().getDefaultDisplay().getRotation()).build();
preview.setSurfaceProvider(cameraView.createSurfaceProvider());
cameraProvider.bindToLifecycle(this,selector,preview,imageCapture);
btnCapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selfieFile = new File(getFilesDir(),"attendance.jpg");
OutputFileOptions outputFileOptions = new OutputFileOptions.Builder(selfieFile).build();
imageCapture.takePicture(outputFileOptions, executor, new ImageCapture.OnImageSavedCallback() {
@Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Glide.with(CameraActivity.this).load(outputFileResults.getSavedUri()).into(capturedImage);
//updateUI(false);
}
});
}
@Override
public void onError(@NonNull ImageCaptureException exception) {
}
});
}
});
}
我已经尝试加载 Uri.fromFile() 并尝试获取 imageproxy 并转换为位图,但仍然有这些问题。任何帮助将不胜感激...
根据 ImageCapture.OutputFileResults.getSavedUri()
的文档,当 ImageCapture.OutputFileOptions
由 MediaStore 支持时,此方法仅 returns 一个值,即当您使用 ImageCapture.OutputFileOptions.Builder(ContentResolver, Uri, ContentValues)
.
您正在使用 ImageCapture.OutputFileOptions.Builder(File)
,这意味着您在调用 ImageCapture.OutputFileResults.getSavedUri()
时应该会收到 null
。
我对 Glide 不够熟悉,所以我不确定为什么使用 Uri.fromFile()
加载图像不起作用。您可以尝试直接在 ImageView 上设置图像,看看您是否误用了 Glide:
capturedImage.setImageURI(Uri.fromFile(selfieFile))