简体   繁体   English

为什么 CameraX 无法在 Android Studio 中将图片放大到我需要的大小?

[英]Why can't CameraX zoom in a picture as larger as I need in Android Studio?

I'm learning CameraX, and CameraXBasic is a sample code.我正在学习CameraX, CameraXBasic是一个示例代码。

I write a zoomX function based CameraFragment.kt .我写了一个基于 ZoomX function 的CameraFragment.kt you can see the Code A. I think the function can zoom in a picture any time.你可以看到代码A。我认为function可以随时放大图片。

I find that a picture can be zoom in when I invoke it with sa small value, such as zoomX(2f) , zoomX(3f) , but the picture will not be zoom in again when I use a big value such as zoomX(6.0f) , zoomX(7.0f) ... why?我发现当我用 sa 小值调用它时可以放大图片,例如zoomX(2f)zoomX(3f) ,但是当我使用大值例如zoomX(6.0f) ) 时,图片不会再次放大zoomX(6.0f) , zoomX(7.0f) ... 为什么?

Code A代码 A

   private lateinit var viewFinder: TextureView 
    private var preview: Preview? = null

    fun zoomX(orign: Float ){       
        val x=orign+1

        val singleWidth=viewFinder.width/x
        val singleHeight=viewFinder.height/x

        val left=viewFinder.width/2f-singleWidth/2f
        val right=left+singleWidth
        val top=viewFinder.height/2f-singleHeight/2f
        val bottom=top+singleHeight

        val my= Rect(left.toInt(), top.toInt(), right.toInt(), bottom.toInt())
        preview?.zoom(my)      
    }

This is not a matter of tradition, but of Futures .这不是传统的问题,而是Futures的问题。 Updating that CameraXBasic example from 1.0.0-alpha06 to 1.0.0-alpha08 breaks a lot, but CameraX CameraControl features these two methods:CameraXBasic示例从1.0.0-alpha06更新到1.0.0-alpha08会破坏很多,但 CameraX CameraControl具有以下两种方法:

Sets current zoom by a linear zoom value ranging from 0f to 1.0f .通过范围从0f1.0f的线性缩放值设置当前缩放。

linearZoom 0f represents the minimum zoom while linearZoom 1.0f represents the maximum zoom. linearZoom 0f代表最小缩放,而linearZoom 1.0f代表最大缩放。 The advantage of linearZoom is that it ensures the field of view (FOV) varies linearly with the linearZoom value, for use with slider UI elements (while setZoomRatio(float) works well for pinch-zoom gestures). linearZoom的优势在于它确保视野 (FOV) 随linearZoom值线性变化,适用于 slider UI 元素(而setZoomRatio(float)适用于捏缩放手势)。

Sets current zoom by ratio.按比例设置当前缩放。

It modifies both current zoomRatio and linearZoom so if apps are observing zoomRatio or linearZoom , they will get the update as well.它同时修改了当前的zoomRatiolinearZoom ,因此如果应用程序正在观察zoomRatiolinearZoom ,它们也会得到更新。 If the ratio is smaller than CameraInfo.getMinZoomRatio() or larger than CameraInfo.getMaxZoomRatio() , the returned ListenableFuture will fail with IllegalArgumentException and it won't modify current zoom ratio.如果比率小于CameraInfo.getMinZoomRatio()或大于CameraInfo.getMaxZoomRatio() ,则返回的ListenableFuture将失败并出现IllegalArgumentException并且不会修改当前缩放比率。 It is the applications' duty to clamp the ratio.限制比率是应用程序的职责。

Also see Executor and there's also CameraXExecutors .另请参阅Executor并且还有CameraXExecutors


Also see the release notes or the commits ...for all the API changes, which break the CameraXBasic example.另请参阅 发行说明提交...对于所有 API 更改,这些更改破坏了 CameraXBasic 示例。 I won't explain any more of these API differences (since this wasn't the question), but have forked it;我不会再解释这些 API 差异(因为这不是问题),但已经分叉了; see issues #131 (so far, at least the preview works there).请参阅问题 #131 (到目前为止,至少可以在那里进行预览)。


