我想要做的是在我的上方显示一个矩形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 的示例,而且我希望它能够使用范围权限。
那可能吗?您能举个例子来避免将视频保存到图库吗?
我正在使用该库的 1.1.0 版本浏览Android 开发人员https://developer.android.com/codelabs/camerax-getting-started#3上的 CameraX 教程。日志一开始看起来不错,但后来引发了 TimeoutException 并且相机预览保持黑色:
(omitted)
D/CameraStateRegistry: Recalculating open cameras:
Camera State
-------------------------------------------------------------------
Camera@f35a3d7[id=0] OPEN
Camera@e46a673[id=1] UNKNOWN
-------------------------------------------------------------------
Open count: 1 (Max allowed: 1)
D/CameraStateMachine: New public camera state CameraState{type=OPEN, error=null} from OPEN and null
D/CameraStateMachine: Publishing new public camera state CameraState{type=OPEN, error=null}
D/UseCaseAttachState: All use case: [androidx.camera.core.Preview-c7727ae2-d02c-4135-956c-a76c24deb02d40667318] for camera: 0
D/UseCaseAttachState: Active and attached use case: [androidx.camera.core.Preview-c7727ae2-d02c-4135-956c-a76c24deb02d40667318] for camera: 0
D/Camera2CameraImpl: {Camera@f35a3d7[id=0]} Transitioning camera internal state: OPENED --> OPENED
D/CameraStateMachine: New public camera state …Run Code Online (Sandbox Code Playgroud) 我正在创建一个工具,可以使用cameraX从预览中捕获每一帧(用于面部识别)
我发现使用ImageAnalysis是必经之路。
直到我尝试使用推荐的代码:
val imageAnalysisConfig = ImageAnalysisConfig.Builder()
.setTargetResolution(Size(1280, 720))
.setImageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
.build()
val imageAnalysis = ImageAnalysis(imageAnalysisConfig)
imageAnalysis.setAnalyzer({ image: ImageProxy, rotationDegrees: Int ->
viewModel.onAnalyzeImage(image)
})
Run Code Online (Sandbox Code Playgroud)
我在setAnalyser方法上收到以下编译错误:
None of the following function can be called with the arguments supplied
setAnalyser((command : Runnable!) -> Unit, (image: ImageProxy!, rotationDegrees: Int) -> Unit)
Run Code Online (Sandbox Code Playgroud) 我想ImageAnalysis与 CameraX一起使用,但要调整一些相机设置,例如自动对焦或自动白平衡、曝光和帧持续时间。
这是我需要的设置示例以及我如何使用 Camera2 设置它们:
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_OFF)
captureRequestBuilder.set(CaptureRequest.CONTROL_AWB_MODE, CameraMetadata.CONTROL_AWB_MODE_OFF)
captureRequestBuilder.set(CaptureRequest.SENSOR_FRAME_DURATION, FRAME_DURATION_NS)
captureRequestBuilder.set(CaptureRequest.SENSOR_EXPOSURE_TIME, EXPOSURE_TIME_LIMIT_NS)
Run Code Online (Sandbox Code Playgroud)
如何将其“翻译”到 CameraX?
我正在关注有关如何在 Android 上使用 CameraX API 的此代码实验室示例链接,但是当我尝试捕获图像并将其保存到在主活动的 Oncreate 方法中创建的外部媒体目录时,我收到一条错误消息:无法将捕获结果保存到指定位置
下面是创建目录的方法,在 Oncreate 方法中调用:
private fun getOutputDirectory(): File {
val mediaDir = externalMediaDirs.firstOrNull()?.let {
File(it, resources.getString(R.string.app_name)).apply { mkdirs() } }
return if (mediaDir != null && mediaDir.exists())
{ Log.i(TAG, mediaDir.path); mediaDir } else { filesDir }
}
Run Code Online (Sandbox Code Playgroud)
根据我在 Android 文档中阅读的内容,externalMediaDirs在外部存储中创建了一个文件夹,虽然我的手机没有外部存储,但该文件夹已在此路径下成功创建:/storage/emulated/0/Android/media/com.example.camerax/cameraX
然后当点击拍照图像按钮时调用此方法:
private fun takePhoto() {
// Get a stable reference of the modifiable image capture use case
val imageCapture = imageCapture ?: return
// Create time-stamped output file …Run Code Online (Sandbox Code Playgroud) android-camerax ×10
android ×9
kotlin ×3
appium ×1
camera ×1
java ×1
jetpack-compose-accompanist ×1
video ×1