Skip to content

Commit

Permalink
Add JPEG_R capture to extension sample camera app
Browse files Browse the repository at this point in the history
  • Loading branch information
r-dhanjal committed Jul 4, 2024
1 parent ab6f960 commit f863493
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ class CameraFragment : Fragment(), TextureView.SurfaceTextureListener {
previewSurface = createPreviewSurface(previewSize)
stillImageReader = createStillImageReader()
postviewImageReader = createPostviewImageReader()
if (postviewImageReader == null) {
isPostviewAvailable = false
}

val outputConfig = ArrayList<OutputConfiguration>()
outputConfig.add(OutputConfiguration(stillImageReader.surface))
Expand Down Expand Up @@ -722,14 +725,26 @@ class CameraFragment : Fragment(), TextureView.SurfaceTextureListener {
* Creates the still image reader and sets up OnImageAvailableListener
*/
private fun createStillImageReader(): ImageReader {
var stillFormat: Int
var stillCaptureSize: Size

val yuvColorEncodingSystemSizes = extensionCharacteristics.getExtensionSupportedSizes(
currentExtension, ImageFormat.YUV_420_888
)
val jpegSizes = extensionCharacteristics.getExtensionSupportedSizes(
currentExtension, ImageFormat.JPEG
)
val stillFormat = if (jpegSizes.isEmpty()) ImageFormat.YUV_420_888 else ImageFormat.JPEG
val stillCaptureSize = if (jpegSizes.isEmpty()) yuvColorEncodingSystemSizes[0] else jpegSizes[0]
stillFormat = if (jpegSizes.isEmpty()) ImageFormat.YUV_420_888 else ImageFormat.JPEG
stillCaptureSize = if (jpegSizes.isEmpty()) yuvColorEncodingSystemSizes[0] else jpegSizes[0]

val jpegRSizes = extensionCharacteristics.getExtensionSupportedSizes(
currentExtension, ImageFormat.JPEG_R
)
if (args.hdr) {
stillFormat = if (jpegRSizes.isEmpty()) stillFormat else ImageFormat.JPEG_R
stillCaptureSize = if (jpegRSizes.isEmpty()) stillCaptureSize else jpegRSizes[0]
}

val stillImageReader = ImageReader.newInstance(
stillCaptureSize.width,
stillCaptureSize.height, stillFormat, 1
Expand All @@ -742,7 +757,8 @@ class CameraFragment : Fragment(), TextureView.SurfaceTextureListener {
hideCaptureProgressUI()
val file = File(
requireActivity().getExternalFilesDir(null),
if (image.format == ImageFormat.JPEG) "frame.jpg" else "frame.yuv"
if (image.format == ImageFormat.JPEG
|| image.format == ImageFormat.JPEG_R) "frame.jpg" else "frame.yuv"
)
output = FileOutputStream(file)
output.write(getDataFromImage(image))
Expand Down Expand Up @@ -790,9 +806,11 @@ class CameraFragment : Fragment(), TextureView.SurfaceTextureListener {
if (!jpegSupportedSizes.isEmpty()) {
postviewSize = jpegSupportedSizes[0]
postviewFormat = ImageFormat.JPEG
} else {
} else if (!yuvSupportedSizes.isEmpty()){
postviewSize = yuvSupportedSizes[0]
postviewFormat = ImageFormat.YUV_420_888
} else {
return null
}
val postviewImageReader =
ImageReader.newInstance(postviewSize.width, postviewSize.height, postviewFormat, 1)
Expand Down Expand Up @@ -1235,7 +1253,7 @@ class CameraFragment : Fragment(), TextureView.SurfaceTextureListener {
val planes = image.planes
var buffer: ByteBuffer
var offset = 0
if (format == ImageFormat.JPEG) {
if (format == ImageFormat.JPEG || format == ImageFormat.JPEG_R) {
buffer = planes[0].buffer
data = ByteArray(buffer.limit())
buffer.rewind()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package com.example.android.camera2.extensions.fragments

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.ImageFormat;
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CameraManager
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -61,7 +63,7 @@ class SelectorFragment : Fragment() {
view.setOnClickListener {
Navigation.findNavController(requireActivity(), R.id.fragment_container)
.navigate(SelectorFragmentDirections.actionSelectorToCamera(
item.cameraId))
item.cameraId, item.hdr))
}
}
}
Expand All @@ -71,7 +73,8 @@ class SelectorFragment : Fragment() {

private data class CameraInfo(
val name: String,
val cameraId: String)
val cameraId: String,
val hdr: Boolean = false)

/** Converts a lens orientation enum into a human-readable string */
private fun lensOrientationString(value: Int) = when (value) {
Expand Down Expand Up @@ -99,9 +102,18 @@ class SelectorFragment : Fragment() {
.REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) &&
extensionCharacteristics.supportedExtensions.isNotEmpty()) {
availableCameras.add(CameraInfo("$orientation ($id)", id))
val currentExtension = extensionCharacteristics.supportedExtensions[0]

if (Build.VERSION.SDK_INT >= 35) {
var jpegRSizes = extensionCharacteristics.getExtensionSupportedSizes(
currentExtension, ImageFormat.JPEG_R
)
if (jpegRSizes.isNotEmpty()) {
availableCameras.add(CameraInfo("$orientation ($id) JPEG_R", id, true))
}
}
}
}

return availableCameras
}
}
Expand Down
5 changes: 5 additions & 0 deletions Camera2Extensions/app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
app:argType="string"
app:nullable="false"/>

<argument
android:name="hdr"
app:argType="boolean"
app:nullable="false"/>

</fragment>

</navigation>

0 comments on commit f863493

Please sign in to comment.