Android实现手机拍照功能攻略
1. 添加权限和依赖项
首先,在AndroidManifest.xml文件中添加相机权限:
<uses-permission android:name=\"android.permission.CAMERA\" />
然后,在app的build.gradle文件中添加相机依赖项:
implementation 'androidx.camera:camera-camera2:1.0.0'
implementation 'androidx.camera:camera-lifecycle:1.0.0'
implementation 'androidx.camera:camera-view:1.0.0-alpha24'
2. 创建相机预览界面
在布局文件中,添加一个PreviewView
来显示相机预览:
<androidx.camera.view.PreviewView
android:id=\"@+id/previewView\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\" />
3. 初始化相机
在Activity或Fragment中,初始化相机并将其与PreviewView
关联起来:
private lateinit var cameraProvider: ProcessCameraProvider
private lateinit var preview: Preview
private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
cameraProvider = cameraProviderFuture.get()
preview = Preview.Builder().build()
preview.setSurfaceProvider(previewView.createSurfaceProvider())
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
val camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview)
}, ContextCompat.getMainExecutor(this))
}
4. 拍照
添加一个按钮或其他触发拍照的UI元素,并在点击事件中执行拍照操作:
private lateinit var imageCapture: ImageCapture
private fun takePhoto() {
val imageCapture = ImageCapture.Builder().build()
val outputFile = File(externalMediaDirs.first(), \"photo.jpg\")
val outputOptions = ImageCapture.OutputFileOptions.Builder(outputFile).build()
imageCapture.takePicture(outputOptions, ContextCompat.getMainExecutor(this),
object : ImageCapture.OnImageSavedCallback {
override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
// 图片保存成功后的处理逻辑
}
override fun onError(exception: ImageCaptureException) {
// 拍照出错时的处理逻辑
}
})
}
示例说明
示例1:拍照并显示预览
class MainActivity : AppCompatActivity() {
private lateinit var cameraProvider: ProcessCameraProvider
private lateinit var preview: Preview
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val previewView = findViewById<PreviewView>(R.id.previewView)
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
cameraProvider = cameraProviderFuture.get()
preview = Preview.Builder().build()
preview.setSurfaceProvider(previewView.createSurfaceProvider())
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
val camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview)
}, ContextCompat.getMainExecutor(this))
}
fun takePhoto(view: View) {
val imageCapture = ImageCapture.Builder().build()
val outputFile = File(externalMediaDirs.first(), \"photo.jpg\")
val outputOptions = ImageCapture.OutputFileOptions.Builder(outputFile).build()
imageCapture.takePicture(outputOptions, ContextCompat.getMainExecutor(this),
object : ImageCapture.OnImageSavedCallback {
override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
// 图片保存成功后的处理逻辑
}
override fun onError(exception: ImageCaptureException) {
// 拍照出错时的处理逻辑
}
})
}
}
示例2:自定义拍照界面
<RelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\">
<androidx.camera.view.PreviewView
android:id=\"@+id/previewView\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\" />
<Button
android:id=\"@+id/captureButton\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:layout_alignParentBottom=\"true\"
android:layout_centerHorizontal=\"true\"
android:layout_marginBottom=\"16dp\"
android:text=\"拍照\"
android:onClick=\"takePhoto\" />
</RelativeLayout>
class MainActivity : AppCompatActivity() {
private lateinit var cameraProvider: ProcessCameraProvider
private lateinit var preview: Preview
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val previewView = findViewById<PreviewView>(R.id.previewView)
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
cameraProvider = cameraProviderFuture.get()
preview = Preview.Builder().build()
preview.setSurfaceProvider(previewView.createSurfaceProvider())
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
val camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview)
}, ContextCompat.getMainExecutor(this))
}
fun takePhoto(view: View) {
val imageCapture = ImageCapture.Builder().build()
val outputFile = File(externalMediaDirs.first(), \"photo.jpg\")
val outputOptions = ImageCapture.OutputFileOptions.Builder(outputFile).build()
imageCapture.takePicture(outputOptions, ContextCompat.getMainExecutor(this),
object : ImageCapture.OnImageSavedCallback {
override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
// 图片保存成功后的处理逻辑
}
override fun onError(exception: ImageCaptureException) {
// 拍照出错时的处理逻辑
}
})
}
}
以上是实现Android手机拍照功能的完整攻略,其中包含了两个示例说明。你可以根据自己的需求选择其中一种方式来实现。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android实现手机拍照功能 - Python技术站