我正在使用 CameraX 通过我的应用程序拍照。我已经在 Sony Xperia (Android Nougat)、Huawei P9 (Android Nougat)、Samsung Galaxy A3 (Android Oreo) 和 Samsung S8+ (Android Pie) 上对其进行了测试。
它在前三个上效果很好,但在我的 S8+ 上,我的相机的 textureView 是像素化的(就像地狱一样)。但是当我拍照时,它的质量很棒。
这是我启动相机时的代码。有谁知道可能是什么原因?我多次更改了 PreviewConfig 和 ImageCaptureConfig,但是当我这样做时,我的照片质量很差,而且在我的 S8+ 上,如果我慢慢旋转手机,屏幕就会变形。
private void startCamera() {
DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Display mDisplay = getWindowManager().getDefaultDisplay();
mDisplay.getRealMetrics(mDisplayMetrics);
PreviewConfig mPreviewConfig = new PreviewConfig.Builder()
.setTargetResolution(new Size(mViewFinderTV.getWidth(), mViewFinderTV.getHeight()))
.setTargetAspectRatio(new Rational(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels))
.setTargetRotation(getWindowManager().getDefaultDisplay().getRotation())
.setTargetRotation(mViewFinderTV.getDisplay().getRotation())
.build();
Preview mPreview = new Preview(mPreviewConfig);
mPreview.setOnPreviewOutputUpdateListener(output -> {
ViewGroup mViewGroup = (ViewGroup) mViewFinderTV.getParent();
mViewGroup.removeView(mViewFinderTV);
mViewGroup.addView(mViewFinderTV, 0);
mViewFinderTV.setSurfaceTexture(output.getSurfaceTexture());
});
ImageCaptureConfig mImageCaptureConfig = new ImageCaptureConfig.Builder()
.setTargetAspectRatio(new Rational(mViewFinderTV.getWidth(), mViewFinderTV.getHeight()))
.setTargetRotation(getWindowManager().getDefaultDisplay().getRotation())
.setCaptureMode(ImageCapture.CaptureMode.MAX_QUALITY)
.build();
ImageCapture mImageCapture = new ImageCapture(mImageCaptureConfig);
mTakePhotoFAB.setOnClickListener(view -> {
mDir = new File(getExternalCacheDir(), "PhotosAuthentifier");
boolean success = true;
if (!mDir.exists()) {
success = mDir.mkdir();
}
if (success) {
File mFile = new File(mDir, new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.getDefault()).format(new Date()) + ".jpg");
mImageCapture.takePicture(mFile,
new ImageCapture.OnImageSavedListener() {
@Override
public void onImageSaved(@NonNull File file) {
mListOfPhotos.add(file.getAbsolutePath());
mAdapter.setmListOfPhotos(mListOfPhotos);
mRecyclerView.setAdapter(mAdapter);
if (mListOfPhotos.size() < 3) {
actualNumberOfPhoto();
} else {
new AsyncTaskUpdateRecyclerView().execute();
}
}
@Override
public void onError(@NonNull ImageCapture.ImageCaptureError imageCaptureError, @NonNull String message, @Nullable Throwable cause) {
String mMessage = "Photo capture failed: " + message;
Toast.makeText(CameraActivity.this, mMessage, Toast.LENGTH_SHORT).show();
assert cause != null;
cause.printStackTrace();
}
});
}
});
mSendPhotoFAB.setOnClickListener(view -> {
AlertDialog.Builder mAlertDialogBuilder = new AlertDialog.Builder(CameraActivity.this);
mAlertDialogBuilder
.setCancelable(false)
.setMessage(R.string.validationBeforePursue)
.setPositiveButton(R.string.yes, (dialogInterface, i) -> {
if (mIDDemande != 0) {
new AsyncTaskSendComplementaryPhotos(this).execute();
} else {
new AsyncTaskSendData(this).execute();
}
})
.setNegativeButton(R.string.no, (dialogInterface, i) -> dialogInterface.dismiss());
AlertDialog mAlertDialog = mAlertDialogBuilder.create();
mAlertDialog.show();
});
mGalleryFAB.setOnClickListener(view -> {
checkPermissions();
if (mPermission) {
Intent mIntentGallery = new Intent(Intent.ACTION_PICK);
mIntentGallery.setType("image/*");
startActivityForResult(mIntentGallery, GALLERY_REQUEST);
}
});
CameraX.bindToLifecycle(this, mPreview, mImageCapture);
}