This is how it actually works:这就是它的实际工作方式:

val camera: Camera = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview)
val control: CameraControl = camera.cameraControl
control.setZoomRatio(5.0f)

Zoom ratios from 1.0f until 8.0f work on my Motorola XT1900:1.0f8.0f的缩放比例适用于我的摩托罗拉 XT1900:

val info: CameraInfo = camera.cameraInfo
val cameraId = (info as Camera2CameraInfoImpl).cameraId
val zoomRatio = info.getZoomRatio().value
val maxZoomRatio = info.getMaxZoomRatio().value
val minZoomRatio = info.getMinZoomRatio().value
val linearZoom = info.getLinearZoom().value

It's a Android tradition, zoom level exist between 0 and 1 like many other values..这是 Android 的传统,缩放级别像许多其他值一样存在于01之间。

0 mean the lowest zoom level. 0表示最低缩放级别。

1 mean the maximum zoom level. 1表示最大缩放级别。

It's a float value so you increase it like 0.1 , 0.2 , 0.9 and so on.这是一个浮点值,因此您可以像0.10.20.9等一样增加它。

First of all, find out the max zoom value of the camera - using getMaxZoom() method.首先,找出相机的最大缩放值 - 使用getMaxZoom()方法。

https://developer.android.com/reference/android/hardware/Camera.Parameters.html#getMaxZoom() https://developer.android.com/reference/android/hardware/Camera.Parameters.html#getMaxZoom()

If you have used Camera Preview then you can set zoom in Camera preview using Rect .如果您使用过 Camera Preview,那么您可以使用Rect在 Camera preview 中设置缩放。 Like below -如下图——

PreviewConfig config = new PreviewConfig.Builder().build();
    Preview preview = new Preview(config);

    Rect rect = Rect(left, top, right, bottom);
    preview.zoom(rect)

    preview.setOnPreviewOutputUpdateListener(
        new Preview.OnPreviewOutputUpdateListener() {
            @Override
            public void onUpdated(Preview.PreviewOutput previewOutput) {
                // Your code here. For example, use previewOutput.getSurfaceTexture()
                // and post to a GL renderer.
            };
    });

    CameraX.bindToLifecycle((LifecycleOwner) this, preview);

You may get more details from here - Android Camera preview zoom using double finger touch您可以从这里获得更多详细信息 - Android 使用双指触摸的相机预览缩放

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

相关问题 如何在 Android Studio 中使用 CameraX API 冻结相机图片? - How can I freeze picture of camera with CameraX API in Android Studio? Android studio 如何在 CameraX 中添加设置缩放 function - Android studio How to add set Zoom function in CameraX 是否可以在没有任何交互的情况下拍照? (Android Studio、CameraX) - Is it possible to take a picture without any interaction? (Android Studio, CameraX) Android camerax 把图片变成变量 - Android camerax take picture into variable Android Studio CameraX 模块 - Android Studio CameraX Module 我需要在网络视图中使用图片进行缩放 - I need to use a picture in a webview to zoom it 如何在 Android 中冻结 cameraX 预览? - how can I freeze cameraX preview in Android? 我可以使用 CameraX (Android Jetpack) 录制视频吗? - Can I record video with CameraX (Android Jetpack)? 如何捕获多个图像并将图像发送到下一个活动并使用 CameraX [Android Studio] 显示它们 - How Can I Capture Multiple Images and send the images to next Activity and display them using CameraX [Android Studio] 如果图片是使用 android 的相机应用程序拍摄的,为什么我无法在我的 android 应用程序中上传图片库中的图片? - Why can't I upload a picture from gallery in my android app, if the picture was taken with the camera app from android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM