如何在Android中显示使用CameraX拍摄的图像。
我的案例:我有两个片段。在其中一个片段中,我实现了 CameraX 功能来拍摄照片并将其保存在本地。现在我想将此图像传递给另一个片段并将其显示在 ImageView 中。
最好的方法应该是什么(如果你能提供一个有用的工作代码)
我想到的解决这个问题的方法:
第一种方法:发送从
overriding onImageSaved
中提取的Uri Id(我们在ImageCapture.OutputFileResults
中获取Uri id)。但这很少有问题。将 Uri
作为捆绑包发送或使用 Android 导航发送 safe args
将其显示为 null。这意味着导航中不会传输 Uri。
第二种方法:将
uri
转换为位图,然后将该位图发送到另一个片段。解码位图并将其放在 ImageView 上。我不确定这种方法,如果您之前使用过此方法,请告诉我。
还有其他方法可以解决这个问题吗?我在 Android 官方文档中找不到任何关于此内容的文章。
相机片段(第一个片段):
class CameraFragment : Fragment() {
private var _binding: FragmentCameraBinding? = null
private val binding: FragmentCameraBinding get() = _binding!!
private var imageCapture: ImageCapture? = null
private lateinit var cameraExecutor: ExecutorService
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
_binding = FragmentCameraBinding.inflate(inflater, container, false)
//Request Camera Permissions
// Request camera permissions
if (allPermissionsGranted()) {
startCamera()
} else {
ActivityCompat.requestPermissions(
requireActivity(), REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS
)
}
cameraExecutor = Executors.newSingleThreadExecutor()
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Setting up listeners for taking Photo
binding.imageCaptureButton.setOnClickListener {
takePhoto()
}
}
private fun takePhoto() {
// Get a stable reference of the modifiable image capture use case
val imageCapture = imageCapture ?: return
// Create time stamped name and MediaStore entry.
val name = SimpleDateFormat(FILENAME_FORMAT, Locale.US)
.format(System.currentTimeMillis())
val contentValues = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, name)
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/CameraX-Image")
}
}
// Create output options object which contains file + metadata
val outputOptions = ImageCapture.OutputFileOptions
.Builder(
requireContext().contentResolver,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
contentValues
)
.build()
// Set up image capture listener, which is triggered after photo has
// been taken
imageCapture.takePicture(
outputOptions,
ContextCompat.getMainExecutor(requireContext()),
object : ImageCapture.OnImageSavedCallback {
override fun onError(exc: ImageCaptureException) {
Timber.e("Photo capture failed: ${exc.message}", exc)
}
override fun onImageSaved(output: ImageCapture.OutputFileResults) {
val msg = "Photo capture succeeded: ${output.savedUri}"
Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show()
Timber.d(msg)
val uriFilePath = output.savedUri?.path
val path: String = uriFilePath.toString()
val bundle = Bundle()
bundle.putString("image", path)
Navigation.findNavController(requireView()).navigate(R.id
.cameraFragmentToCameraFinalFragment, bundle)
}
})
}
private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all {
ContextCompat.checkSelfPermission(
requireContext(), it
) == PackageManager.PERMISSION_GRANTED
}
private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext())
cameraProviderFuture.addListener({
// Used to bind the lifecycle of cameras to the lifecycle owner
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
// Preview
val preview = Preview.Builder()
.build()
.also { preview ->
preview.setSurfaceProvider(binding.viewFinder.surfaceProvider)
}
imageCapture = ImageCapture.Builder()
.build()
val imageAnalyzer = ImageAnalysis.Builder()
.setTargetResolution(Size(1280, 720))
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build()
.also {
it.setAnalyzer(cameraExecutor, LuminosityAnalyzer { luma ->
Timber.d("Average luminosity: $luma")
})
}
// Select back camera as a default
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
try {
// Unbind use cases before rebinding
cameraProvider.unbindAll()
// Bind use cases to camera
cameraProvider.bindToLifecycle(
this, cameraSelector, preview, imageCapture, imageAnalyzer
)
} catch (exc: Exception) {
Timber.e("Use case binding failed", exc)
}
}, ContextCompat.getMainExecutor(requireContext()))
}
override fun onRequestPermissionsResult(
requestCode: Int, permissions: Array<String>, grantResults:
IntArray
) {
if (requestCode == REQUEST_CODE_PERMISSIONS) {
if (allPermissionsGranted()) {
startCamera()
} else {
Toast.makeText(
requireContext(), "Permissions not granted by the user.",
Toast.LENGTH_SHORT
).show()
}
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
cameraExecutor.shutdown()
}
companion object {
private const val FILENAME_FORMAT = "yyyy-MM-dd-HH-mm-ss-SSS"
private const val REQUEST_CODE_PERMISSIONS = 10
private val REQUIRED_PERMISSIONS = mutableListOf(
Manifest.permission.CAMERA,
).apply {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
}.toTypedArray()
}
}
照片最终片段(第二个片段):
class PhotoFinalFragment : Fragment() {
private var _binding: FragmentPhotoFinalBinding? = null
private val binding: FragmentPhotoFinalBinding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
_binding = FragmentPhotoFinalBinding.inflate(inflater, container, false)
val bundle: Bundle? = arguments?.getBundle("image")
val uri: Uri = bundle?.get("image") as Uri
binding.imageView.setImageURI(uri)
binding.btnYes.setOnClickListener {
Navigation.findNavController(requireView()).navigate(R.id.cameraFinalFragmentToStoryFragment)
}
binding.btnNo.setOnClickListener {
Navigation.findNavController(requireView()).navigate(R.id.cameraFinalFragmentTocameraFragment)
}
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
使用 ImageCapture.OnImageCapturedCallback 而不是 ImageCapture.OnImageSavedCallback
当使用 OnImageCapturedCallback() 时,您可以重写 onCaptureSuccess,它提供了可以转换为图像并设置为您的 ImageView 的 ImageProxy。
val buffer = imageProxy.planes[0].buffer
val bytes = ByteArray(buffer.capacity())
buffer[bytes]
val bitmapImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
imageProxy.close()
imageView.rotation = imageProxy.imageInfo.rotationDegrees.toFloat()
imageView.setImageBitmap(bitmapImage)
onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
Uri selectedImageUri = outputFileResults.getSavedUri();
Bitmap imageBitmap = BitmapUtils.loadFromUri(MainActivity.this, selectedImageUri);
imgView.setImageBitmap(imageBitmap);
}
捕获成功后,有一个覆盖方法 onimage 保存,您可以将图像设置为。