标签: android-camerax

如何在 CameraX 上切换到前置摄像头?

我已按照此处的步骤进行 CameraX 设置,现在我正在尝试使前置摄像头按钮正常工作。

这是我的设置代码:

private lateinit var preview: Preview

private fun startCamera() {

    // Create configuration object for the viewfinder use case
    val previewConfig = PreviewConfig.Builder().apply {
        setLensFacing(CameraX.LensFacing.BACK)
    }.build()

    // Build the viewfinder use case
    preview = Preview(previewConfig)

    // Every time the viewfinder is updated, recompute layout
    preview.setOnPreviewOutputUpdateListener {

        // To update the SurfaceTexture, we have to remove it and re-add it
        val parent = viewFinder.parent as ViewGroup
        parent.removeView(viewFinder)
        parent.addView(viewFinder, 0)

        viewFinder.surfaceTexture = it.surfaceTexture
        updateTransform()
    } …
Run Code Online (Sandbox Code Playgroud)

android android-camerax

5
推荐指数
2
解决办法
7177
查看次数

图片已经关闭。CameraX 分析器

我正在尝试将 ProxyImage 从 cameraX 分析器转换为 Bitmap 以使用 Tensor Flow 光分析图像。所以我实现了cameraX Analyze回调,它将图像作为proxyImage。我需要转换为位图的那个 proxyImage。如果我在 UI 线程上进行此对话,则会导致相机预览滞后。所以我想用 Coroutines 来做这件事。现在的问题是,每当我将 proxyImage 传递给协程以将其转换为后台线程上的位图时,它就会因“图像已关闭”的 IllegalStateException 而崩溃。

08-04 16:28:59.690 16185-16185/com.example.camerax E/libEGL: call to OpenGL ES API with no current context (logged once per thread)
08-04 16:29:00.849 16185-16308/com.example.camerax E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
    Process: com.example.camerax, PID: 16185
    java.lang.IllegalStateException: Image is already closed
        at android.media.Image.throwISEIfImageIsInvalid(Image.java:68)
        at android.media.ImageReader$SurfaceImage$SurfacePlane.getBuffer(ImageReader.java:787)
        at androidx.camera.core.AndroidImageProxy$PlaneProxy.getBuffer(AndroidImageProxy.java:141)
        at com.example.camerax.MainActivity.getImageFromProxy(MainActivity.kt:216)
        at com.example.camerax.MainActivity.convertProxyImageToBitmap(MainActivity.kt:150)
        at com.example.camerax.MainActivity.access$convertProxyImageToBitmap(MainActivity.kt:38)
        at com.example.camerax.MainActivity$startCamera$3$1.invokeSuspend(MainActivity.kt:136)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
Run Code Online (Sandbox Code Playgroud)

我认为下一帧是引用前一帧,而应用程序在后台线程中将 proxyImage 转换为位图。我阅读了文档,他们说 …

android android-architecture-components android-jetpack kotlin-coroutines android-camerax

5
推荐指数
1
解决办法
2249
查看次数

如何使用 ZXing 和 android CameraX 来解码条码和二维码

如何集成 ZXing 库以将其与新的 Android Jetpack CameraX 一起使用

我知道我必须建立一个ImageAnalyzer并在其中使用ZXing来解码二维码条形码

android zxing kotlin android-camerax

5
推荐指数
1
解决办法
2680
查看次数

是否可以将 cameraX 或 Camera2 缩放到 Api 最大缩放限制之外

所以我正在尝试制作一个双筒望远镜/望远镜类的应用程序,但问题是我想放大到最大。我看到商店里有应用程序可以做到这一点,但不确定如何。我尝试过 cameraX linearZoom 方法,但它只能在 0f - 1f 之间的范围内工作,或者是否可以使用 Camera2 api 来做到这一点?任何帮助将不胜感激。请注意,我只是在尝试或研究了一整天不同的事情后才发布了这个问题。

谢谢

android android-camera2 android-camerax

5
推荐指数
1
解决办法
481
查看次数

Android CameraX 检索摄像机配置文件参数时出错

我正在迁移到 cameraX 并遇到异常

androidx.camera.core.InitializationException: java.lang.RuntimeException: Error retrieving camcorder profile params
Run Code Online (Sandbox Code Playgroud)

错误日志指出调用时抛出此错误

            cameraProvider = cameraProviderFuture.get()
Run Code Online (Sandbox Code Playgroud)

我无法在任何设备上重现此错误。

我在这里初始化相机:

private fun initCameraProvider() {
    val cameraProviderFuture = ProcessCameraProvider.getInstance(this) //Error is thrown here
    cameraProviderFuture.addListener(Runnable {
        cameraProvider = cameraProviderFuture.get()
        val metrics = DisplayMetrics().also {
            previewView?.display?.getRealMetrics(it) 
        }
        val screenAspectRatio = ImageUtils.aspectRatio(metrics.widthPixels, metrics.heightPixels)
        val rotation = previewView?.display?.rotation ?: 0

        preview = Preview.Builder()
                .setTargetAspectRatio(screenAspectRatio)
                .setTargetRotation(rotation)
                .build()
        analyzer = BarcodeAnalyzer(previewView, overlay) { onSuccess(it) }
        val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA

        imageAnalyzer = ImageAnalysis.Builder()
                .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
                .setTargetAspectRatio(screenAspectRatio)
                .build()
                .also {
                    it.setAnalyzer(cameraExecutor, analyzer)
                }


        val …
Run Code Online (Sandbox Code Playgroud)

android android-camera androidx android-camerax

5
推荐指数
1
解决办法
585
查看次数

如何在 jetpack compose 中使用 PreviewView?

我试图用一个CameraXPreviewView一个内部Composable通过AndroidView,但预览拉伸和右半边被裁剪,你可以在看截图。尽管如此,该视图似乎占据了正确的空间。此问题仅在纵向模式下发生。我能够在两部手机和模拟器中重现这个问题,所以我怀疑它是特定于硬件的。

我尝试了不同的比例类型,但这似乎不会影响拉伸。我会报告一个错误,但我不确定这是 compose 还是 cameraX 中的错误。

请参阅我在下面使用的代码:

package com.example.camerapreview

import android.Manifest
import android.graphics.Color
import android.os.Bundle
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.widget.LinearLayout
import androidx.activity.ComponentActivity
import androidx.activity.compose.registerForActivityResult
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.camera.core.AspectRatio.RATIO_16_9
import androidx.camera.core.CameraSelector
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.camera.view.PreviewView
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner
import com.example.pointergun.ui.theme.PointerGunTheme
import com.google.common.util.concurrent.ListenableFuture
import androidx.camera.core.Preview

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val cameraProviderFuture …
Run Code Online (Sandbox Code Playgroud)

android android-camerax android-jetpack-compose

5
推荐指数
1
解决办法
346
查看次数

ImageAnalyzer ML Kit 边界框错误排列

我有一个简单的布局:

 <RelativeLayout
    android:id="@+id/myP"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <androidx.camera.view.PreviewView
        android:id="@+id/mPreviewView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <com.example.dochjavatestimplementation.pkgActivity.ExtendedImageView
        android:id="@+id/imageViewOmgShowIt"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

previewView 用于显示相机,ExtendedImageView 用于显示显示找到的对象 rect。

我的图像分析器:

imageAnalysis = new ImageAnalysis.Builder()
 .setTargetResolution(new Size(mPreviewView.getWidth(),mPreviewView.getHeight()))
 .build();

imageAnalysis.setAnalyzer(executor, new PaperImageAnalyser());
Run Code Online (Sandbox Code Playgroud)

纸图像分析器:

public class PaperImageAnalyser implements ImageAnalysis.Analyzer {


ObjectDetectorOptions options = new ObjectDetectorOptions.Builder()
    .setDetectorMode(ObjectDetectorOptions.STREAM_MODE)
    .enableClassification()  // Optional
    .build();

ObjectDetector objectDetector = ObjectDetection.getClient(options);



@Override
public void analyze(@NonNull ImageProxy imageProxy) {
@SuppressLint("UnsafeExperimentalUsageError") Image mediaImage = imageProxy.getImage();

if (mediaImage != null) {
    InputImage image =InputImage.fromMediaImage(mediaImage, imageProxy.getImageInfo().getRotationDegrees());

    objectDetector.process(image)
        .addOnSuccessListener(
            new OnSuccessListener<List<DetectedObject>>() {
                @Override …
Run Code Online (Sandbox Code Playgroud)

android image-processing android-imageview android-camerax google-mlkit

5
推荐指数
1
解决办法
74
查看次数

用于录制视频的 CameraX 和 MediaRecorder

我尝试在这里使用 CameraX PreviewView

            <androidx.camera.view.PreviewView
                android:id="@+id/camPreviewView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
Run Code Online (Sandbox Code Playgroud)

我可以使用 MediaProjection 为整个屏幕录制视频。

但是现在,我只需要记录相机视图。我尝试使用 MediaRecorder 中的 setPreviewDisplay,但由于我使用的是 PreviewView,我不知道如何获取表面。

我也尝试使用 MediaRecorder 的 setCamera,但它已被弃用。它建议使用 getSurface 代替。

有没有将 MediaRecorder 与 CameraX PreviewView 一起使用的示例?

CameraX 有 VideoCapture,但由于某种原因,我无法使用它。我需要使用 MediaRecorder 和 CameraX PreviewView 来录制文件。

有什么建议吗?

android android-mediarecorder android-camerax

5
推荐指数
0
解决办法
157
查看次数

如何为 android Camera x androidx.camera.core.Preview 实现自定义 SurfaceProvider

我正在关注 Android 的原始 CameraX 示例:https : //developer.android.com/codelabs/camerax-getting-started#5 和这里:https : //github.com/android/camera-samples/blob/ main/CameraXBasic/app/src/main/java/com/android/example/cameraxbasic/fragments/CameraFragment.kt#L337

我想像示例中那样实例化我的预览:

       val preview = Preview.Builder()
           .build()
           .also {
               it.setSurfaceProvider(viewFinder.surfaceProvider)
           }
Run Code Online (Sandbox Code Playgroud)

但后来我意识到......好吧,多么令人惊喜-viewFinder / androidx.camera.view.PreviewView来自一个只是 alpha 版本的库

    implementation 'androidx.camera:camera-view:1.0.0-alpha24'
Run Code Online (Sandbox Code Playgroud)

(见这里) https://github.com/android/camera-samples/blob/main/CameraXBasic/app/build.gradle

我当然不想使用 alpha 版本!所以我想好吧也许我可以实现我自己的SurfaceProvider

文档表明我可以做这样的事情:

         * class MyGlSurfaceProvider implements Preview.SurfaceProvider {
         *     // This executor must have also been used with Preview.setSurfaceProvider() to
         *     // ensure onSurfaceRequested() is called on our GL thread.
         *     Executor mGlExecutor;
         *
         *     {@literal @}Override
         *     public void onSurfaceRequested(@NonNull SurfaceRequest request) …
Run Code Online (Sandbox Code Playgroud)

android preview android-camera androidx android-camerax

5
推荐指数
0
解决办法
166
查看次数

如何更改 android 中的 whiteBalance 增益值?

我想获取当前增益的值并更改 RGB 增益的值。

在 iOS 中,Apple提供了 setWhiteBalanceModeLockedWithDeviceWhiteBalanceGains:completionHandler.

- (void)setWhiteBalanceGains:(AVCaptureWhiteBalanceGains)gains
{
  NSError *error = nil;
  
  if ( [self.captureDevice lockForConfiguration:&error] ) {
    AVCaptureWhiteBalanceGains normalizedGains = [self normalizedGains:gains];
    [self.captureDevice setWhiteBalanceModeLockedWithDeviceWhiteBalanceGains:normalizedGains completionHandler:nil];
    [self.captureDevice unlockForConfiguration];
  }
  else {
    NSLog( @"Could not lock device for configuration: %@", error );
  }
}

- (AVCaptureWhiteBalanceGains)normalizedGains:(AVCaptureWhiteBalanceGains) g
{
  AVCaptureWhiteBalanceGains gains = g;
  gains.redGain = MAX(gains.redGain, 1.0f);
  gains.greenGain = MAX(gains.greenGain, 3.0f);
  gains.blueGain = MAX(gains.blueGain, 18.0f);
  
  return gains;
}
Run Code Online (Sandbox Code Playgroud)

我们如何使用cameraX在android中实现这一点?

COLOR_CORRECTION_GAINS

COLOR_CORRECTION_MODE

我已经检查了有关通道控制的文档。但是我们如何使用新控件更改色彩校正并重置 cameraX 预览?

android android-camera2 android-camerax

5
推荐指数
1
解决办法
120
查看次数