好吧,我浏览了不同的帖子,发现根据移动制造商的不同,可能会出现捕获图像旋转等复杂情况,因此您必须意识到这一点。我所做的是:
fun rotateBitmap(bitmap: Bitmap): Bitmap? {
val matrix = Matrix()
when (getImageOrientation(bitmap)) {
ExifInterface.ORIENTATION_NORMAL -> return bitmap
ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> matrix.setScale(-1f, 1f)
ExifInterface.ORIENTATION_ROTATE_270 -> matrix.setRotate(-90f)
ExifInterface.ORIENTATION_ROTATE_180 -> matrix.setRotate(180f)
ExifInterface.ORIENTATION_ROTATE_90 -> matrix.setRotate(90f)
ExifInterface.ORIENTATION_FLIP_VERTICAL -> {
matrix.setRotate(180f)
matrix.postScale(-1f, 1f)
}
ExifInterface.ORIENTATION_TRANSPOSE -> {
matrix.setRotate(90f)
matrix.postScale(-1f, 1f)
}
ExifInterface.ORIENTATION_TRANSVERSE -> {
matrix.setRotate(-90f)
matrix.postScale(-1f, 1f)
}
else -> return bitmap
}
Run Code Online (Sandbox Code Playgroud)
这有效。但后来我注意到一些非常奇怪的事情,这可能与我配置 Camera X 的方式有关。
使用相同的设备,我得到不同旋转的位图(好吧,这不应该发生。如果设备奇怪地旋转图像,它应该以两种模式旋转图像 - inImageAnalysesUseCase和ImageCaptureUseCase)。
那么,为什么会发生这种情况以及如何解决它?
代码实现:
将相机 X 绑定到生命周期:
CameraX.bindToLifecycle(
this,
buildPreviewUseCase(),
buildImageAnalysisUseCase(),
buildImageCaptureUseCase()
)
Run Code Online (Sandbox Code Playgroud)
预览用例:
private …Run Code Online (Sandbox Code Playgroud) 我想在没有任何 xml 和 PreviewView 或其他用于显示图片的东西的情况下使用 CameraX 拍照,但我有问题。
在相机 1 中,我们有FaceDetectionListener方法camera.startFaceDetection()。使用这种方式可以更容易地找到面孔。
在相机 2 中,我们可以使用 CameraCaptureSession.CaptureCallback() 方法和此静态变量执行相同操作
Integer mode = result.get(CaptureResult.STATISTICS_FACE_DETECT_MODE);
Face[] faces = result.get(CaptureResult.STATISTICS_FACES);
Run Code Online (Sandbox Code Playgroud)
现在有一个名为 CameraX 的新相机库。它是Camera2的包装,推荐使用。如果是Camera 2的封装,我们可以很容易得到如下的回调结果CameraCaptureSession.CaptureCallback()
但经过三天的尝试,我未能找到解决方案。
谁能给我像camera2一样调用以下方法的解决方案?
在相机2中,
private val mCaptureCallback = object : CameraCaptureSession.CaptureCallback() {
override fun onCaptureProgressed(
session: CameraCaptureSession,
request: CaptureRequest,
partialResult: CaptureResult
) {
}
override fun onCaptureCompleted(
session: CameraCaptureSession,
request: CaptureRequest,
result: TotalCaptureResult
) {
}
}
mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback,
mBackgroundHandler);
Run Code Online (Sandbox Code Playgroud)
如何使用CameraX获取回调结果?
我仍在练习 Kotlin 和 Android 开发。据我了解,Camera 类已被弃用,Android 邀请使用 Camerax 来代替,因为这个高级类是独立于设备的,并且它们简化了在应用程序上实现相机的过程。
我尝试阅读文档(https://developer.android.com/training/camerax),但它写得太糟糕了,我几乎不明白他们试图解释的内容。所以我去阅读了文档本身给出的整个示例代码(https://github.com/android/camera-samples/tree/main/CameraXBasic)。CameraFragment 代码大约有 500 行长(忽略导入和各种注释)。
我真的需要编写 500 行代码来简单地拍照吗?这怎么能被认为是“比以前简单”呢?
我的意思是,在 Android 编程中,我只需要编写 4 行代码即可要求用户从其存储中选择一个图像并检索它并将其显示在 ImageView 中。有没有一种真正简单的拍照方法,或者我真的需要停下来并失去一整天的工作来编写所有这些代码行?
编辑:获取文档的此页面: https://developer.android.com/training/camerax/architecture#kotlin 它以这段代码开始。
val preview = Preview.Builder().build()
val viewFinder: PreviewView = findViewById(R.id.previewView)
// The use case is bound to an Android Lifecycle with the following code
val camera = cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, preview)
Run Code Online (Sandbox Code Playgroud)
CameraProvider 不知从何而来。这应该是什么?我发现它是一个 ProcessCameraProvider,但是我应该如何初始化它?它应该是一个 Lateinit var 还是已经在其他地方初始化了?因为如果我尝试写入,val cameraProvider = ProcessCameraProvider()就会出现错误,那么我该怎么办?什么是cameraSelector参数?之前没有定义过。我发现它是前置摄像头或后置摄像头的选择器,但是阅读该文档的那一页我怎么知道它呢?这份文档怎么可能在发布时存在这些缺陷呢?怎样才能让一个人轻松学习呢?
我想要做的是在我的上方显示一个矩形PreviewView,并且仅记录该矩形内部的区域,即使 PreviewView 显示整个相机也是如此。
我只想将白色矩形内的内容保存到文件中,但我希望预览显示所有内容:

