我正在开发一种功能,可以将手电筒切换为开/关状态。几天前,我们在io2019中看到了来自Google的新库。我想出了一个主意,为什么不使用它。
一段时间后,我看不出有任何可能使用库中唯一的割炬。
即使在官方文档中,我也找不到适合我的任何有用信息,而且,他们中的示例应用程序也不必处理我的案件。
您是否有一些容易实现的想法,或者您知道如何使用CameraX做到这一点?
我担心使用camera或camera2,因为要粘贴的代码量太糟糕了。
友情链接:
[1] https://developer.android.com/training/camerax
[2] https://proandroiddev.com/android-camerax-preview-analyze-capture-1b3f403a9395
[3] https://github.com/android/camera/tree/master/CameraXBasic
[4] https://github.com/android/camera/tree/master/CameraXBasic
CameraX是一个Android Jetpack库,旨在简化相机开发。
我正在尝试使用Firebase ML套件扫描二维码,但无法在 android Pie(诺基亚 6.1 Plus)上启动相机。但是,相同的代码可以正常工作Android Oreo Redmi 6 A
这是我的毕业证书:
implementation "com.google.firebase:firebase-ml-vision:21.0.0"
implementation "androidx.camera:camera-core:1.0.0-alpha05"
implementation "androidx.camera:camera-camera2:1.0.0-alpha05"
Run Code Online (Sandbox Code Playgroud)
我已经在 Manifest 中提供了所有必需的权限和相机功能
这是我如何使用 Camera X 代码:
val previewConfig = PreviewConfig.Builder()
.setLensFacing(CameraX.LensFacing.BACK)
.build()
val preview = Preview(previewConfig)
preview.setOnPreviewOutputUpdateListener { previewOutput ->
textureView.surfaceTexture = previewOutput.surfaceTexture
}
val imageAnalysisConfig = ImageAnalysisConfig.Builder()
.build()
val imageAnalysis = ImageAnalysis(imageAnalysisConfig)
Run Code Online (Sandbox Code Playgroud)
日志猫:
E/libc: Access denied finding property "vendor.camera.aux.packagelist"
W/com.zup.cash: type=1400 audit(0.0:17168): avc: denied { read } for name="u:object_r:vendor_camera_prop:s0" dev="tmpfs" ino=18547 scontext=u:r:untrusted_app:s0:c130,c258,c512,c768 tcontext=u:object_r:vendor_camera_prop:s0 tclass=file permissive=0
E/GLConsumer: [SurfaceTexture-0-7457-1] …Run Code Online (Sandbox Code Playgroud) 最近几个月,Android 发布了一个新的 API camerax。我试图了解如何让相机自动对焦工作。
https://groups.google.com/a/android.com/forum/#!searchin/camerax-developers/auto $20focus|sort:date/camerax-developers/IQ3KZd8iOIY/LIbrRIqEBgAJ
这是关于该主题的讨论,但几乎没有关于它的具体文档。
这也是基本的camerax应用程序,但我找不到任何处理自动对焦的文件。
任何有关文档的提示或要点都是有帮助的。另外,我对 android 还很陌生,所以很可能我遗漏了一些使上述链接更有用的东西。
假设我们只想拍摄具有目标1200x1600像素分辨率的肖像(而不是风景)照片。
根据setTargetResolution方法文档ImageCapture.Builder:
分辨率应以用例的目标轮换来表示。例如,请求纵向图像的自然目标旋转中具有纵向自然方向的设备可以指定 480x640,而旋转 90 度并瞄准横向方向的同一设备可以指定 640x480。
我们应该构建ImageCapture这样的用例:
ImageCapture.Builder()
.setTargetResolution(Size(1200, 1600))
.build()
Run Code Online (Sandbox Code Playgroud)
假设我们的手机始终处于纵向自然方向。
一切正常,除了在某些华为手机(例如华为 Mate 20 Lite)上,图像被裁剪,因为我们会反转尺寸(即Size(1600,1200))。
我们可以使用任何“安全”的解决方法来解决这个问题吗?我不想像Signal 那样将华为设备列入黑名单。
CameraX版本1.0.0-beta03。
编辑
1.0.0-beta06还是行不通。
我尝试使用CameraX api为我的应用内相机实现广角选项,但遇到了一个问题 -CameraControl.setZoomRatio允许在ZoomState.getMinZoomRatio()和之间设置变焦ZoomState.getMaxZoomRatio(),在我测试的手机上minZoomRatio是1.0f。同一款手机支持缩小到0.5f系统相机内。
当前片段我如何初始化相机:
private var camera: Camera? = null
private var imageCapture: ImageCapture? = null
private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
val preview = Preview.Builder()
.build()
.also {
it.setSurfaceProvider(binding.viewFinder.surfaceProvider)
}
imageCapture = ImageCapture.Builder()
.setFlashMode(ImageCapture.FLASH_MODE_AUTO)
.build()
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
try {
cameraProvider.unbindAll()
camera = cameraProvider.bindToLifecycle(
this, cameraSelector, preview, imageCapture
)
viewModel.onCameraStarted()
} catch (exc: Exception) {
Timber.e(exc)
}
}, …Run Code Online (Sandbox Code Playgroud) 我想1200x1600使用 CameraX 库拍摄固定分辨率(即 )的照片。
因此,根据文档,我可以设置目标分辨率ImageCaptureConfig:
val imageCaptureConfig = ImageCaptureConfig.Builder()
.setLensFacing(CameraX.LensFacing.BACK)
.setCaptureMode(ImageCapture.CaptureMode.MAX_QUALITY)
.setTargetResolution(Size(1200, 1600))
.setTargetAspectRatio(Rational(3,4))
.build()
Run Code Online (Sandbox Code Playgroud)
文档描述setTargetResolution方法如下:
设置预期的输出目标分辨率。
目标分辨率尝试建立图像分辨率的最小界限。实际图像分辨率将是尺寸上最接近的可用分辨率,且不小于由相机实现确定的目标分辨率。但是,如果不存在等于或大于目标分辨率的分辨率,则将选择小于目标分辨率的最接近的可用分辨率。
如果我错了,请纠正我,但如果设备能够拍摄大于1200x1600(例如3024x4032)的照片,则输出照片将至少为1200x1600。
不幸的是,在许多设备(例如Huawei P20 Pro)中Samsung A5,输出照片明显小于1200x1600例如480x640。请注意,这些设备能够拍摄非常大的照片。
是我的ImageCaptureConfig配置错误还是这是一个错误?
我正在学习CameraX API,CameraXBasic是office 示例代码。
CameraXBasic项目中的CameraFragment.kt显示真实的相机预览。
现在我希望显示负模式预览。如何使用 CameraX API?有示例代码吗?
相机片段.kt
private lateinit var viewFinder: TextureView
private fun bindCameraUseCases() {
// Get screen metrics used to setup camera for full screen resolution
val metrics = DisplayMetrics().also { viewFinder.display.getRealMetrics(it) }
val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)
Log.d(TAG, "Screen metrics: ${metrics.widthPixels} x ${metrics.heightPixels}")
// Set up the view finder use case to display camera preview
val viewFinderConfig = PreviewConfig.Builder().apply {
setLensFacing(lensFacing)
// We request aspect ratio but no resolution to let …Run Code Online (Sandbox Code Playgroud) 目前正在试用新的 CameraX。任何人都知道是什么问题。我应该拥有所需的所有权限。
这是我的整个 logcat。
2019-12-08 03:57:56.716 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:56.717 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:56.717 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:56.717 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:56.718 11667-11696/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:56.718 11667-11696/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:56.759 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:57.192 11667-11667/com.example.camerax E/libc: Access denied finding property "vendor.camera.aux.packagelist"
2019-12-08 03:57:57.215 11667-11667/com.example.camerax E/libc: …Run Code Online (Sandbox Code Playgroud) 我正在尝试以纵向模式创建预览,并且希望预览与屏幕一样宽并垂直环绕内容。但是,当我这样做时,我总是会获得预览尺寸1200x 1200,并且预览会水平拉伸以填充屏幕。
捕获的图像尺寸正确(1024x768.
换句话说,我希望预览尺寸与捕获的图像相同。
任何帮助将不胜感激。
preview = new Preview.Builder()
.setTargetAspectRatioCustom(new Rational(3, 4))
.build();
Run Code Online (Sandbox Code Playgroud)
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.molescope.CameraXActivity">
<androidx.camera.view.PreviewView
android:id="@+id/preview_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<ImageButton
android:id="@+id/capture_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginBottom="@dimen/margin_vertical"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
def camerax_version = "1.0.0-beta03"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-view:1.0.0-alpha10"
implementation "androidx.camera:camera-extensions:1.0.0-alpha10"
Run Code Online (Sandbox Code Playgroud) 我正在试验 Google 的CameraX示例应用程序(CameraXBasic,可以在 Github 上找到),并希望将图像捕获为位图,以便能够在保存图像之前对图像进行一些修改。有没有人有关于如何实现这一目标的建议?
请参阅下面的谷歌原始代码来捕获和保存图像:
// Listener for button used to capture photo
controls.findViewById<ImageButton>(R.id.camera_capture_button).setOnClickListener {
// Get a stable reference of the modifiable image capture use case
imageCapture?.let { imageCapture ->
// Create output file to hold the image
val photoFile = createFile(outputDirectory, FILENAME, PHOTO_EXTENSION)
// Create output options object which contains file + metadata
val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile)
.build()
// Setup image capture listener which is triggered after photo has been taken
imageCapture.takePicture(
outputOptions, cameraExecutor, object …Run Code Online (Sandbox Code Playgroud)