【发布时间】:2021-08-30 06:03:20
【问题描述】:
默认情况下,在翻转相机时,屏幕会空白一段时间,直到相机启动。我想稍微改变一下这种行为。
我希望最后一帧模糊并设置,直到创建新的绑定相机实例。我尝试将getBitmap() 与BitmapDrawable 和setBackground() 一起使用,但这似乎不起作用(屏幕仍然是空白的)。
代码:
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