简体   繁体   English

Android studio 如何在 CameraX 中添加设置缩放 function

[英]Android studio How to add set Zoom function in CameraX

I find a source code to use CameraX in Android studio.我在 Android 工作室中找到了使用 CameraX 的源代码。 But it doesn't work well because it doesn't have focus function so my photo always out of focus.但它效果不佳,因为它没有焦点 function 所以我的照片总是失焦。 I have searched on the Internet, but since I just learnt Android Studio for a few days, so I dont really understand how to convert their code to my code.我在网上搜索过,但是由于我这几天才学习Android Studio,所以不太明白如何将他们的代码转换成我的代码。 To sum up, I want to add Zoom in and out function but I dont know how to do, anyone could give me a hand?总结一下,我想添加放大和缩小 function 但我不知道该怎么做,有人可以帮我吗? Please~请~

public abstract class AbstractCameraXActivity<R> extends BaseModuleActivity {
private static final int REQUEST_CODE_CAMERA_PERMISSION = 200;
private static final String[] PERMISSIONS = {Manifest.permission.CAMERA};
private long mLastAnalysisResultTime;
protected abstract int getContentViewLayoutId();
protected abstract TextureView getCameraPreviewTextureView();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getContentViewLayoutId());

    startBackgroundThread();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
        != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(
            this,
            PERMISSIONS,
            REQUEST_CODE_CAMERA_PERMISSION);
    } else {
        setupCameraX();
    }
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_CODE_CAMERA_PERMISSION) {
        if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
            Toast.makeText(
                this,
                "You can't use object detection example without granting CAMERA permission",
                Toast.LENGTH_LONG)
                .show();
            finish();
        } else {
            setupCameraX();
        }
    }
}
private void setupCameraX() {
    final TextureView textureView = getCameraPreviewTextureView();
    final PreviewConfig previewConfig = new PreviewConfig.Builder().build();
    final Preview preview = new Preview(previewConfig);
    preview.setOnPreviewOutputUpdateListener(output -> textureView.setSurfaceTexture(output.getSurfaceTexture()));


    final ImageAnalysisConfig imageAnalysisConfig =
        new ImageAnalysisConfig.Builder().setTargetResolution(new Size(1024, 1024))
            .setCallbackHandler(mBackgroundHandler)
            .setCallbackHandler(mBackgroundHandler)
            .setImageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
                .build();
    final ImageAnalysis imageAnalysis = new ImageAnalysis(imageAnalysisConfig);
    imageAnalysis.setAnalyzer((image, rotationDegrees) -> {
        if (SystemClock.elapsedRealtime() - mLastAnalysisResultTime < 500) {
            return;
        }

        final R result = analyzeImage(image, rotationDegrees);
        if (result != null) {
            mLastAnalysisResultTime = SystemClock.elapsedRealtime();
            runOnUiThread(() -> applyToUiAnalyzeImageResult(result));
        }
    });
    CameraX.bindToLifecycle(this, preview, imageAnalysis);
}
protected abstract R analyzeImage(ImageProxy image, int rotationDegrees);
protected abstract void applyToUiAnalyzeImageResult(R result);

} ''' } '''

I would recommend using the PreviewView / CameraController API.我建议使用PreviewView / CameraController API。 It's a high level API that takes care of many low level details for you, including zooming/focusing.它是一个高级 API,可以为您处理许多低级细节,包括缩放/对焦。 Sample code:示例代码:

PreviewView previewView = view.findViewById(R.id.preview_view);
LifecycleCameraController cameraController = new LifecycleCameraController(requireContext());

previewView.setController(cameraController);
cameraController.bindToLifecycle(getViewLifecycleOwner())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM