如何解决java.lang.illegalArgumentException:找不到相机设备支持的表面组合
安卓相机 X
java.lang.illegalArgumentException:找不到相机设备支持的表面组合 - 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);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。