android - 为什么CameraX预览在真机上模糊不清,怎么对焦?
问题描述
现在,我使用 CameraX 制作了自己的相机。预览工作正常,图像正在保存。然而,预览是模糊的,几乎没有任何关于如何自动对焦甚至手动对焦的文档。因为我使用 Firebase 的机器学习工具包来识别文本,所以我的应用程序非常清晰非常重要。
private void startCamera() {
CameraX.unbindAll();
Rational aspectRatio = new Rational (textureView.getWidth(), textureView.getHeight());
Size screen = new Size(textureView.getWidth(), textureView.getHeight()); //size of the screen
PreviewConfig pConfig = new PreviewConfig.Builder().setTargetAspectRatio(aspectRatio).setTargetResolution(screen).setTargetRotation(Surface.ROTATION_0).build();
preview = new Preview(pConfig);
preview.setOnPreviewOutputUpdateListener(
new Preview.OnPreviewOutputUpdateListener() {
//to update the surface texture we have to destroy it first then re-add it
@Override
public void onUpdated(Preview.PreviewOutput output){
ViewGroup parent = (ViewGroup) textureView.getParent();
parent.removeView(textureView);
parent.addView(textureView, 0);
textureView.setSurfaceTexture(output.getSurfaceTexture());
updateTransform();
}
});
ImageCaptureConfig imageCaptureConfig = new ImageCaptureConfig.Builder().setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
.setTargetRotation(getWindowManager().getDefaultDisplay().getRotation()).build();
imgCap = new ImageCapture(imageCaptureConfig);
findViewById(R.id.imgCapture).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cameraImage.setImageResource(R.drawable.cameraon);
CameraX.unbind(preview);
loadingDialog.startLoadingDialog();
file = new File(Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".png");
imgCap.takePicture(file, new ImageCapture.OnImageSavedListener() {
@Override
public void onImageSaved(@NonNull File file) {
String filePath = file.getPath();
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
rotateImage(bitmap);
}
@Override
public void onError(@NonNull ImageCapture.ImageCaptureError imageCaptureError, @NonNull String message, @Nullable Throwable cause) {
String msg = "Image Capture Failed : " + message;
Toast.makeText(getBaseContext(), msg,Toast.LENGTH_LONG).show();
if(cause != null){
cause.printStackTrace();
}
}
});
}
});
//bind to lifecycle:
CameraX.bindToLifecycle((LifecycleOwner)this, preview, imgCap);
}
解决方案
如果您可以分享一些代码示例,我认为回答这个问题会更有帮助。总的来说,这个CameraControl
类(链接到源代码)有两个相关的 API
ListenableFuture<FocusMeteringResult> startFocusAndMetering(FocusMeteringAction action);
ListenableFuture<Void> cancelFocusAndMetering();
我相信您可以使用startFocusMetering
相关参数来触发中心焦点或面部焦点(如果您有面部坐标)。现在,如果您在每次拍摄之前调用此函数或在循环中运行此函数以每 500 毫秒调用一次,它可能会解决您的问题。
Github查找后的一些代码参考:
推荐阅读
- javascript - 如何使 if 语句永久化?
- r - 在不单独创建 lm-obejct 的情况下将 p 值添加到 ggplot
- android - 隐藏键盘返回空指针
- java - char 是如何工作的,它与英文字母有什么关系?
- java - 如何从 Firebase 检索数据以在 TextView 中显示?
- react-native - react-native-fs:如何读取本地图片(获取本地文件的base64)
- python - django 从查询集值中区分查询集
- javascript - 将 Bing Map Autosuggest API 添加到表单中的多个字段
- python - Python 如何处理来自“其他地方”的对象
- c# - 在 C# 中减去整数时出现错误“无法将 int 隐式转换为 short”