标签: android-camerax

如何使用CameraX设置正确的纵横比?

我正在使用CameraX代码实验室,即使使用setTargetAspectRatiosetTargetResolution方法,预览中的纵横比也错误。

private fun startCamera() {
    // Create configuration object for the viewfinder use case
    val previewConfig = PreviewConfig.Builder().apply {
        setTargetAspectRatio(Rational(1, 1))
        setTargetResolution(Size(640, 640))
    }.build()
    ...
Run Code Online (Sandbox Code Playgroud)

布局使用的是硬编码的大小,如代码实验室中所示。

<TextureView
    android:id="@+id/view_finder"
    android:layout_width="640px"
    android:layout_height="640px"
    ...
Run Code Online (Sandbox Code Playgroud)

如果库具有CameraTextureView和属性android:scaleType(类似于的现有属性ImageView),可以将预览调整为预览大小,那就太好了。

CameraX错误的宽高比

android android-jetpack android-camerax

7
推荐指数
1
解决办法
1750
查看次数

Android:如何使用 CameraX 裁剪图像?

我想拍摄一张照片并使用 CameraX 从中心裁剪出一个 25x25 dp 的正方形。我读过使用 ImageCapture 可以进行裁剪,但不幸的是到目前为止几乎没有类似的例子。

val imageCaptureConfig = ImageCaptureConfig.Builder().apply {
    setTargetAspectRatio(Rational(1, 1))
    setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
}.build()

val imageCapture = ImageCapture(imageCaptureConfig)
btn_take_photo.setOnClickListener {
    imageCapture.takePicture(
        object : ImageCapture.OnImageCapturedListener() {
            override fun onCaptureSuccess(image: ImageProxy?, rotationDegrees: Int) {
                super.onCaptureSuccess(image, rotationDegrees)

                // image manipulation here?
            }
        }
    )
}
Run Code Online (Sandbox Code Playgroud)

android android-camerax

7
推荐指数
3
解决办法
8800
查看次数

如何在 Android 中使用 CameraX 显示负片模式预览?

我正在学习CameraX API,CameraXBasic是office 示例代码。

CameraXBasic项目中的CameraFragment.kt显示真实的相机预览。

现在我希望显示负模式预览。如何使用 CameraX API?有示例代码吗?

相机片段.kt

private lateinit var viewFinder: TextureView

private fun bindCameraUseCases() {
    // Get screen metrics used to setup camera for full screen resolution
    val metrics = DisplayMetrics().also { viewFinder.display.getRealMetrics(it) }
    val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)
    Log.d(TAG, "Screen metrics: ${metrics.widthPixels} x ${metrics.heightPixels}")

    // Set up the view finder use case to display camera preview
    val viewFinderConfig = PreviewConfig.Builder().apply {
        setLensFacing(lensFacing)
        // We request aspect ratio but no resolution to let …
Run Code Online (Sandbox Code Playgroud)

android android-camerax

7
推荐指数
1
解决办法
1176
查看次数

Camerax 正在运行。如何在前台服务中获取生命周期所有者或在没有它的情况下运行?

用例:是我必须在没有任何活动或片段的服务中在后台运行相机

拦截器:新的camerax 会话绑定到生命周期所有者,但该服务没有任何。那么如何获取此对象或在没有它的情况下运行?

已经尝试过:我可以用 Camera2 库做同样的事情,但我想知道是否可以用 Camerax 也因为 Camera2 api 将来可能会被弃用。或者谷歌故意阻止用户在没有活动的情况下运行相机?

请建议

android background-service android-camera2 android-camerax

7
推荐指数
1
解决办法
1155
查看次数

CameraX - 无法配置相机

我想实现一个自定义视图,它将使用 Camera X API 显示实时预览,但我坚持Camera X...的配置

基于CameraX 示例代码,我尝试实现我的自定义视图,但只出现黑屏并且我的日志显示:

E/Camera:无法配置camera 1,超时!

这是我的活动布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBlack"
    tools:context=".MainActivity">

    <com.component.LiveView
        android:id="@+id/live"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:background="@android:color/black" />

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

以及与我的组件相关的代码

override fun onResume() {
    super.onResume()
    when {
        ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED ->
            // Start camera preview
            live.start(this)
        ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA) -> ConfirmationDialogFragment.newInstance(R.string.camera_permission_confirmation,
                arrayOf(Manifest.permission.CAMERA),
                REQUEST_CAMERA_PERMISSION,
                R.string.camera_permission_not_granted)
                .show(supportFragmentManager, FRAGMENT_DIALOG)
        else -> ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA),
                REQUEST_CAMERA_PERMISSION)
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我的Live组件是这样的

class LiveView(context: Context, attrs: AttributeSet) : CameraSourcePreview(context, attrs) { …
Run Code Online (Sandbox Code Playgroud)

android android-camerax

7
推荐指数
1
解决办法
1965
查看次数

如何降低 Android CameraX ImageAnalysis 的帧速率?

如何在图像分析中将帧速率降低到 1 fps,这样我就不会多次检测到条形码。在我的用例中,以 1 秒的间隔多次扫描相同的条形码应该会增加一个计数器。所以我希望它能够正常工作。(类似于商店收银台的产品扫描仪)

cameraX版本:1.0.0-beta02

类似问题:

目前的实施:

https://proandroiddev.com/update-android-camerax-4a44c3e4cdcc
按照此文档,限制图像分析。

override fun analyze(image: ImageProxy) {
    val currentTimestamp = System.currentTimeMillis()
    if (currentTimestamp - lastAnalyzedTimestamp >= TimeUnit.SECONDS.toMillis(1)) {
        // Image analysis code
    }
    image.close()
}
Run Code Online (Sandbox Code Playgroud)

更好的解决方案会有所帮助。

android android-camerax

7
推荐指数
1
解决办法
3134
查看次数

为什么 FirebaseVisionImage.fromMediaImage() 会产生 OutOfMemoryError

构建CameraX,调用analyze() 方法并传递图像,然后使用close() 方法关闭(删除)。从此图像 FirebaseVisionImage 被创建并传递以进行处理(文本识别)。代码示例和代码实验室不同,不使用 CameraX 或使用旧 API 版本实现 TextRecognition。

堆栈跟踪

  override fun analyze(imageProxy: ImageProxy) {
    if (isValidText) {
        imageProxy.close()
        return
    }
    val mediaImage = imageProxy.image // requires annotation
    val degrees = imageProxy.imageInfo.rotationDegrees
    val rotation = rotationDegreesToFirebaseRotation(degrees)
    if (mediaImage != null) {
        runTextRecognition(mediaImage, rotation)  // line 44
    }
    imageProxy.close()
} 


private fun runTextRecognition(mediaImage: Image, rotation: Int) {
    // Create FirebaseVisionImage from frame
    val visionImage = FirebaseVisionImage.fromMediaImage(mediaImage, rotation) // line 64
    val recognizer = FirebaseVision.getInstance()
        .onDeviceTextRecognizer
    recognizer.processImage(visionImage)
        .addOnSuccessListener { texts ->
            processTextRecognitionResult(texts!!, …
Run Code Online (Sandbox Code Playgroud)

ocr out-of-memory text-recognition firebase-mlkit android-camerax

7
推荐指数
1
解决办法
753
查看次数

CameraX:将照片捕捉为位图

我正在试验 Google 的CameraX示例应用程序(CameraXBasic,可以在 Github 上找到),并希望将图像捕获为位图,以便能够在保存图像之前对图像进行一些修改。有没有人有关于如何实现这一目标的建议?

请参阅下面的谷歌原始代码来捕获和保存图像:

// Listener for button used to capture photo
        controls.findViewById<ImageButton>(R.id.camera_capture_button).setOnClickListener {

            // Get a stable reference of the modifiable image capture use case
            imageCapture?.let { imageCapture ->

                // Create output file to hold the image
                val photoFile = createFile(outputDirectory, FILENAME, PHOTO_EXTENSION)

                    // Create output options object which contains file + metadata
                    val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile)
                            .build()

                    // Setup image capture listener which is triggered after photo has been taken
                    imageCapture.takePicture(
                            outputOptions, cameraExecutor, object …
Run Code Online (Sandbox Code Playgroud)

android image-capture bitmap kotlin android-camerax

7
推荐指数
1
解决办法
3404
查看次数

如何根据预览显示上的矩形裁剪 CameraX 捕获的图像

你好,我是android开发新手,谁能帮我根据PreviewView上的矩形裁剪CameraX捕获的图像?

这是activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/frameRoot"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

        <View
            android:id="@+id/viewOverlay_center"
            android:background="@drawable/card_rectangle"
            android:layout_width="270dp"
            android:layout_height="400dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/ll_bottom"/>

        <LinearLayout
            android:id="@+id/ll_bottom"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/black"
            android:gravity="center"
            android:orientation="horizontal"
            android:padding="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent">

            <ImageView
                android:id="@+id/image_capture_button"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:background="?android:selectableItemBackground"
                android:src="@drawable/shutter_camera" />
        </LinearLayout>
        
    </androidx.constraintlayout.widget.ConstraintLayout>

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

这是我的MainActivity.java:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(viewBinding.getRoot());

        viewBinding.imageCaptureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                capturePhoto();
            }
        });
    }

    private void capturePhoto(){
        if (imageCapture == null) …
Run Code Online (Sandbox Code Playgroud)

android android-studio android-camerax

6
推荐指数
1
解决办法
3631
查看次数

如何使用 Jetpack Compose 将相机预览缩放到半透明背景中的任何给定尺寸或形状

我有一个带有半透明背景的对话框片段。我想以圆形或方形的形式在此对话框片段上放置相机预览。

设置比例类型为 时PreviewView.ScaleType.FIT_CENTER,预览如下:

带黑色边框的相机预览

而如果我将比例类型设置为PreviewView.ScaleType.FILL_CENTER,则预览如下:

相机预览溢出超出设定边界

以下代码是我用于相机预览可组合项的代码:

@Composable
private fun CameraPreview(
    modifier: Modifier = Modifier,
    scaleType: PreviewView.ScaleType = PreviewView.ScaleType.FILL_CENTER,
    onUseCase: (UseCase) -> Unit
) {
    AndroidView(
        modifier = modifier,
        factory = { context ->
            val previewView = PreviewView(context).apply {
                this.scaleType = scaleType
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
            }
            onUseCase(
                Preview.Builder()
                    .build()
                    .also {
                        it.setSurfaceProvider(previewView.surfaceProvider)
                    }
            )
            previewView
        }
    )
}
Run Code Online (Sandbox Code Playgroud)

我尝试为布局参数设置整数值,但这似乎没有帮助。我怎样才能设置相机预览,使其适合边界,并且不会超出边界或在边界周围留下黑色边框?

这是供参考的图像,它显示了我想要实现的目标:

我想要实现的目标的图片

android kotlin android-camerax android-jetpack-compose

6
推荐指数
1
解决办法
1243
查看次数