活动.xml:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.camera.view.PreviewView
android:id="@+id/textureView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/crop"
android:layout_width="350dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="@drawable/background_drawable"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
活动.kt
private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
cameraProvider = cameraProviderFuture.get()
// The display information
val metrics = DisplayMetrics().also { textureView.display.getRealMetrics(it) }
// The ratio for the output image and preview
val aspectRatio = aspectRatio(metrics.widthPixels, metrics.heightPixels)
// The display rotation
val rotation = textureView.display.rotation
val localCameraProvider = …Run Code Online (Sandbox Code Playgroud) 我正在按照此Codelab创建一个cameraX应用程序。
它在我的手机上运行良好。但我正在开发的应用程序是针对 Android 媒体播放器的。它没有任何内置摄像头,仅连接了外部 USB 摄像头。
这是我启动相机的代码。
private void startCamera() {
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
Preview preview = new Preview.Builder().build();
preview.setSurfaceProvider(viewFinder.getSurfaceProvider());
imageCapture = new ImageCapture.Builder().build();
cameraProviderFuture.addListener(() -> {
try {
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
cameraProvider.unbindAll();
cameraProvider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA,preview,imageCapture);
} catch (ExecutionException e) {
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
} catch (InterruptedException e) {
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
catch (IllegalArgumentException e){
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}, ContextCompat.getMainExecutor(this)
);
}
Run Code Online (Sandbox Code Playgroud)
它抛出IllegalArgumentException说没有连接相机。
在cameraProvider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA,preview,imageCapture);
唯一CameraSelector.DEFAULT_FRONT_CAMERA且CameraSelector.DEFAULT_BACK_CAMERA
可用。 …
我正在尝试实现 Camera X 应用程序。我现在面临的问题是我无法弄清楚如何使用以下命令将下面的图像保存到特定文件夹ImageCapture.OutputFileOptions.Builder
我当前的代码如下。我需要采用其他方式吗?或者我也可以这样做?
private void capturePhoto() {
showProgress(true);
long currentTime = System.currentTimeMillis();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, makeImageName(currentTime));
if (Build.VERSION.SDK_INT >= 29) {
contentValues.put(MediaStore.Images.Media.DATE_TAKEN, currentTime);
contentValues.put(MediaStore.Images.Media.RELATIVE_PATH,
Environment.DIRECTORY_PICTURES + "/" + FOLDER_IMAGES);
} else {
// Todo ( Something equivalent to RELATIVE_PATH)
}
ImageCapture.OutputFileOptions options = new ImageCapture.OutputFileOptions.Builder(
getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues).build();
imageCapture.takePicture(options,
ContextCompat.getMainExecutor(this),
new ImageCapture.OnImageSavedCallback() {
@Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults results) {
ToastUtility.successToast(getApplicationContext(),
"Photo Capture Successfully");
showProgress(false);
}
@Override
public void onError(@NonNull ImageCaptureException exception) …Run Code Online (Sandbox Code Playgroud) 我们想要自动化应用程序的测试,主要功能涉及来自相机的视频流的图像处理。
使用 Appium 注入视频流并使其在应用程序中看起来就像来自设备的摄像头的最佳方法是什么?
我们正在寻找一种无需更改应用程序代码即可实现此目标的方法。
问题是关于 Android 的,但如果您知道如何在 iOS 中执行此操作,我们将不胜感激,这是针对 iOS 的问题:iOS:iOS Appium 相机测试:如何注入视频,就像来自相机一样,以使用 Appium 测试应用程序
automated-tests android-camera kotlin appium android-camerax
我想知道如何检测用户何时在伴奏者权限库中撤销权限(两次拒绝权限),我还检查了该库的 GitHub 存储库并且示例是旧的。
我在用,
compose_version = '1.2.0-alpha03'
accompanist_version = '0.24.2-alpha'
这是我的代码片段,
@ExperimentalMaterial3Api
@ExperimentalPermissionsApi
@Composable
fun CameraPermission() {
/* Camera permission state.*/
val cameraPermissionState = rememberPermissionState(permission = Manifest.permission.CAMERA)
val context = LocalContext.current
val intent =Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", BuildConfig.APPLICATION_ID, null))
when (cameraPermissionState.status) {
/* If the camera permission is granted, then show screen with the feature enabled.*/
PermissionStatus.Granted -> {
Text("Camera permission Granted")
}
is PermissionStatus.Denied -> {
/*
* This is a rationale explaining why we need the …Run Code Online (Sandbox Code Playgroud) android android-permissions android-camerax android-jetpack-compose jetpack-compose-accompanist
我正在按照官方文档使用 CameraX 捕获视频,并且我想避免将捕获的视频保存在图库中。
现在我正在处理文档代码的这一部分:
// Create MediaStoreOutputOptions for our recorder
val name = "CameraX-recording-" +
SimpleDateFormat(FILENAME_FORMAT, Locale.US)
.format(System.currentTimeMillis()) + ".mp4"
val contentValues = ContentValues().apply {
put(MediaStore.Video.Media.DISPLAY_NAME, name)
}
val mediaStoreOutput = MediaStoreOutputOptions.Builder(this.contentResolver,
MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
.setContentValues(contentValues)
.build()
// 2. Configure Recorder and Start recording to the mediaStoreOutput.
val recording = videoCapture.output
.prepareRecording(context, mediaStoreOutput)
.withAudioEnabled()
.start(ContextCompat.getMainExecutor(this), captureListener)
Run Code Online (Sandbox Code Playgroud)
我注意到prepaeRecording()可以采用FileOutputOptions而不是MediaStoreOutput,所以我认为它可能是我应该工作的地方,但我没有找到任何 FileOutputOptions 和 CameraX 的示例,而且我希望它能够使用范围权限。
那可能吗?您能举个例子来避免将视频保存到图库吗?
android-camerax ×10
android ×9
kotlin ×3
camera ×2
appium ×1
callback ×1
java ×1
jetpack-compose-accompanist ×1
rotation ×1
video ×1