Java 在android中使用摄像头
我正在尝试构建一个Android应用程序,它只使用相机拍照,而不启动默认的相机应用程序。换句话说,我想制作一个定制的相机应用程序。我可以使用Camera hardware object类来实现这一点,但是这是不推荐使用的,我希望使用camerax的一些新功能,而不必担心代码在一段时间后无法工作。我也阅读了CameraAPI文档,但是还不清楚如何使用相机。有没有非常简单的一步一步的教程或指南可以帮助我?Java 在android中使用摄像头,java,android,mobile,Java,Android,Mobile,我正在尝试构建一个Android应用程序,它只使用相机拍照,而不启动默认的相机应用程序。换句话说,我想制作一个定制的相机应用程序。我可以使用Camera hardware object类来实现这一点,但是这是不推荐使用的,我希望使用camerax的一些新功能,而不必担心代码在一段时间后无法工作。我也阅读了CameraAPI文档,但是还不清楚如何使用相机。有没有非常简单的一步一步的教程或指南可以帮助我? 谢谢,您可以查看我的示例,了解如何使用AndroidX库和TextureView进行相机定制
谢谢,您可以查看我的示例,了解如何使用AndroidX库和TextureView进行相机定制 首先,定义你的布局。这是我的活动\u main.xml文件:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextureView
android:id="@+id/view_finder"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/take_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_margin="@dimen/horizontal_margin"
app:layout_constraintStart_toStartOf="parent"
android:src="@drawable/ic_camera"/>
</androidx.constraintlayout.widget.ConstraintLayout>
在这个类中,您可以借助PreviewConfig.Builder()将相机预览发送到TextureView,并使用CameraX.bindToLifeCycle()将其绑定到活动生命周期
此外,不要忘记在清单中添加相机权限,并考虑运行时权限。
屏幕截图:希望这对你有帮助 您可以查看我的示例,了解如何使用AndroidX库和TextureView进行相机自定义 首先,定义你的布局。这是我的活动\u main.xml文件:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextureView
android:id="@+id/view_finder"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/take_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_margin="@dimen/horizontal_margin"
app:layout_constraintStart_toStartOf="parent"
android:src="@drawable/ic_camera"/>
</androidx.constraintlayout.widget.ConstraintLayout>
在这个类中,您可以借助PreviewConfig.Builder()将相机预览发送到TextureView,并使用CameraX.bindToLifeCycle()将其绑定到活动生命周期
此外,不要忘记在清单中添加相机权限,并考虑运行时权限。
屏幕截图:希望这对你有帮助 您是否尝试过
android.hardware.camera2
package,请求诸如:之类的非现场资源被视为离题。您是否尝试过android.hardware.camera2
package,请求诸如:之类的非现场资源被视为离题。