android - java.lang.illegalArgumentException:找不到相机设备支持的表面组合
问题描述
安卓相机 X
java.lang.illegalArgumentException: No Supported surface combination is found for camera device - id: 1 可能试图绑定太多案例。
现有表面:[] 新配置:[androidx.camera.core.impl.ImageAnalysisConfig@37a677,androidx.camera.core.Impl.PreviewConfig@174f711,androidx.camera.core.impl.ImageCaptureConfig@e20f34d]
执行以下行时出现异常。
Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner)this, cameraSelector, preview,
imageCapture, imageAnalysis);
Used Dependencies
def camerax_version = "1.0.0-beta05"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-view:1.0.0-alpha12"
implementation "androidx.camera:camera-extensions:1.0.0-alpha12"
专门针对 Samaung A71 Android 10 设备和目前其他运行良好的设备
注意:我无法调试,因为这是在最终用户设备中出现的。
解决方案
你可以使用我在 Github 上分享的代码。
在这里,我将 CameraX 用于相机和多次点击图像将显示在相机屏幕中
完整源代码/项目: https ://github.com/abhishekhzb/quick_camera
筛选:
XML 代码:
<TextureView
android:id="@+id/view_finder"
android:layout_marginTop="90dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout"
app:layout_constraintVertical_bias="0.0" />
Java 代码:
private void startCamera() {
CameraX.unbindAll();
Rational aspectRatio = new Rational(textureView.getWidth(), textureView.getHeight());
Size screen = new Size(textureView.getWidth(), textureView.getHeight()); //size of the screen
PreviewConfig pConfig = new PreviewConfig.Builder().setTargetAspectRatio(aspectRatio).setTargetResolution(screen).build();
Preview preview = new Preview(pConfig);
preview.setOnPreviewOutputUpdateListener(
new Preview.OnPreviewOutputUpdateListener() {
//to update the surface texture we have to destroy it first then re-add it
@Override
public void onUpdated(Preview.PreviewOutput output) {
ViewGroup parent = (ViewGroup) textureView.getParent();
parent.removeView(textureView);
parent.addView(textureView, 0);
textureView.setSurfaceTexture(output.getSurfaceTexture());
updateTransform();
}
});
ImageCaptureConfig imageCaptureConfig = new ImageCaptureConfig.Builder().setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
.setTargetRotation(getWindowManager().getDefaultDisplay().getRotation()).build();
imgCap = new ImageCapture(imageCaptureConfig);
findViewById(R.id.imgCapture).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String root = Environment.getExternalStorageDirectory().toString();
myDir = new File(root + "/QuickCamera/" + folderName);
myDir.mkdirs();
count++;
counter.setText(String.valueOf(count));
fname = "" + folderName + "-" + count + ".jpg";
File file = new File(myDir, fname);
if (file.exists()) file.delete();
// File file = new File(Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".png");
imgCap.takePicture(file, new ImageCapture.OnImageSavedListener() {
@Override
public void onImageSaved(@NonNull File file) {
String msg = "Pic captured at " + file.getAbsolutePath();
Log.e("PATH", "" + msg);
list.add(new ImageModel(count, file.getAbsolutePath(), fname));
mAdapter = new ImageRecyclerAdapter(CameraActivity.this, list);
recyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
@Override
public void onError(@NonNull ImageCapture.UseCaseError useCaseError, @NonNull String message, @Nullable Throwable cause) {
String msg = "Pic capture failed : " + message;
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
if (cause != null) {
cause.printStackTrace();
}
}
});
}
});
//bind to lifecycle:
CameraX.bindToLifecycle((LifecycleOwner) this, preview, imgCap);
}
private void updateTransform() {
Matrix mx = new Matrix();
float w = textureView.getMeasuredWidth();
float h = textureView.getMeasuredHeight();
float cX = w / 2f;
float cY = h / 2f;
int rotationDgr;
int rotation = (int) textureView.getRotation();
switch (rotation) {
case Surface.ROTATION_0:
rotationDgr = 0;
break;
case Surface.ROTATION_90:
rotationDgr = 90;
break;
case Surface.ROTATION_180:
rotationDgr = 180;
break;
case Surface.ROTATION_270:
rotationDgr = 270;
break;
default:
return;
}
mx.postRotate((float) rotationDgr, cX, cY);
textureView.setTransform(mx);
}
推荐阅读
- maven - 在 pom.xml 中为 Maven 设置代理(不在 settings.xml 中)
- javascript - V-html 指令不适用于获取的数据
- c# - 无法隐式转换类型'System.Collections.Generic.List
' 到 'DTO.ProductivityDTO' - c# - 模仿 SceneView 的自定义窗口
- arrays - Excel如何在没有VBA的情况下从逗号分隔列表中查找所有匹配元素的行
- r - 如何从r中的第二行减去一天?
- javascript - 尝试测试 tcf 同意时无效的 cmp 类型
- command - 赛普拉斯:我想从 JSON 获取数据并将其作为参数传递给其他测试
- android - android:如何处理前台通知的点击
- java - Spring boot 2 Redis缓存默认客户端