【问题标题】:CameraX: How to set background of PreviewView?CameraX:如何设置 PreviewView 的背景?
【发布时间】:2021-08-30 06:03:20
【问题描述】:

默认情况下,在翻转相机时,屏幕会空白一段时间,直到相机启动。我想稍微改变一下这种行为。

我希望最后一帧模糊并设置,直到创建新的绑定相机实例。我尝试将getBitmap()BitmapDrawablesetBackground() 一起使用,但这似乎不起作用(屏幕仍然是空白的)。

代码:

    void startCamera(final boolean forced){

        if(!forced && camera!=null) return;

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

        CameraSelector cameraSelector = new CameraSelector.Builder()
                .requireLensFacing(this.cameraSelector)
                .build();

        ImageAnalysis imageAnalysis = new ImageAnalysis.Builder()
                .build();

        ImageCapture.Builder builder = new ImageCapture.Builder();

        final ImageCapture imageCapture = builder
                .setTargetRotation(this.getWindowManager().getDefaultDisplay().getRotation())
                .build();

        Bitmap b = mPreviewView.getBitmap();

        preview.setSurfaceProvider(mPreviewView.getSurfaceProvider());

        mPreviewView.setBackground(new BitmapDrawable(getResources(), b));

        // Unbind/close all other camera(s) [if any]
        cameraProvider.unbindAll();

        // Get a camera instance bound to the lifecycle of this activity
        camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageAnalysis, imageCapture);

        // Focus camera on touch/tap
        mPreviewView.setOnTouchListener(this);

        start_auto_focus();
    }

位图模糊功能

    private Bitmap blurRenderScript(Bitmap smallBitmap, int radius) {

        final float defaultBitmapScale = 0.1f;

        int width  = Math.round(smallBitmap.getWidth() * defaultBitmapScale);
        int height = Math.round(smallBitmap.getHeight() * defaultBitmapScale);

        Bitmap inputBitmap  = Bitmap.createScaledBitmap(smallBitmap, width, height, false);
        Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

        RenderScript renderScript = RenderScript.create(this);
        ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
        Allocation tmpIn = Allocation.createFromBitmap(renderScript, inputBitmap);
        Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap);
        theIntrinsic.setRadius(radius);
        theIntrinsic.setInput(tmpIn);
        theIntrinsic.forEach(tmpOut);
        tmpOut.copyTo(outputBitmap);

        return outputBitmap;
    }

有没有办法覆盖空白屏幕行为?

【问题讨论】:

  • 这很奇怪,可能是您使用的设备问题。因为我现在使用 CameraX 有一段时间了,我从来没有遇到过这个问题。而且翻转几乎是瞬间的。可能是你的代码不好。
  • 哦...也许我没有正确完成...我在答案中分享了代码。
  • 你能在翻转相机的同时分享一个gif吗
  • 好的,请给我2分钟
  • 我有一个 Pixel 设备..所以我不认为设备是问题

标签: android android-camera drawable android-camerax


【解决方案1】:

我刚刚解决了您的问题,为此使用this BlurImageView 作为预览覆盖,使用值动画器自动更改模糊级别,并在预览可见/不可见时使用 PreviewView 回调。

设置 ValueAnimator:(用于自动更改渐进式模糊动画的模糊级别)

// Initializing the preview animator with value to blur
val previewAnimator = ValueAnimator.ofInt(6, 12, 18, 24, 25)
// Setting animation duration
previewAnimator.duration = 2000
// Adding listener for every value update of the animation
previewAnimator.addUpdateListener { animator ->
    // Blurring the preview when it is not visible
    blurOverlayImageView.setBlur(animator.animatedValue as Int)
}
// Adding action that is invoked on animation start
previewAnimator.doOnStart {
    // Setting the current preview frame as the canvas bitmap
    blurOverlayImageView.setImageBitmap(previewView.bitmap)
}
// Adding action that is invoked on animation cancel
previewAnimator.doOnCancel {
    // Removing the blurred preview to reveal the current actual preview
    blurOverlayImageView.setImageBitmap(null)
}

*动画的持续时间是2秒,因为2秒比重新开始预览的时间要长,所以这样动画在完成之前可以取消,因为完成之后就不能取消了取消,所以删除最后一张预览图的回调不会触发

*在每次重启相机之前启动 previewAnimator

*blurOverlayImageView - 预览叠加模糊图像视图

预览可见性回调:

查看answer我在 Android CameraX 讨论组中的问题,了解如何为预览可见性设置回调 - 使用:previewView.previewStreamState.observe(previewObserver)

*previewObserver - 用于观察 PreviewView.StreamState 变化的 Observer 对象

【讨论】:

  • 非常感谢您的帮助!问题已经解决了。我在问题的描述中添加了模糊位图的功能,并且在取消绑定相机之前拍摄了最后一帧(位图)(尽管可以在稍后阶段拍摄)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-05
相关资源
最近更新 更多